Here is the question - Write a program to find the maximum element in a matrix using functions.
Function specification:
int findMax(int **a, int m, int n) The first argument corresponds to the pointer to the matrix. The second argument corresponds to the number of rows in the matrix. The third argument corresponds to the number of columns in the matrix.
The following is my code and though there have been no compilation errors, I do not know where i am going wrong. Please help and thanks in advance!
#include<stdio.h>
#include<malloc.h>
int findMax(int **a, int m, int n) {
int c,d, maximum=a[0][0];
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
if ( a[c][d] > maximum )
maximum = a[c][d];
}
} return maximum;
}
int main()
{
int m, n, c, d, maximum;
int **a = (int **)malloc(10 * sizeof(int *));
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&a[c][d]);
}
}
printf("The matrix is\n");
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
printf("%d ",a[c][d]);
}
printf("\n");
}
maximum = findMax(a,m,n);
printf("The maximum element in matrix is %d\n", maximum);
return 0;
}