Anish Kumar
Anish Kumar

Reputation: 488

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

 #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.

Upvotes: 0

Views: 12581

Answers (4)

j.nagarjun
j.nagarjun

Reputation: 1

#include<stdio.h>
#include<stdlib.h>
int findMax(int **a,int m,int n)
{
    int i,j,larg;
    larg=a[0][0];
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            if(larg<a[i][j])
                larg=a[i][j];
        }
    }
    return larg;
}
int main()
{
    int m,n,i,j,larg;
    printf("Enter the number of rows in the matrix\n");
    scanf("%d",&m);
    printf("Enter the number of columns in the matrix\n");
    scanf("%d",&n);
    printf("Enter the elements in the matrix\n");
    int **a=(int**)malloc(m*sizeof(int *));
    for(i=0;i<m;i++)
        a[i]=(int *)malloc(n*sizeof(int));
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d\n",&a[i][j]);
    larg=findMax(a,m,n);
    printf("The matrix is\n");
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    printf("The maximum element in the matrix is %d\n",larg);
    return 0;
}

Upvotes: 0

manojpt
manojpt

Reputation: 1269

This code snippet works for you.

printf("Enter the elements in the matrix\n");
   for(i=0;i<m;i++){
      a[i]=malloc(sizeof(int)*c);
   for(j=0;j<n;j++)
      scanf("%d", &a[i][j]);
 }

Then your function call max=findMax(a,r,c); will work.

Upvotes: 1

Mukit09
Mukit09

Reputation: 3399

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. :)

Upvotes: 2

David Ranieri
David Ranieri

Reputation: 41017

You can not pass a 2d array (int a[10][10]) to a pointer to pointer.

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

should be

int findMax(int (*a)[10], int r, int c) /* Pointer to array of 10 ints */ 

Use the heap if you don't know the size of the 2d array beforehand (and note that arrays are base 0):

#include <stdio.h>
#include <stdlib.h>

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

int main(void)
{
    int *a, r, c, i, j, max;
    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);
    a = malloc(r * c * sizeof(*a));
    if (a == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    printf("Enter the elements in the matrix\n");
    for(i = 0; i < r; i++) {
        for(j = 0; j < c; j++)
            scanf("%d", &a[i * c + j]);
    }
    printf("The matrix is\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++)
            printf("%d", a[i * c + j]);
    }
    printf("\n");
    max = findMax(a, r, c);
    printf("The maximum elements in the matrix is %d\n", max);
    free(a);
    return 0;
}

int findMax(int *a,int r, int c)
{ 
    int t, i, j;

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

Or you can use a variable-length-array if you are under C99:

#include <stdio.h>

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

int main(void)
{
    int 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);
    int a[r][c];
    printf("Enter the elements in the matrix\n");
    for(i = 0; i < r; i++) {
        for(j = 0; j < c; j++)
            scanf("%d", &a[i][j]);
    }
    printf("The matrix is\n");
    for(i = 0; i < r; i++)  {
        for(j = 0; j < c; j++)
            printf("%d", a[i][j]);
    }
    printf("\n");
    max = findMax(r, c, a);
    printf("The maximum elements in the matrix is %d\n", max);
    return 0;
}

int findMax(int r, int c, int (*a)[c])
{ 
    int t, i, j;

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

Upvotes: 1

Related Questions