2017-11-16 4 views
-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; 
     } 
    } 
} 
+1

'accumilator + =スコア[numberOfStudents] [numberOfTests];'ループインデックス変数を使用し、ループの最大値ではありません。 – user4581301

+1

最小、完全、および検証可能な例の作成を検討してください。 https://stackoverflow.com/help/mcve –

答えて

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 
関連する問題