2017-01-19 24 views
-1

私のコードはコンパイルされていて、私が望むように動作します。 これは基本的に黒板で、割り当ての数と割り当てあたりの重量をパーセンテージで入力します。次に、1人または複数の生徒の平均成績を計算するかどうかを選択できます。加重平均を正しく計算する方法C++

ここで問題になるのは、エラーチェックをしたかったので、合計の合計数を取得しようとしていましたが、印刷すると、合計...私は100を取得しようとしている誰かが私がどこを台無しにしたことを教えてくれる?

愚かな質問を許してください...これは私のプログラムをC++で書くのは初めてのことなので、これについては何も分かりません---> [i]

#include <iostream> 
using namespace std ; 

int main(int argc, char **argv) 
{ 
    const int maxSize = 100 ; 
    float weight [ maxSize ] ; 
    float grades [ maxSize ] ; 

    int numAssignments ; 

    cout << "    WELCOME TO BLACKBOARD ! " << endl ; 
    cout << " (This program was created for the calculation grades) " << endl ; 
    cout << " " << endl ; 
    cout << " To start, enter number of assignments: " ; 
    cin >> numAssignments ; 

    cout << " thanks! " ; 
    cout << " Now, enter weight of assignments " << endl ; 
    out << " (total sum of the weights should be 100) " << endl ; 

    float sum = 0.0 ; 
    int i = 0 ; 
    for (int i = 0; i < numAssignments; i++) 
    { 
     cout << " Assignment " << i + 1 << " : " ; 

     cin >> weight [i] ; 
     weight[i] /= 100 ; 

     sum += weight[i] * 100 ; 

    } 
cout<<"sum is: "<< sum<<endl; 

EDITエントリ:

私は総平均値を得ることが間違って何をやっていた考え出しました。このブロックで :

float sum = 0.0 ; 
    int i = 0 ; 
    for (int i = 0; i < numAssignments; i++) 
    { 
     cout << " Assignment " << i + 1 << " : " ; 

     cin >> weight [i] ; 
     weight[i] /= 100 ; 

     sum += weight[i] * 100 ; 

    } 
cout<<"sum is: "<< sum<<endl; 

私はPERCと呼ばれる別の変数を追加しました:

float sum = 0.0 ; 
    int i = 0 ; 
    for (int i = 0; i < numAssignments; i++) 
    { 
     cout << " Assignment " << i + 1 << " : " ; 

     cin >> weight [i] ; 

     float perc ; 
     perc = weight[i] /= 100 ; 

     sum += perc * 100 ; 

    } 
cout<<"sum is: "<< sum<<endl; 

それは私が望んでいた結果を得るために安価な方法のようなものだが、ベクターでの提案はどのように理解するために実際には非常に有用であったがあまりにもそれを回避する。

+1

たら...私は学生の学年あたりの総重量のための下部にダウン行われ、単純な平均値を得ることができないことができ数学で吸うという事実を除いて...コンパイルし、私が欲しいもの、ほとんどありませんあなたは配列とその使い方を知らないので、私はあなたに[良い初心者の本を見つける]ことをお勧めします(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) )を読んでください。 –

+0

http://www.cplusplus.com/doc/tutorial/arrays/ – jamek

+1

コンパイルエラーを修正しました。[再現できません](http://ideone.com/w9tRaF)。 (コードをコピーして貼り付けるのではなく、実際のコードに '='、さらには '+ +'があると思われます) – molbdnilo

答えて

0

ここに私の固定コードのベクトルによる更新があります。それは

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

    int main(int argc, char **argv) 
    { 
     const int maxSize = 100 ; 

     vector <float> weight (maxSize) ; 


     cout << "    WELCOME TO BLACKBOARD ! " << endl ; 
     cout << " " << endl ; 
     cout << " (This program was created to calculate grades) " << endl ; 
     cout << " " << endl ; 

     // Here is where the user inputs the number of assignments 
     cout << " To start, enter number of assignments: " ; 
     int numAssignments ; 
     cin >> numAssignments ; 

     cout << " " << endl ; 
     cout << " Now, enter weight (in percent) of assignments: " << endl ; 
     cout << " " << endl ; 

     // Loop to input the weighted value per assignment 
     float sum = 0.0 ; 
     float num ; 
     for (int i = 0; i < numAssignments; i++) 
     { 
      cout << " Assignment " << i + 1 << " : " ; 

      cin >> num ; 
      weight.push_back(num/100) ; 

      sum += num ; 



     } 
     cout << " Total Weight is: "<< sum << endl ; 

     vector <float> grades (maxSize) ; 

     // Input number of students 

     cout << endl ; 
     cout << " Please, enter number of students graded : " ; 
     int numStud ; 
     cin >> numStud ; 

     // Loop for the calculation of average grade per student 


     for (int j = 0 ; j < numStud ; j++) 
     { 
      cout << endl ; 
      cout << " Enter grades for student " << j + 1 << ": " << endl ; 
      float numGrade ; 
     for (int k = 0 ; k < numAssignments ; k++) 
      { 
       cout << " Grade for assignment " << k + 1 << ": " ; 

       cin >> numGrade ; 

       grades.at(numGrade) ; 
      } 

      float totalWeight = 0.0 ; 
      totalWeight += num * numGrade ; 

      char letterGrade ;  
      if (totalWeight >= 90) 
      letterGrade = 'A' ; 
      else if (totalWeight >= 80) 
      letterGrade = 'B' ; 
      else if (totalWeight >= 70) 
      letterGrade = 'C' ; 
      else if (totalWeight >= 60) 
      letterGrade = 'D' ; 
      else 
      letterGrade = 'F' ; 

      cout << " weight average is : " << totalWeight << endl ;  
      cout << " Letter grade is : " << letterGrade << endl ; 
     } 

     return 0; 
    } 
関連する問題