我有一个程序,我输入数字来制作矩阵。然后它将每行中的数字相加并打印总和。但是我需要打印最大额的行。有人可以帮帮我吗?谢谢。 http://onlinemovies.pw
以下是代码:
#include <stdio.h>
int main (void)
{
static int array [10][10];
int i, j, m, n, sum = 0;
printf ("Enter the order of the matrix\n");
scanf ("%d %d", &m, &n);
printf ("Enter the co-efficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf ("%d", &array [i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf ("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
}
答案 0 :(得分:1)
您需要声明一个变量并使用最小值初始化它,如果这些值始终为正,则可以将其初始化为0:
int max = 0;
如果没有,您可以使用INT_MIN
<limits.h>
#include <limits.h>
...
int max = INT_MIN;
然后在循环中使用它:
int max = INT_MIN, imax = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf ("Sum of the %d row is = %d\n", i, sum);
if (sum > max) {
max = sum;
imax = i;
}
sum = 0;
}
printf ("Max row (%d) is = %d\n", imax, max);