c++ - 如何取输入到 C++ 程序中的值的平均值?

标签 c++ sum average

这是我的。我在代码中有注释,我在底部遇到了麻烦。我正在尝试查找用户输入的成绩的总平均值。用户在底部附近输入了四个不同的等级。它们占成绩的 10%。

程序占 30%,练习占 10%。期末考试占30%。请给我建议该怎么做。不要告诉我代码。我想在寻求额外帮助之前尝试自己弄清楚代码,以便我了解这一点。我只想就下一步要采取的步骤提出建议。谢谢。

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

float average(const vector<float> &values);

int main()
{
    cout << "Kaitlin Stevers" << endl;
    cout << "Program 11" << endl;
    cout << "December 8th 2016" << endl;
    cout << endl;
    cout << endl;

    vector<float> exerciseGradesVector;
    float exerciseGrades;

    cout << "Enter the grades for the Exercises. Type 1000 when you are finished." << endl;
    while ( std::cin >> exerciseGrades && exerciseGrades != 1000 )
    {
        exerciseGradesVector.push_back (exerciseGrades);
    }
    cout << endl;

    cout << "You entered " << exerciseGradesVector.size() << " grades." << endl;
    cout << endl;
    cout << "The Exercise grades you entered are : ";
    for(int i=0; i < exerciseGradesVector.size(); i++)
    {
        cout << exerciseGradesVector[i] << "  ";
    }
    cout << endl;
    float averageExerciseGrades = average( exerciseGradesVector );
    cout << "The average of the Exercise grades is: " << averageExerciseGrades << endl;
    cout << endl;   

    vector<float> programGradesVector;
    float programGrades;
    cout << "Enter the grades for the Programss. Type 1000 when you are finished." << endl;
    while ( std::cin >> programGrades && programGrades != 1000 )
    {
        programGradesVector.push_back (programGrades);
    }
    cout << endl;
    cout << "You entered " << programGradesVector.size() << " grades." << endl;
   cout << endl;
    cout << "The Program grades you entered are : ";
    for(int i=0; i < programGradesVector.size(); i++)
    {
        cout << programGradesVector[i] << "  ";
    }
    cout << endl;
    float averageProgramGrades = average( programGradesVector );
    cout << "The average of the Program grades is: " << averageProgramGrades << endl;
    cout << endl;

    float midTermTest;
    float midTermProgram;
    float finalExamTest;
    float finalExamProgram;

    cout << "Please enter the Midterm Exam score for the multipule choice section. " << endl;
    cin >> midTermTest;
    cout << "Please enter the Midterm Exam score for the Programming section. " << endl;
    cin >> midTermProgram;
    cout << "Please enter the Final Exam score for the multipul choice section. " << endl;
    cin >> finalExamTest;
    cout << "Please enter the Final Exam score for the Programming section. " << endl;
    cin >> finalExamProgram;
///////I need to find the average off ALL the grades.. All the 4 inputs above and the two vectors need to be in the average.
///////I am not sure if I need to read the 3 numbers before the last one into a vector because each is worth 10 percent and then do the next step or what. Would that mess it up? Advice please?  
    vector<float> allGradesVector;
    float totalaverageGrade = average( allGradesVector ); //This is obviously not finished. Just ignore it..

   }


float average( const vector<float> &values )
{
    float sum = 0.0;

    for ( size_t i = 0; i < values.size(); i++ ) sum += values[i];

    return values.size() == 0 ? sum : sum / values.size();
}

最佳答案

我建议像这样重写循环

do
{
    cin >> exerciseGrades;
    exerciseGradesVector.push_back (exerciseGrades);
}
while (exerciseGrades != 1000);

以下方式

while ( std::cin >> exerciseGrades && exerciseGrades != 1000 )
{
    exerciseGradesVector.push_back (exerciseGrades);
}

在这种情况下,不需要使用像 dropOne 这样的奇怪变量。

求平均值很容易。该函数可以如下所示

float average( const std::vector<float> &values )
{
    float sum = 0.0f;

    for ( float x : values ) sum += x;

    return values.size() == 0 ? sum : sum / values.size();
}

或者如果编译器不支持基于范围的for循环你可以这样写

float average( const std::vector<float> &values )
{
    float sum = 0.0f;

    for ( size_t i = 0; i < values.size(); i++ ) sum += values[i];

    return values.size() == 0 ? sum : sum / values.size();
}

要调用您可以在 main 中编写的函数

float averageExerciseGrades = average( exerciseGradesVector );

函数必须在使用之前声明,例如在 main 之前声明

float average( const std::vector<float> &values );

尽管它可能在 main 之后定义。

关于c++ - 如何取输入到 C++ 程序中的值的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41051955/

相关文章:

c++ - 如何使 QT 小部件从其父小部件更新修改后的属性?

c++ - 从 QT 资源加载 openCV Haar Cascade

mysql - AVG() 函数显示所有员工的平均值,而不是每个城镇每个商店的员工平均值

SQL:带有 NULL 值的 AVG

ios - 从 iOS 相机获取亮度

c++ - 在多行文本编辑控件中自动显示垂直滚动条

c++ - 为什么CUDA不会导致C++代码加速?

mysql - 从行中的金额中获取总数 MySQL PHP

sql - 尝试对 SQL Server 中的两列求和导致错误消息

c++ - 计算 n 个整数之和的递归函数 C++