Largest and Smallest from mxn Matrix using Pointer and User Defined Function
Question: Write a Program in c to find largest and smallest element from mxn matrix using concept of passing 2-Dimensional array to the user defined function and pointer.
Program
#include<stdio.h>
#include<conio.h>
/* Function Prototypes */
float getLarge(float *p, int m, int n);
float getSmall(float *p, int m, int n);
int main()
{
float a[10][10],large, small;
int m,n,i,j;
clrscr();
printf("Enter row and column size:\n");
scanf("%d%d", &m, &n);
for(i=0; i< m; i++)
{
for(j=0; j< n; j++)
{
printf("a[%d][%d]=",i,j);
scanf("%f",&a[i][j]);
}
}
/* Function Calls */
large = getLarge(&a[0][0], m, n);
small = getSmall(&a[0][0], m, n);
/* Output */
printf("Large value in matrix is: %0.3f\n", large);
printf("Small value in matrix is: %0.3f\n", small);
getch();
return 0;
}
/* Function Definition for getLarge() */
float getLarge(float *p, int m, int n)
{
float lg = *p;
int i,j;
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
if(*(p+10*i+j)>lg)
{
lg = *(p+10*i+j);
}
}
}
return lg;
}
/* Function Definition for getSmall() */
float getSmall(float *p, int m, int n)
{
float sm = *p;
int i,j;
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
if(*(p+10*i+j)< sm)
{
sm = *(p+10*i+j);
}
}
}
return sm;
}