Ask Experts » Computers » Software »

C program to find largest element in the row and column of matrix


Date: 06 Jan 2010   Posted By: Charu     Group: Computers    Category: Software   

Please tell me the code for the above program



Author: Krishna Teja Yeluripati    19 Jul 2016      Member Level: Gold     Points : 7  (Rs 7)    Voting Score: 0

This question can be understood in two ways. So, I am mentioning the C programs for both the cases with their sample outputs and explanations.

To find the largest number of the matrix
Program
#include<stdio.h>
#include<conio.h>
int max(int a[][],int m,int n)
{
int i,j,max;
max=a[0][0];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>max)
max=a[i][j];
}
}
return max;
}
void main(void)
{
int a[25][25],m,n,i,j;
clrscr();
printf("\n Enter the order of matrix : ");
scanf("%dx%d",m,n);
printf("\n Enter the matrix row wise : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\n Largest number of matrix is : %d",max(a,m,n));
getch();
}

Output
Enter the order of matrix : 3x3
Enter the matrix row wise : 1 2 3 4 5 6 7 8 9
Largest number of matrix is : 9

Here, a function called 'max' is used to find the largest number in the matrix. A variable 'max' is considered and the value at position (0,0) is put in it. Then, it is compared with every number in the matrix and replaced if a larger number is found. Eventually, the largest number of the matrix gets into it.

To find the largest number in every row and every column of the matrix
Program
#include<stdio.h>
#include<conio.h>
int max(int a[],int n)
{
int i,max;
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
return max;
}
void main(void)
{
int a[25][25],b[25],m,n,i,j;
clrscr();
printf("\n Enter the order of matrix : ");
scanf("%dx%d",m,n);
printf("\n Enter the matrix row wise : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
b[j]=a[i][j];
printf("\n Largest number of row %d : %d",i+1,max(b,n));
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
b[j]=a[i][j];
printf("\n Largest number of column %d : %d",i+1,max(b,m));
}
getch();
}

Output
Enter the order of matrix : 3x3
Enter the matrix row wise : 1 2 3 4 5 6 7 8 9
Largest number of row 1 : 3
Largest number of row 2 : 6
Largest number of row 3 : 9
Largest number of column 1 : 7
Largest number of column 2 : 8
Largest number of column 3 : 9

Here, each row and each column of the matrix are copied to a single dimensional array and the largest number in that single dimensional array is found using the 'max' function. A variable 'max' is considered and the value at position 0 is put in it. Then, it is compared with every number in the single dimensional array and replaced if a larger number is found.

Hope this answer helps.



Is this a good answer ?    



Post Answer



You must Sign In to post a response.


Post Answer        





Related Questions
  • Return to Question Index
  • Ask Question to Experts


  • Awards & Gifts
    Top Contributors
    TodayLast 7 Daysmore...

     
    ISC Technologies, Kochi - India. © All Rights Reserved.