November 16th, 2012, 03:37 PM
-
Help with multi-diemensional array
Hello,
I have a problem with the following code
int main()
{
int i,j,max;
int a[3][3]={{85,99,77},
{80,73,74},
{89,33,88}};
for (i=0;i<3;i++){
max=0;
printf (" \n");
for (j=0;j<3;j++)
if (max<a[i][j])
max=a[i][j];
printf ("%d the maximum is %i ",a[i][j],max);
}
}
I want the output to be the ALL numbers in arrays and the maximum number of each row I tried and tried but I couldn't know where is the problem,and here is the output
80 the maximum is 99
89 the maximum is 80
0 the maximum is 99
any help pls
November 16th, 2012, 03:49 PM
-
You could try posting nicely formated code like below...
Code:
#include <stdio.h>
int main()
{
int i, j, max;
int a[3][3]={{85,99,77}, {80,73,74}, {89,33,88}};
for (i= 0; i < 3; i++)
{
max=0;
printf (" \n");
for (j=0;j<3;j++)
{
if (max < a[i][j])
max = a[i][j];
printf ("%d the maximum is %i\n",a[i][j],max);
}
fputs("\n", stdout);
}
return 0;
}
The output of the above code is...
Code:
85 the maximum is 85
99 the maximum is 99
77 the maximum is 99
80 the maximum is 80
73 the maximum is 80
74 the maximum is 80
89 the maximum is 89
33 the maximum is 89
88 the maximum is 89
November 16th, 2012, 03:51 PM
-
printf ("%d the maximum is %i ",a[i][j],max);
Print i and j too :)
Code:
printf ("%d the maximum is %i (i is %d; j is %d)\n", a[i][j], max, i, j);
November 17th, 2012, 02:32 AM
-