我最近了解了二维数组。问题在于AverageScores功能。我正在尝试计算每个学生的平均值,然后将计算出的值存储到不同的数组中然后打印出来。我遇到了麻烦但我明白我需要对这一行进行求和并计算平均值,打印,然后转移到下一个学生身上。
编辑:当我运行程序时,程序根本不计算平均值,而是打印测试次数和负值不正确值。 恩。如果有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;
}
}
}
答案 0 :(得分: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