1

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;
}
Prarthana Hegde
  • 361
  • 2
  • 7
  • 18

2 Answers2

3

You allocated memory for a but not for the rows of a.

for( c = 0 ; c < m ; c++ )
{
   // Add this
   a[c] = malloc(n*sizeof(int));

   for( d = 0 ; d < n ; d++ )
   {
      scanf("%d",&a[c][d]);
   }
}

Also, make sure to add code to deallocate the memory.

for( c = 0 ; c < m ; c++ )
{
   free(a[c]);
}
free(a);

Further improvement:

Instead of using a hard coded number 10 to allocate memory for a, use m. Use:

int m, n, c, d, maximum;
int **a = NULL

scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");

a = malloc(m * sizeof(*a));

Don't use explicit cast to the value returned by malloc. See Specifically, what's dangerous about casting the result of malloc?.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0
#include<stdio.h>
#include<malloc.h>
int findMax(int **a,int m,int n);
int max;
int main()
{
  int **b,r,c,i,j,y;
  printf("Enter the number of rows in the matrix\n");
  scanf("%d",&r);
  printf("Enter the number of columns in the matrix\n");
  scanf("%d",&c);
  printf("Enter the elements in the matrix\n");
  b=malloc(sizeof(int*)*r);
  for(i=0;i<r;i++)
  {
    b[i]=malloc(sizeof(int*)*c);
    for(j=0;j<c;j++)
    {
      scanf("%d",&b[i][j]);
         }
  }
    printf("The matrix is\n");
      for(i=0;i<r;i++)
      {
        for(j=0;j<c;j++)
        {
          printf("%d ",b[i][j]);
        }
        printf("\n");
      }
    y=findMax(b,r,c);
      printf("The maximum element in the matrix is %d\n",y);
  return(0);
}
int findMax(int **a,int m,int n)
{
  int i,j;
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
      if(a[i][j]>max)
        max=a[i][j];
    }
  }
    return(max);
  }
A-004
  • 23
  • 1
  • 3