2017-02-19 10 views
0

私の目標は、パーセントでグレードを取り、そのウェイト値(10進形式またはパーセント形式のいずれか)を乗算するプログラムを作成して作成することです。式は基本的には次のとおりです。配列からの入力をC++の式に入力する方法は?

全体のグレード=(グレード1 * weightInDecimal1)+(grade2 * weightInDecimal2)+(grade3 * weightInDecimal3)+ ...

または

全体のグレード=(グレード1 *重量% 1)+(grade2 *重量%2)+(grade3 *重量%3)+ ...


入力を保存し、後でコードでそれをリコールする方法はありますか?あるいはもっと効率的な方法でしょうか?

また、動的配列を作成したいと考えています。私はユーザーに割り当てられているアサインメントの数を尋ね、それに基づいて配列を作成するプログラムを作りたいと思っています。その方法は、それが4つの割り当て

#include <string> 
#include <iostream> 
using namespace std; 
int main() { 
    int numbers[4][2]; 
    for(int i=0;i<4;i++) 
    { 
     cout<<"Grade #"<<i<<endl; 
     cin>>numbers[i][0]; 
     cout<<"Weight for grade #"<<i<<":"<<endl; 
     cin>>numbers[i][1]; 
    } 

    for (int i = 0; i<4; i++) 
    { 
     cout << "|" << numbers[i][0]*numbers[i][1]<< "|"; 
    } 
    system ("PAUSE"); 
return 0; 
} 
+1

intstd::vectorのカップルは、なぜ配列の代わりに 'のstd :: vector'を使わないのでしょうか?次に、必要な数の要素を動的に追加できます。 –

答えて

0

で立ち往生していないこれは、構造体は

#include <string> 
#include <iostream> 
#include <array> 
using namespace std; 

struct entry { 
    int grade; 
    int weight; 
    int gradeWeight; //grade*weight 
}; 

int main() { 
    array<entry,4> numbers; 
    for(int i=0;i<numbers.max_size();i++) 
    { 
     cout<<"Grade #"<<i<<endl; 
     cin>>numbers[i].grade; 
     cout<<"Weight for grade #"<<i<<":"<<endl; 
     cin>>numbers[i].weight; 
    } 

    for (int i = 0; i<numbers.max_size(); i++) 
    { 
     numbers[i].gradeWeight = numbers[i].grade*numbers[i].weight; 
     cout << "|" << numbers[i].gradeWeight << "|"; 
    } 
    system ("PAUSE"); 
    return 0; 
} 

あなたはまた、単にアレイのサイズを増やすことで数字の量を増やすことができますこの方法のためにあるものです。

+1

あなたは '#を含める必要があります ' – Olipro

+0

ありがとうございます。 1つは、それを投稿する前にコードをテストする必要があります^^ –

0

アレイの使用を避ける理由は多数あります(動的またはその他)。たとえば、StroustrupのFAQエントリWhat's wrong with arrays?を参照してください。std::vectorのようなコンテナを使用すると、コメントにGregが示唆するように、より良い品質のコードを書く可能性が高くなります。

あなたが使用することを好む場合は、(必要であれば)...コンストラクタに一方

auto syze{ 0 }; 
auto initialValue{ 0 }; 

// calculate or input syze 
. . . 

// dynamically allocate an "array" 
// of ints and an "array" of floats 
std::vector<int> grades(syze, initialValue); 
std::vector<float> weights(syze, initialValue); 

をそのサイズを渡すことができ、それを割り当てる前に、あなたのコンテナのサイズを計算したり、入力することができた場合動的にデータを保持するために成長し、コンテナが到着として、あなたがそれを行うことができますあまりにも...

// dynamically allocate an empty "array" 
// of ints and an empty "array" of floats 
std::vector<int> grades; 
std::vector<float> weights; 

while (...condition...) 
{ 
    std::cin >> g; // input a grade ... 
    std::cin >> w; // ... and a weight 

    // grow the containers by adding 
    // one item at the end of each 
    grades.emplace_back(g); 
    weights.emplace_back(w); 
} 

更新

私は2つのベクトルから結果を計算する方法を指摘しておきました。 STLの<numeric>ヘッダーのstd::inner_productを使用して、コードの行を1つ追加するだけで全体のグレードを計算することができます。以下のコードでは、最後の引数はstd::inner_productintではなくdoubleを返すように0.0(ちょうど0ではなく)です。これにより、floatの値がintに切り捨てられてしまう危険性が回避されます(コンパイラーからのかなり醜い警告は避けられます)。

auto overallGrade = std::inner_product(grades.begin(), grades.end(), weights.begin(), 0.0); 
0

あなたは彼らが持っているどのように多くの割り当てについてユーザーに言わせればそれは次元がコンパイル時に固定されているためstd::arrayが間違っコンテナで、他の人が指摘したように。

他のものとして、私は直接メモリ割り当ての使用を控除しますが、それを管理するためにstd::vectorの使用を除いています。

割り当ての数がわかっている場合は、reserve()(方法はstd::vector)の使用をお勧めします。

次の完全な例を使用して、代わりに単一std::vectorstd::pair<int, int>

#include <utility> 
#include <vector> 
#include <iostream> 

int main() 
{ 
    int        valG, valW; 
    std::size_t      dim; 
    std::vector<std::pair<int, int>> vec; 

    std::cout << "How many grade/weight couples? "; 
    std::cin >> dim; 

    vec.reserve(dim); 

    for (auto i = 0U ; i < dim ; ++i) 
    { 
     std::cout << "Grade #" << i << "? " << std::endl; 
     std::cin >> valG; 
     std::cout << "Weight for grade #" << i << "? " << std::endl; 
     std::cin >> valW; 

     vec.emplace_back(valG, valW); 
    } 

    for (auto const & p : vec) 
     std::cout << '|' << (p.first * p.second) << '|'; 

    std::cout << std::endl; 

    return 0; 
} 
関連する問題