0
#include <stdio.h>

#include <stdlib.h>

int findMax(int **a, int m, int n)

{

    int max=**a;
    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;

}
int main()

{ int 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");

    int **a=(int **) malloc (c*sizeof(int *));
    int i,j;

    for (i=0;i<c;i++){

        *(a+i)=(int *) malloc (r*sizeof (int));
    }

    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");
}
printf ("The maximum element in the matrix is %d",findMax(a,r,c));
return 0;
}

I tried to write a code to find maximum element in a matrix

It gives segmentation fault if we enter 3 as rows and 2 as columns. That is, after writing 5 inputs, it ends abruptly.

Sample Input:

Enter the number of rows in the matrix

3

Enter the number of columns in the matrix

2

Enter the elements in the matrix

2

4

1

3

5

9

This input fails. Any help would be greatly appreciated.

Girik Garg
  • 87
  • 7
  • 3
    Take care to not mix up `r` and `c`, which you seem to have done. – Some programmer dude Jan 22 '21 at 06:46
  • 2
    And on an unrelated note, remember that for *any* pointer or array `p` and index `i`, the expression `*(p + i)` is *exactly* equal to `p[i]`. The latter is usually easier to read and understand (and therefore easier to maintain), especially when it comes to jagged arrays like yours. It's also less to write. – Some programmer dude Jan 22 '21 at 06:51
  • 1
    Case in point regarding the readability, think about e.g. `*(*(a+i)+j)` and compare that with `a[i][j]`. Which would you prefer to read? Which is easier to see what's going on at a quick glance? – Some programmer dude Jan 22 '21 at 06:58
  • I am sorry @Someprogrammerdude but the coding platform doesn't allow me to use subscript operator and asks me to use only pointers as they are testing knowledge of pointers. – Girik Garg Jan 22 '21 at 07:43
  • 1
    `a[i][j]` _is_ using pointers. Tell your teacher to read this: [Do pointers support “array style indexing”?](https://stackoverflow.com/questions/55747822/do-pointers-support-array-style-indexing) – Lundin Jan 22 '21 at 09:45

1 Answers1

1

You just inverted column and row indexes in your loops.

The following seems to work fine:

#include <stdio.h>

#include <stdlib.h>

int findMax(int **a, int m, int n)

{

    int max=**a;
    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;

}
int main()

{ int 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");

    int **a=(int **) malloc (c*sizeof(int *));
    int i,j;

    for (i=0;i<c;i++){

        *(a+i)=(int *) malloc (r*sizeof (int));
    }

    for (i=0;i<c;i++){
        for (j=0;j<r;j++){
            scanf (" %d",(*(a+i)+j));
        }
    }
    printf ("The matrix is\n");

    for (i=0;i<c;i++){
        for (j=0;j<r;j++){
            printf ("%d ",(*(*(a+i)+j)));
        }
    printf ("\n");
}
printf ("The maximum element in the matrix is %d",findMax(a,c,r));
return 0;
}

On a sidenote, when you use 2 dimensional arrays, usually the first index is the row not the column. it might be important for readibility for other developpers.

Guillaume Petitjean
  • 2,408
  • 1
  • 21
  • 47