如何恰当地加权加权平均C ++

时间:2017-01-19 09:38:08

标签: 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;  

编辑条目:

我弄清楚我得到的总平均值是错误的。 在这个块中:

    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 个答案:

答案 0 :(得分:0)

这里是我的固定代码与向量的更新。它编译并且主要完成我想要的...除了我在数学上吮吸的事实可以在底部得到一个简单的平均值,以达到每个学生的总分数... ... / p>

    #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;
    }