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();
}