C/Arrays and functions
Expert: Narendra - 6/1/2004
QuestionI found that you set max=0 but I get the same answers before. Max value=31073 and row 10 column 20. I don't know what is wrong with this thing.
Followup To
Question -
------------------------
No I am still having problems. I still get the answer 31073 and when I added the row/column it tells me row 10 column 20. Which isn't right. Thanks for trying.
Followup To
Question -
Hi, I wrote a function that is suppose to find the maximum value in a 2 dimensional array. But, something is wrong cause it is displaying a value of 31073 when the highest number I put in it is 100. What am I doing wrong? Also, how can I get it to display the row and column of the maximum value. The book I am reading doesn't clearly explain this. This is what I have:
#include <stdio.h>
void findmax (int [][]);
int main()
{
int nums [10][20]={
{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,47,48,49,50,51,52,53,54,55,56,57,58,59,60},
{61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80},
{81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100},
{1,99,89,79,69,59,49,39,29,19,8,15,26,14,13,19,55,48,32,85},
{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},
{20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1},
{60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41},
{50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31} };
findmax(nums);
return 0;
}
void findmax (int val [10][20])
{
int a,b,max;
for (a=0;a<10;++a)
for(b=0;b<20;++b)
if (val[a][b]>max)
max=val[a][b];
printf("The maximum array value is %d.\n",max);
return;
}
Answer -
When I executed the code, I got the right answer - 100.
I think there is no problem in the code.
Check, if you are compiling the correct code and executing the correct exe?
I have modified the code for including the row no and col no of the max value. Compile and execute.
Let me know if you still face problems?
-ssnkumar
#include <stdio.h>
void findmax (int [][]);
int main()
{
int nums [10][20]={
{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,47,48,49,50,51,52,53,54,55,56,57,58,59,60},
{61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80},
{81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100},
{1,99,89,79,69,59,49,39,29,19,8,15,26,14,13,19,55,48,32,85},
{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},
{20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1},
{60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41},
{50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31} };
findmax(nums);
return 0;
}
void findmax (int val [10][20])
{
int a,b,max, colmax, rowmax;
colmax = rowmax = 0;
for (a=0;a<10;++a)
{
for(b=0;b<20;++b)
{
if (val[a][b]>max)
{
max=val[a][b];
rowmax = a;
colmax = b;
}
}
}
printf("The maximum array value is arr[%d][%d] = %d.\n", rowmax, colmax, max);
return;
}
Answer -
OK, I got the problem (a small bug!)
Try this code now and let me know if it works:
#include <stdio.h>
void findmax (int [][]);
int main()
{
int nums [10][20]={
{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,47,48,49,50,51,52,53,54,55,56,57,58,59,60},
{61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80},
{81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100},
{1,99,89,79,69,59,49,39,29,19,8,15,26,14,13,19,55,48,32,85},
{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},
{20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1},
{60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41},
{50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31} };
findmax(nums);
return 0;
}
void findmax (int val[10][20])
{
int a,b,max = 0, colmax, rowmax;
colmax = rowmax = 0;
for (a=0;a<10;a++)
{
for(b=0;b<20;b++)
{
if (val[a][b]>max)
{
max=val[a][b];
rowmax = a;
colmax = b;
}
}
}
printf("The maximum array value is arr[%d][%d] = %d.\n", rowmax, colmax, max);
return;
}
I am curious to know the result. So, please give the feedback.
Hope you can find what I have changed and what was the bug!
-ssnkumar
AnswerI am really surprised now!
Previously "max" was not initialized.
Now, it is initialized and there is no other problem in the code.
BTW, which is the compiler and OS you are using?
I tried it both on Solaris and Linux and it is giving the correct value:
The maximum array value is arr[4][19] = 100.
I don't have the compilers on Windows and hence didn't give a try there!
-ssnkumar