I am writing a program that allows a user to enter four test scores for each of five students. The prompt says to use five 1D arrays to do this, one for each student storing four test scores. I want to write the code in a way that I iterate through all five arrays sending each one to the function in one loop, but I can't figure out if that's even possible. The reason I want to do this is because I want the user to just be able to change the global constant NUM_STUDENTS to however many students they have and for the program to work. (I know this would be so much easier using a 2D array but I don't think we are supposed to). This is the only thing I've come up with, but this means that if someone had, say 10 students, they would have to adjust the code instead of just changing the global constant.
const int NUM_STUDENTS = 5;
const int NUM_TESTS = 4;
void getStudent(string);
void getScores(double [], int);
int main()
{
string students[NUM_STUDENTS];
double studentOne[NUM_TESTS];
double studentTwo[NUM_TESTS];
double studentThree[NUM_TESTS];
double studentFour[NUM_TESTS];
double studentFive[NUM_TESTS];
for(int i = 0; i < NUM_STUDENTS; i++)
{
//Get student: function to get students name
getStudent( students[i] );
//Get test scores: function to get students four test scores
if( i == 0)
{
getScores( studentOne, i );
}
else if( i == 1 )
{
getScores( studentTwo, i );
}
else if( i == 2 )
{
getScores( studentThree, i );
}
else if( i == 3 )
{
getScores( studentFour, i );
}
else if( i == 4 )
{
getScores( studentFive, i );
}
}
return 0;
}
void getStudent(string students)
{
cout << "Enter the student's name: ";
getline(cin, students);
cout << students;
cout << endl;
}
void getScores(double testScores[], int student)
{
for(int i = 0; i < NUM_TESTS; i++)
{
cout<< "Enter test scores for student " << (student + 1) << ": ";
cin >> testScores[i];
}
for(int i = 0; i < NUM_TESTS; i++)
{
cout << testScores[i] << endl;
}
}
This is what I have so far, which seems to work for the most part, however it's not really how I want to do it.
getScoresas well as your arrays, and simply just prefer to have a minimal reproducible example (roughly what you have, but with amainand declaration of those functions)switch?