# A program to find largest number from 5 row by 5 column matrix

This is a discussion on A program to find largest number from 5 row by 5 column matrix within the C Programming forums, part of the General Programming Boards category; I made the code that stores and prints 5 row by 5 column values but I have no idea how ...

1. ## A program to find largest number from 5 row by 5 column matrix

I made the code that stores and prints 5 row by 5 column values but I have no idea how find the largest number from them.What should I do to find the largest number?If I use if-else then it would be very tedious but I think there is way out with for loop but still I can't frame the logic.
Here is the code
Code:
```#include<stdio.h>
main()
{
int lnum[5][5];
int i,j;
for(i=0;i<=4;i++)
{
printf("\n");
for(j=0;j<=4;j++)
{
printf("Enter the number");
scanf("%d",&lnum[i][j]);
}
}
for(i=0;i<=4;i++)
{
printf("\n");
for(j=0;j<=4;j++)
{
printf("%d",lnum[i][j]);
}
}
getch();
}```

2. Well create a variable called
int largestSoFar;

and mentally work your way though your print loop, imagining that you're trying to find the largest number so far, and what you might be doing with a variable called largestSoFar

3. Originally Posted by Salem
Well create a variable called
int largestSoFar;

and mentally work your way though your print loop, imagining that you're trying to find the largest number so far, and what you might be doing with a variable called largestSoFar
Salem,I am still unable to frame the logic,do I have to use strings or structures?

4. > do I have to use strings or structures?

You draw out a 5x5 grid, fill it with some numbers and then look at each one in turn to figure out which the biggest is.

Until you understand how to do it on paper, there's no point looking at the code.

Trust me, you have a "oh wow" moment coming up.

5. Hint: Think of how a mechanical counter works, to see if this sparks any ideas. Also do some research on nested loops, if you're not familiar with how they work.

6. Originally Posted by Matticus
Hint: Think of how a mechanical counter works, to see if this sparks any ideas. Also do some research on nested loops, if you're not familiar with how they work.
Thank you Salem,after an hour of trying your suggestion,I finally got it.Here is the code.
Code:
```#include<stdio.h>
main()
{
int lnum[5][5];
int i,j,lnsf;
for(i=0;i<=4;i++)
{
printf("\n");
for(j=0;j<=4;j++)
{
printf("Enter the number");
scanf("%d",&lnum[i][j]);
}
}
for(i=0;i<=4;i++)
{
printf("\n");
for(j=0;j<=4;j++)
{
printf("%d",lnum[i][j]);
if(lnum[i][j]>lnum[i][j-1])
lnsf=lnum[i][j];
lnum[i][j]=lnsf;

}

}
printf("the largest number is %d",lnsf);
getch();
}```

7. Oops, it appears you already had nested loops. Somehow I missed that.

One problem: On line 21, when 'i' and 'j' are both zero, [j-1] goes outside the bounds of your array.

You can simplify this by initializing your "lnsf" variable to a really low number (like -1 if you're only expecting positive input), and comparing the current array element with that variable - instead of comparing the current array element with a previous array element.

8. Originally Posted by Matticus
Oops, it appears you already had nested loops. Somehow I missed that.

One problem: On line 21, when 'i' and 'j' are both zero, [j-1] goes outside the bounds of your array.

You can simplify this by initializing your "lnsf" variable to a really low number (like -1 if you're only expecting positive input), and comparing the current array element with that variable - instead of comparing the current array element with a previous array element.
Thank you Matticus,yes [j-1] does go outside the bounds and your suggestion is better.
Here is the better code.
Code:
```#include<stdio.h>
main()
{
int lnum[5][5];
int i,j,lnsf=1;
for(i=0;i<=4;i++)
{
printf("\n");
for(j=0;j<=4;j++)
{
printf("Enter the number");
scanf("%d",&lnum[i][j]);
}
}
for(i=0;i<=4;i++)
{
printf("\n");
for(j=0;j<=4;j++)
{
printf("%d",lnum[i][j]);
if(lnum[i][j]>lnsf)
lnsf=lnum[i][j];
}

}
printf("the largest number is %d",lnsf);
getch();
}```