How to find the maximum element in the matrix using the function?

advertisements
 #include<stdio.h>
 int findMax(int **a,int r,int c);
 int main()
 {
  int a[10][10],i,j,max,r,c;
  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");
  for(i=1;i<=r;i++)
  {  for(j=1;j<=c;j++)
     scanf("%d",&[i][j]);
  }
  printf("The matrix is\n");
  for(i=1;i<=r;i++)
  { for(j=1;j<=c;j++)
    scanf("%d",&a[i][j]);
  }printf("\n");}
  max=findMax((int **)a,r,c);
  printf("The maximum elements in the matrix is %d\n",max);
  return 0;
  }
  int findMax(int **a,int r,int c)
  {
    int t,i,j;
    t=a[1][1];
    for(i=1;i<r;i++)
    { for(j=1;j<c;j++)
    { if(a[i][j]>t)
        t=a[i][j];
    }
    }
   return (t);
  }

Here I attached my coding, I need to find the maximum element present in the matrix using function, I am doing the coding, calling function is not executed, I don't know why, Help me to figure it out.


Change

int findMax(int **a,int r,int c)

to

int findMax(int (*a)[10],int r,int c)

And also,

for(i=1;i<r;i++)
{
    for(j=1;j<c;j++)
    {
        if(a[i][j]>t)
            t=a[i][j];
    }
}

to

for(i=1;i<=r;i++)
{
    for(j=1;j<=c;j++)
    {
        if(a[i][j]>t)
            t=a[i][j];
    }
}

EDIT:

I think, your code should be like this:

#include<stdio.h>

int findMax(int (*a)[10],int r,int c);

int main()
{
    int a[10][10],i,j,mx,r,c;
    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");

    for(i=1;i<=r;i++)
    {
        for(j=1;j<=c;j++)
            scanf("%d",&a[i][j]);
    }
    printf("The matrix is\n");
    for(i=1;i<=r;i++)
    {
        for(j=1;j<=c;j++)
            printf("%d ",a[i][j]);
        printf("\n");
    }
    printf("\n");
    mx=findMax(a,r,c);
    printf("The maximum elements in the matrix is %d\n",mx);
    return 0;
}

int findMax(int (*a)[10],int r,int c)
{
    int t,i,j;
    t=a[1][1];
    for(i=1;i<=r;i++)
    {
        for(j=1;j<=c;j++)
        {
            if(a[i][j]>t)
                t=a[i][j];
        }
    }
    return (t);
}

Hope, it will help. :)