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.
You are right, a 2D data structure would be best suited for a problem like this, but since you are limited to 1D arrays, what you are doing works just fine.
However, It makes more sense to me to iterate over the individual tests, and get everyone's score for the same test before moving to the next one because that is how tests are usually graded.
In addition to that, passing your student arrays into the function is not necessary. Since you are only dealing with one value (the test score) you can simply pass in the name of the student and the test number and return the score for that student and test into the proper array.
string studentNames[NUM_STUDENTS] = {"Bob", "Sally", "Rick", "Tim", "Stacy"};
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_TESTS; i++) {
studentOne[i] = getScore(i, studentNames[0]);
studentTwo[i] = getScore(i, studentNames[1]);
studentThree[i] = getScore(i, studentNames[2]);
studentFour[i] = getScore(i, studentNames[3]);
studentFive[i] = getScore(i, studentNames[4]);
}
With getScore being:
double getScore(int test, string name) {
int score;
cout << name << "'s score for test #" << test+1<< ": ";
cin >> score;
return score;
}
I would make use of pre-processor macros in this instance. You can use pre-processor pasting / concatenation to achieve the desired result.
// change naming of arrays to use numbers
double student1[NUM_TESTS];
// etc.
...
// Define a macro that pastes a value onto 'student'
#define student(num) student##num
...
// Call the macro inside the for loop
for (int i = 0; i < NUM_STUDENTS; i++)
{
getStudent(student(i), i);
}
http://www.cprogramming.com/reference/preprocessor/token-pasting-operator.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With