Mega Code Archive

 
Categories / C / Beginners
 

Program to pick & display the largest element of an input matrix

#include <stdio.h> #include <conio.h> #include <math.h> #define MAXROWS 30 #define MAXCOLS 30 void largest(int a[][MAXCOLS],int nrows,int ncols); void readinput(int a[][MAXCOLS],int m,int n); void main() { int nrows,ncols; int a[MAXROWS][MAXCOLS]; clrscr(); printf("How many rows in the matrix? "); scanf("%d",&nrows); printf("How many columns in the matrix? "); scanf("%d",&ncols); printf(" Table "); readinput(a,nrows,ncols); largest(a,nrows,ncols); getch(); } void readinput(int a[][MAXCOLS],int m,int n) { int row,col; for (row=0;row<m;++row) { printf(" Enter data for row no. %2d ",row+1); for (col=0;col<n;++col) scanf("%d",&a[row][col]); } printf(" TABLE 1"); for (row=0;row<m;++row) { printf(" "); for (col=0;col<n;++col) printf("%d%c",a[row][col],' '); } return; } void largest(int a[][MAXCOLS],int m,int n) { int i,j,largest; largest = a[0][0]; for (i=0;i<m;++i) { for (j=0;j<n;++j) { if (a[i][j]>largest) largest=a[i][j]; } } printf(" The largest element of the matrix is %d",largest); return; }