Average array

Let me preface this, im a noob in an intro level c++ class.

Here is the problem


Create a 2-D double array with 5 rows and 4 columns.
Each row should hold a student’s three test grades (make them up) and has a place (set to zero) for the average of the three tests
Print the array in row – column form
Use a loop to calculate each student’s average.
Print the array.

Now I've gotten to everything accept trying to calculate the averages from the array. This is what stumps me.

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
#include <iostream>
using namespace std;

int main()
{

	int ar[5][4] =
	{
		{80,77,69,0},
		{100,95,90,0},
		{60,54,72,0},
		{42,80,67,0},
		{82,90,87,0}
	};

	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			cout << ar[i][j] << "\t";
		}
		cout << endl;
	}

	cin.get();
	return 0; 
}
Hello nickmcp11,

Have a think over this:
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
#include <iostream>

using namespace std;

int main()
{
    constexpr int MAXROW{ 5 }, MAXCOL{ 4 };

    int ar[MAXROW][MAXCOL] =
    {
        { 80, 77, 69, 0 },
        { 100, 95, 90, 0 },
        { 60, 54, 72, 0 },
        { 42, 80, 67, 0 },
        { 82, 90, 87, 0 }
    };

    for (int row = 0; row < MAXROW; row++)
    {
        for (int col = 0; col < 4; col++)
        {
            cout << ar[row][col] << "\t";
        }

        cout << '\n';
    }

    for (int row = 0; row < MAXROW; row++)
    {
        for (int col = 0; col < MAXCOL - 1; col++)
        {
            // <--- What would you do here to sum the 3 scores?
            // <--- Then could you do this in the 1st for loop?
        }

        cout << '\n';
    }

    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}


Andy
Im sorry but like how am i supposed to pull the first 3 from each of the array rows. I'm confused
nickmcp11,
Like Andy said, use nested for loops.

1. The outer loop should take the first row,
2. Then the inner loop should add up the first three elements in it,
3. Divide the total by three,
4. And store the average in the fourth element.

Then you can use a second for loop to display the array.

You can use a range-based for loop (introduced in C++11) to display them; it's shorter and simpler.

e.g.
1
2
3
4
5
6
7
8
9
// i is the counter variable
// ar is the name of the array or vector
// WARNING: You cannot use i to do something like 
// cout << ar[i];
// It WILL NOT WORK!!!
for (auto i : ar)
{
     std::cout << i << " ";
}


Have a good one,
max
still a bit loss on how i can incorporate this
Hello nickmcp11,

You already have the for loops to print the entire array. The second for loops that I showed you was to help you think about it.

After working on the program I managed to put everything in the first set of for loops.

Using an (if) statement to sum what is needed, the 1st 3, and an (else if) to put the average in the last column.

You will have to move the "cout" to the last line of the inner for loop.

I get this output:

      GRADES      Average
-----------------------
   80   77   69   75
  100   95   90   95
   60   54   72   62
   42   80   67   63
   82   90   87   86


I added the heading for fun.

Be careful using the (\t) for spacing. Sometimes it does not work well. It does work when the (\t)s are at the beginning of the string.

Using "std::setw" from the "<iomanip>" header file works much better.

Andy
Hey andy,

what im still not understanding is how to call the variables in the 2nd for loop. What you got was exactly what I need. any guidance on how you were able to call it
Hello nickmcp11,

The array has 4 columns. The first 3 hold the grades, ar[0] - ar[2], whether you use 1 set of for loops or 2 different sets does not matter. You need to total the first 3 elements of the array. I used a variable called "sum" for this and once you get your total divide it by 3 and store it in the 4th element, arr[3].

I managed this in 1 set of for loops with an if/else if, but you could use a different set of for loops.

I do not care what you come up with even if it is wrong or does not work. At least then I can see what you have tried and can work from there.

Andy
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
#include <iostream>
using namespace std;

int main()
{
    double ar[5][4] // <-- it's specified as a 2D double array.
    {
        {80,77,69,0},
        {100,95,90,0},
        {60,54,72,0},
        {42,80,67,0},
        {82,90,87,0}
    };

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << ar[i][j] << '\t';
            ar[i][3] += ar[i][j]/3.0; // calculate the average as you go
        }
        cout << ar[i][3] << '\n';
    }

    cin.get();
    return 0;
}
nickmcp11,
To call elements in second for loop (in Andy's example):
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
for (int row = 0; row < MAXROW; row++)
{
     double total {}; // for adding up the three values
     
     for (int col = 0; col < MAXCOL - 1; col++)
     {
          // ar[row][col] is the element in the array that the current loop iteration is on.
          total = ar[row][col] + total;
     }
     
     // ar[row][3] is the last element in the row that the loop is currently on.
     ar[row][3] = total / 3;
     
     cout << '\n';
}
// then whatever code you want to use to display the values! 

Warning: this code is untested. I did not have time to test it, sorry, but I did a quick visual debug and it should work.

If it doesn't, inform me and I can test it later and get back to you.
Good luck!
max

Note: you can copy and paste this in place of Andy's second nested for loop (starting on line 27).
Last edited on
I think you need to read the comments in againtry's code.

Even if you calculate the average as a double, you aren't going to be putting it in an int array.
(You could also RTQ.)
Last edited on
what I'm still not understanding is how to call the variables in the 2nd for loop.

Look at it this way, you could compute the average of each row directly:
1
2
3
4
        for (int row = 0; row < 5; row++)
        {
            ar[row][3] = (ar[row][0] + ar[row][1] + ar[row][2]) / 3;
        }

That works, but it's sort of ugly. After all, what if you had 4 tests instead of 3? What if you had 10?:
 
            ar[row][10] = (ar[row][0] + ar[row][1] + ar[row][2] + ar[row][3] + ar[row][4] + ar[row][5] + ar[row][6] + ar[row][7] + ar[row][8] + ar[row][9]) / 10;  // barf 

It makes sense to use a second, inner loop to add up the values and then compute the average:
1
2
3
4
5
int sum = 0;  // The sum of the tests.
for (int col = 0; col < 3; ++col) {
    sum += ar[row][col];   // add each test to the sum
}
ar[row][3] = sum / 3;  // compute and store the average 


Topic archived. No new replies allowed.