Reputation:
I am writing this program to find the sum of each row, and find the row with the highest sum. however, the sums of each row execute perfectly, but I can't be shown the highest sum.
#include<stdio.h>
int main()
{
int type[5][3]={{3,31,19},{34,2,1},{0,0,9},{3,0,6},{11,9,5}};
for(i=0;i<5;++i)
{
for(j=0;j<3;++j)
{
sum=sum+type[i][j];
if((sum=sum+type[i][j])>(sum=sum+type[i+1][j]))
{
printf("%d type is highest",i);
}
}
printf("Total sumt %d=%d\n",i+1,sum);
sum=0;
}
}
Upvotes: 0
Views: 438
Reputation: 84561
You have a number of problems. i
and j
are not declared, and you read beyond the end of type
with type[i+1][j]
. You must fix both.
To find the largest sum, you need a separate variable that holds the largest of the sums seen and then after you exit your last loop, then output the biggest sum, e.g.
(updated to include row where largest sum occurs)
#include <stdio.h>
#include <limits.h>
int main (void) {
int type[5][3] = {{3,31,19}, {34,2,1}, {0,0,9}, {3,0,6}, {11,9,5}},
biggest = INT_MIN, /* variable to hold biggest sum */
bigindex = 0; /* variable to hold row of biggest */
for (int i = 0; i < 5; i++) { /* loop over each row */
int sum = 0; /* declare/initialize sum = 0 */
for (int j = 0; j < 3; j++) { /* loop over each int */
sum += type[i][j]; /* add int to sum */
}
if (sum > biggest) { /* compare sum to biggest sum */
biggest = sum; /* update biggest if sum larger */
bigindex = i + 1; /* update row where it occurs */
}
printf ("Total sum type[%d] = %d\n",i+1, sum); /* output sum */
}
/* output largest sum and row where it occurred */
printf ("Largest sum: %d at row %d\n", biggest, bigindex);
}
Example Use/Output
$ ./bin/biggestsum
Total sum type[1] = 53
Total sum type[2] = 37
Total sum type[3] = 9
Total sum type[4] = 9
Total sum type[5] = 25
Largest sum: 53 at row 1
Let me know if you have further questions.
Upvotes: 0
Reputation: 1153
Store sum of all the 5 rows of int type[5][3]
in any array, say sum[5]
and then find the highest of them and then print their index+1
no. to show that, this row has the highest sum.
Example:
#include <stdio.h>
#define ROW 2
#define COLUMN 3
int main(void)
{
int type[ROW][COLUMN] = { {1, 2, 3}, {4, 5, 6}};
int sum[ROW] = {0, 0};
int i, j, row = 0, highest = 0;
for ( i = 0; i < ROW; ++i)
for ( j = 0; j < COLUMN; ++j)
sum[i] += type[i][j];
for ( i = 0; i < ROW; ++i)
if(highest < sum[i])
{
highest = sum[i];
row = i;
}
printf("Row %d has the highest sum value %d.\n", row, highest);
return 0;
}
Upvotes: 0
Reputation: 7726
Your program has the following errors:
if((sum=sum+type[i][j])>(sum=sum+type[i+1][j]))
will go out-of-bound of array.There you go with correct program:
#include <stdio.h>
int main(void) {
int arr[][3] = {{3, 31, 19}, {34, 2, 1}, {0, 0, 9}, {3, 0, 6}, {11, 9, 5}};
size_t len = sizeof(arr) / sizeof(arr[0]);
int sum = 0, largest = 0, index = 0;
for (int i = 0; i < len; i++) {
for (int j = 0; j < 3; j++) {
sum += arr[i][j];
if (sum > largest) {
largest = sum; // getting the largest number
index = i; // getting the index of the largest number
}
}
printf("The sum of %d is: %d\n", i + 1, sum);
sum = 0;
}
printf("Highest: %d with %d\n", largest, index);
return 0;
}
This will print the highest number alongside the index where the highest number is provided.
You'll then get something like:
The sum of 1 is: 53
The sum of 2 is: 37
The sum of 3 is: 9
The sum of 4 is: 9
The sum of 5 is: 25
Highest: 53 with 0 // 0 is the index of the array, not counting as a number
Upvotes: 1