'finding out the locations and values of largest and second largest element in a two dimensional array
write a c program which finds the locations and values of largest and second largest element in a two dimensional array.
#include<stdio.h>
int main()
{
int row,col,i,j,max1=0,max2=0,flr,flc,slr=0,slc=0;
printf("Enter the value of row: ");
scanf("%d",&row);
printf("Enter the value of column: ");
scanf("%d",&col);
int a[col][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("Value of %d and %d is:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("The matrix is: \n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
if(a[i][j]>max1)
{
max2=max1;
max1=a[i][j];
flr=i;
flc=j;
}
if(a[i][j]>max2&&a[i][j]<max2)
{
max2=a[i][j];
slr=i;
slc=j;
}
}
}
printf("Largest number of this matrix is:%d ; Location is a[%d][%d]\n",max1,flr+1,flc+1);
printf("Second Largest number of this matrix is:%d ; Location is a[%d][%d]\n",max2,slr+1,slc+1);
}
I find out the largest value and its location. I also find out the second largest value .I try but I can't find out the location of second largest value. plz help me to find where I did wrong.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
