Sum up values of 2-dimensional array in 1-dimensional one

closed account (DzwpfSEw)
I want to write a program allows the teacher of 3 students to input 3 grades for each student in a 2-dimensional array, then each 3 grades are to be sum up and be put in 1-dimensional array called "sum", then the average "avg" is the average of each 3 grades.

The trace of this program should be as follow:

st_grades =
20 30 40
40 50 60
60 70 80

sum =
90
150
210

avg =
90/3 = 30
150/3 = 50
210/3 = 70

This is my code:

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;

void main()
{
	float st_grades[2][2], sum[2], avg[2];
	for (int i=0; i<3; i++)
	{
		for (int j=0; j<3; j++)
			cin>>st_grades[i][j];
	}
	for (int i=0; i<3; i++)
	{
		for (int j=0; j<3; j++)
		{
			sum[i] += st_grades[i][j];
			avg[i] += sum[i]/3;
		}
	}
	cout<<endl;
	for (int k=0; k<3; k++)
	{
		cout<<sum[k]<<" & "<<avg[k]<<endl;
	}

	system ("pause");
}


However, the output of this program is corrupt:
e.g: sum = -1.07374e+008

So, why is this happening?!
Last edited on
while declaring, declare the size of arrays as [3] and not [2]. These shall hold 3 elements each and not 2.
closed account (DzwpfSEw)
I believe arrays start from 0, so when I declare it as [2] this means that there are 3 places in the memory [0], [1] & [2]. However, I've just declared them as [3] and the answer is still corrupt!
I believe arrays start from 0, so when I declare it as [2] this means that there are 3 places in the memory [0], [1] & [2].


No, you need to declare sum[3], so you get [0], [1] & [2]. 3 is the number of values in the array.

The average calculation should be done after the sum calculation is complete, not during it. So move line 17 outside the for loop.

Hope all goes well.
Before the j loop, initialize sum[i] and avg[i] = 0
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
#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>

int main()
{
    const unsigned int N = 3;
    unsigned int st_grades[][N] =
        {
            { 20, 30, 40 },
            { 40, 50, 60 },
            { 60, 70, 80 }    
        };
    unsigned int sum[N];
    
    std::transform( std::begin( st_grades ), std::end( st_grades ), std::begin( sum ),
        []( unsigned int ( &a )[N] ) 
        { return ( std::accumulate( std::begin( a ), std::end( a ), 0u ) ); } );
        
    std::transform( std::begin( sum ), std::end( sum ), 
        std::ostream_iterator<unsigned int>( std::cout, " " ), 
        []( unsigned int x ) { return ( x / N ); } );
        
    return 0;
}
closed account (DzwpfSEw)
Before the j loop, initialize sum[i] and avg[i] = 0


Thank you very much abhishekm71, I was asking for receiving this answer.

---

Thanks TheIdeasMan for correcting me and for this valuable reorder, I needed somebody to confirm this reorder for me.

---

vlad from moscow, I'm afraid to tell you that I'm very newbie in C++ language or even in programming in general. I haven't known these included libraries yet. Nevertheless, I appreciate your effort very much.

---

Thanks again everybody.
Last edited on
vlad from moscow, I'm afraid to tell you that I'm very newbie in C++ language or even in programming in general. I haven't known these included libraries yet. Nevertheless, I appreciate your effort very much.


The sooner you will be acquainted with such constructions the better. You can consider the code as a pseudo code that demonstrates steps of performing the assignment.:)
hey vlad! Where can i learn to use such constructions? Is there a good link that i can refer?
Maybe there is a description of standard algorithms in the forum. You can google using keywoard std::transform
Last edited on
ok thanks..
Topic archived. No new replies allowed.