3

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.

11
  • 1
    This isn't a bad question, and shows some effort. You should edit the question to include the declaration of getScores as well as your arrays, and simply just prefer to have a minimal reproducible example (roughly what you have, but with a main and declaration of those functions) Commented Jun 13, 2017 at 2:30
  • It's a weird requirement that forbids you from using an array of arrays to store student scores for arbitrary number of students. Are you sure? What's the actual wording of the assignment? Commented Jun 13, 2017 at 2:31
  • "Write a program that uses an array of string objects to hold the five student names, an array of five characters to hold the five students' letter grades, and five arrays of four doubles to hold each student's set of test scores" So I don't know I guess, I took that to mean I had to use five separate 1D arrays. Commented Jun 13, 2017 at 2:36
  • Use 1d array of pointers Commented Jun 13, 2017 at 2:52
  • why don't use switch? Commented Jun 13, 2017 at 3:18

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

I will definitely look into that thank you. Is there possibly a more simple way of doing it? This is just a computer science II class and we haven't covered any of that, I don't want to risk getting points off for using something we haven't learned yet (I have no idea if that would happen but just in case).
In my experience, using something you haven't learnt yet will impress the teacher. Unless explicitly specified not to use it, I would use it.
This doesn't do what you think it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.