Reputation: 47
To the best of my knowledge what I have entered should be working properly, but I am new to C and am unable to come up with a reason as to why it isn't. Each function works fine separately but for some reason whilst together they produce weird errors.
#include <stdio.h>
#include<conio.h>
main()
{
int m, n, c, d, sum, matrix[10][10], maximum;
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix\n");
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}
maximum = matrix[0][0];
//Maximum
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
if ( matrix[c][d] > maximum )
maximum = matrix[c][d];
}
}
//Sum
for (c = 0; c < m; ++c)
{
for (d = 0; d < n; ++d)
{
sum = sum + matrix[c][d] ;
}
printf("Sum of the %d row is = %d\n", c, sum);
sum = 0;
}
printf("Maximum element in matrix is %d\n", maximum);
getch();
return 0;
}
Upvotes: 1
Views: 67
Reputation: 836
You are operating addition operation on int sum before initializing it to 0. Initialize sum to 0 while declaring and that should fix your issue.
Upvotes: 1