Working on a 2D Array and need help!

This is my prompt. How I have to make it look.

There are 5 students in class. They took 3 tests. The user enters the scores (integers) of all the tests for each student and the scores are stored into a 2-dimensional array. It will then output their scores along with the labels and the average for each test and each student.

Create the following functions in addition to the main:
A function that will fill the 2-dimensional array with the scores entered from the keyboard.
A function that will display the table shown in the sample run below (all the scores along with the labels and the average for each test and each student).

You must use const.

Sample Run

Enter scores for Test #1
Student #1: 45
Student #2: 78
Student #3: 86
Student #4: 78
Student #5: 67
Enter scores for Test #2
Student #1: 67
Student #2: 88
Student #3: 96
Student #4: 77
Student #5: 57
Enter scores for Test #3
Student #1: 78
Student #2: 56
Student #3: 78
Student #4: 67
Student #5: 57

Test # Stu 1 Stu 2 Stu 3 Stu 4 Stu 5 Average
1 45 78 86 78 67 70.8
2 67 88 96 77 57 77.0
3 78 56 78 67 57 67.2
Average 63.3 74.0 86.7 74.0 60.3

All the numbers should be aligned under the Stu 1, 2, 3 and Average. You need to use a nested for loop to show all these averages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
This is what I have so far...

#include <iostream>
#include <iomanip>
using namespace std;

const int SIZE_STU = 5;
const int SIZE_TEST = 3;

void fillArray(int scores[SIZE_STU][ SIZE_TEST]);
void printArray(const int scores[SIZE_STU][ SIZE_TEST]);

int main()
{
  int scores[SIZE_STU][ SIZE_TEST];

  fillArray(scores); //just the array name without [][]                                                                                              
  printArray(scores);

  return 0;
}

void fillArray(int scores[SIZE_STU][SIZE_TEST])
{
  for(int test = 0; test < SIZE_TEST; test++)
    {
       cout << "Enter the score for test #" << test + 1 <<endl;

    for(int stu = 0; stu < SIZE_STU; stu++)
      {
        cout<< "\tStudent # " << stu + 1<<" : ";
        cin >> scores[stu][test];
      }
    }
}
void printArray(const int scores[SIZE_STU][SIZE_TEST])
{
  int total;
  int total2;

  cout<<endl<<setw(10)<< "Stu 1"<<setw(10)<< "Stu 2"<<setw(10)<<"Stu 3" <<setw(10)<<"Stu 4"<<setw(10)<<"Stu 5"<< setw(10) << "Average"<<endl;
for(int test = 0; test < SIZE_TEST; test++)
    {
     total = 0;

     cout << endl << setw(6) << "Test#" << test + 1 << " ";

      for(int stu = 0; stu < SIZE_STU; stu++)
        {
          cout << setw(6) << scores[stu][test];
          total += scores[stu][test];
        }

      cout<<(float)total/SIZE_STU << endl;
      cout<<endl<<setw(6)<<"Average" <<total<< " ";
cout<<endl<<setw(6)<<"Average" <<total<< " ";
    }

  for(int stu = 0; stu < SIZE_STU; stu++)
    {
      total2=0;
      cout<< setw(6) << "Average" << total2 + 1 << " ";

      for(int test = 0; test < SIZE_TEST; test++)
        {
          cout<<setw(6)<<scores[stu][test];
          total2+= scores[stu][test];
        }

      cout<<(float)total2/SIZE_TEST<<endl;
}
}
Last edited on
Help with...? In other words what is your question? Where are you stuck? What is your program doing? What should it be doing instead? Help us help you.
Topic archived. No new replies allowed.