【问题标题】:Get values from array and calculate average then store in array从数组中获取值并计算平均值然后存储在数组中
【发布时间】:2017-11-16 19:16:55
【问题描述】:

我最近了解了二维数组。问题出在 AverageScores 函数中。我正在尝试计算每个学生的平均值,然后将计算出的值存储到不同的数组中,然后打印出来。我遇到了麻烦,但我知道我需要对这行值求和并计算平均值、打印,然后转到下一个学生。

edit:当我运行程序时,程序根本不计算平均值,而是打印测试次数和负的不正确值。 前任。如果有 2 个测试和 2 个学生,程序将为第一个学生打印两次 -9.25596e+61,然后为第二个学生再打印两次。

#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;

//constant declarations
const int MAX_STUDENTS = 30; //maximum number of students
const int MAX_TESTS = 10; //maximum number of tests
                          //function prototypes
void intro();
void ReadScores(double[][MAX_TESTS], int&, int&);
void PrintScores(const double[][MAX_TESTS], int, int);
void AverageScores(const double[][MAX_TESTS],
    int,
    int,
    double[]);

int main()
{
    //variable declarations
    double scores[MAX_STUDENTS][MAX_TESTS],//array of test scores
        studentAvgs[MAX_TESTS];
    int numberOfStudents; //number of students in a class
    int numberOfTests; //number of tests written
    intro();
    //read the each student’s test scores into an array scores
    ReadScores(scores, numberOfStudents, numberOfTests);
    //print each student’s scores
    PrintScores(scores, numberOfStudents, numberOfTests);
    AverageScores(scores, numberOfStudents, numberOfTests, studentAvgs);

    _getch();
    return 0;
}


void ReadScores(double scores[][MAX_TESTS], //array of test scores
    int& numberOfStudents, //number of students read
    int& numberOfTests) //number of tests read
{
    int student; //row index used for students
    int test; //column index used for tests
              //prompt for and read the number of students and the number of tests
    cout << "Enter the number of students(up to " << MAX_STUDENTS << ") :";
    cin >> numberOfStudents;
    cout << "Enter the number of tests(up to " << MAX_TESTS << ") : ";
    cin >> numberOfTests;
    //read the test scores into the array scores
    for (student = 0; student < numberOfStudents; student++)
    {
        cout << "Enter the " << numberOfTests
            << " test scores for student# " << (student + 1) << endl;
        for (test = 0; test < numberOfTests; test++)
            cin >> scores[student][test];
    }
}

void PrintScores(const double scores[][MAX_TESTS],
    int numberOfStudents,
    int numberOfTests)
{
    int student;
    int test;
    for (student = 0; student < numberOfStudents; student++)
    {
        cout << "The test scores for student# " << (student + 1)
            << " are: " << endl;
        for (test = 0; test < numberOfTests; test++)
            cout << setw(3) << scores[student][test];
        cout << endl << endl;
    }
}

void intro() {
    cout << setw(46) << "Welcome\n\n";
}

void AverageScores(const double scores[][MAX_TESTS],
    int numberOfStudents,
    int numberOfTests,
    double studentAvgs[]){

    int student,
        test,
        accumilator = 0;

    for (student = 0; student < numberOfStudents; student++)
    {
        cout << "The average test score for student# " << (student + 1)
            << " is: " << endl;
        for (test = 0; test < numberOfTests; test++) {
            accumilator += scores[numberOfStudents][numberOfTests];
            studentAvgs[test] = accumilator / numberOfTests;
            cout << studentAvgs[numberOfTests] << endl;
        }
    }
}

【问题讨论】:

  • accumilator += scores[numberOfStudents][numberOfTests]; 使用循环索引变量,而不是循环最大值。
  • 请考虑创建一个最小、完整和可验证的示例。 stackoverflow.com/help/mcve

标签: c++ arrays multidimensional-array average


【解决方案1】:

相同但使用 STL(用于咯咯笑):

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <numeric>


int main()
{
    std::vector<std::pair<std::string, std::vector<double>>> studentScores;

    studentScores.push_back(std::make_pair(std::string("Bob"), std::vector<double>{ 1.23, 3.23, 0.55 }));
    studentScores.push_back(std::make_pair(std::string("John"), std::vector<double>{ 2.09, 2.22, 4.55, 1.28, 4.99 }));
    studentScores.push_back(std::make_pair(std::string("Mary"), std::vector<double>{ 4.11, 0.2, 0.55, 3.88}));


    for (auto studentScore : studentScores)
    {
        std::cout << studentScore.first << "'s average score is: ";
        std::cout << std::accumulate(studentScore.second.begin(), studentScore.second.end(), static_cast<double>(0)) / static_cast<double>(studentScore.second.size()) << std::endl;
    }

    return 0;
}

打印

Bob's average score is: 1.67
John's average score is: 3.026
Mary's average score is: 2.185

【讨论】:

    猜你喜欢
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多