【发布时间】: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