Arrays.

Hi everyone! I was wondering is someone was able to assist me with this question. I'm trying to find the sum of each row and find the largest sum. This question is part of 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  srand(time(0));
        int aray [5][10];
               int sum=0;
                int average=0; 
				int largestsum=0;
		cout<<"original"<<endl;
        for (int row=0; row<5; row++){
               for(int col=0; col<10; col++){
                   aray[row][col]=rand()%100+1;         
			   }
		}
		for (int row=0; row<5; row++){
            for(int col=0; col<10; col++)
			    cout<<aray[row][col]<<"\t";
                          cout << endl;			   
			   }
		for (int row=0; row<5; row++){
               for(int col=0; col<10; col++){
			sum=sum+aray[row][col];
			
			   }
		}
			cout<<"the sum is="<<sum<<"\t";
			cout<<endl;
								   
		for (int row=0; row<5; row++){
               for(int col=0; col<10; col++){
			average=sum/50;
			
			   }
		}
			cout<<"the average is="<<average<<"\t";
			cout<<endl;
	   
		cout<<"second version"<<endl;
		 for (int row=4; row>=0; row--){
               for(int col=9; col>=0; col--)
				   cout<<aray[row][col]<<"\t";
			   cout<<endl;
		 }
				    for (int row=4; row>=0; row--){
               for(int col=9; col>=0; col--)
                       cout<<"   "<<"\t";
							cout<<endl;
					}

I would really appreciate your help!
try this:

int tmpSum = 0;
int largeSum = 0;
int mtrx[num of rows][num of colums];
int sumArr[num of colum];

////here we run on each colum of the matrix and thus sum each row, and put the resault in a new array
for(int i = 0; i < (num of rows) ;i++ )
{
for(int j = 0; j < (num of colums); j++)
tmpSum = tmpSum + mtrx[i][j];
sumArr[i] = tmpSum;
}
///////

/////here we run on the array which has the sum of each row of the matrix and searching the largest number. (you cab also do a bouble sort to put the largest number at the end)
for(int i = 0; i < (num of colum-1); i++ )
{
if(sumArr[i] > sumArr[i+1])
largeSum = sumArr[i];
}


///example for buoble sort
int tmp = 0;
for(int i = 0;i < (num of colum -1); i++ )
{
if(sumArr[i]>sumArr[i+1]
{
sumArr[i+1] = tmp;
sumArr[i+1] = sumArr[i];
sumArr[i] = tmp;
}
}

i hope i helped you! good luck!
Thanks so much, that really helped a lot!
Topic archived. No new replies allowed.