C++ 如何正确计算加权平均值C++;

C++ 如何正确计算加权平均值C++;,c++,C++,因此,我的代码编译和工作方式几乎与我所希望的一样。 这基本上是一个黑板,用户输入作业数量和每个作业的权重(百分比)。然后,您可以选择是否要计算一个或多个学生的平均成绩 我的问题是,我想做一些错误检查,所以我一直试图得到权重总和的总数,但当我打印它时,它会给我最后一个权重,而不是总和。。。我想考100分。谁能告诉我我在哪里搞砸了 请原谅这个愚蠢的问题。。。这是我第一次用C++编写程序,所以我对这个-->[i]做的不太了解。 #include <iostream> using names

因此,我的代码编译和工作方式几乎与我所希望的一样。 这基本上是一个黑板,用户输入作业数量和每个作业的权重(百分比)。然后,您可以选择是否要计算一个或多个学生的平均成绩

我的问题是,我想做一些错误检查,所以我一直试图得到权重总和的总数,但当我打印它时,它会给我最后一个权重,而不是总和。。。我想考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;  
#包括
使用名称空间std;
int main(int argc,字符**argv)
{
常数int maxSize=100;
浮动重量[最大尺寸];
浮动等级[最大尺寸];
国际货币基金组织;

cout这是我的向量固定代码的更新。它编译并做我想做的事情…除了我数学不好,无法在底部得到每个学生分数的总权重的简单平均值

    #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;
    }
#包括
#包括
#包括
使用名称空间std;
int main(int argc,字符**argv)
{
常数int maxSize=100;
向量权重(maxSize);

如果你不知道数组以及如何使用它们,那么我建议你阅读。修复编译错误,(如果你重新键入代码而不是复制粘贴它,我怀疑你真正的代码有
=
,甚至
=+
,其中该代码有
+=
)你的代码对我来说还行:(输出:“sum is:100”)关于代码的两个注释:将权重更改为
std::vector weight;
,然后使循环成为
for(int i=0;i    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;  
    #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;
    }