-1
私は最近、2次元配列について学習しました。問題はAverageScores関数内にあります。私は各学生の平均を計算し、次に計算された値を別の配列に格納してからそれを印刷しようとしています。私は問題を抱えていますが、私は値の列を合計し、平均を計算し、印刷し、次の生徒に移動する必要があることを理解しています。配列から値を取得し、平均を計算して配列に格納する
私がプログラムを実行すると、プログラムは平均を計算しませんが、テストの数と負の不正な値を出力します。 ex。 2つのテストと2人の生徒がいる場合、最初の生徒は2回、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 + =スコア[numberOfStudents] [numberOfTests];'ループインデックス変数を使用し、ループの最大値ではありません。 – user4581301
最小、完全、および検証可能な例の作成を検討してください。 https://stackoverflow.com/help/mcve –