# Thread: How do I print the row and column number in an array????

1. ## How do I print the row and column number in an array????

I'm looking to modify my function for a 10 x 20 array that finds the max value in the array. I'm trying to print the row and column number of the element that is the max value but I always get 10 and 20 no matter where the max value is. Can someone help me figure out why or point me to another thread. I searched but couldn't find anything pertaining to this topic. I modified it for a smaller array until I get it figured out.

Here's the code:

#include <stdio.h>
void findmax(int[2][20];
int main()
{
int numbers[2][20] = {1,4,5,8,3,2,15,227,45,87,6,92,23,21,45,85,95,187, 34,67,56,4,2,7,0,9,6,78,94,300,486,867,917,45,1,66 6,911,56,41,35};

findmax(numbers);
return 0;
}

void findmax(int num[2][20])
{
int i,j,max;
for(i=0;i<2;i++)
for (j=0;j<20;j++)
if (num[i][j] > max)
max = num [i][j];
printf("\nThe maximum array value is %d\n",max);
return;
}

2. Use code tags.

All you need to do is have ti store the row and column of the maximum value when you find it.
Code:
```max = num [i][j];
maxrow = i;
maxcol = j;```

3. all you'd have to do is after your first printf statement make another that uses i and j.
Code:
`printf("Max found in array element [%d][%d]",i+1,j+1);`

4. Originally posted by Draco
all you'd have to do is after your first printf statement make another that uses i and j.
Code:
`printf("Max found in array element [%d][%d]",i+1,j+1);`
That wouldn't work, because i and j are out of scope by then.

5. I don't see why he couldn't put that statement in the findmax function

6. Here's the code indented properly:
Code:
```#include <stdio.h>
void findmax(int[2][20];
int main()
{
int numbers[2][20] = {1,4,5,8,3,2,15,227,45,87,6,92,23,21,45,85,95,187,
34,67,56,4,2,7,0,9,6,78,94,300,486,867,917,45,1,66
6,911,56,41,35};

findmax(numbers);
return 0;
}

void findmax(int num[2][20])
{
int i,j,max;
for(i=0;i<2;i++)
for (j=0;j<20;j++)
if (num[i][j] > max)
max = num [i][j];
printf("\nThe maximum array value is %d\n",max);
return;
}```
Now do you see why it wouldn't work?

7. yeah. It's confusing when they don't put in loop braces....I didn't even know you could do that.

8. If you don't use braces, it will only include the next line of code in the loop/if statement/whatever.

9. I know that..but I thought for loops needed them.

10. Nope.

11. Thanks for the help Xsquared.