0

I am trying to find the maximum of all values in a 2D array accessed from a C function with the help of double pointer. When I run the code it just terminates with returning any value to the caller function.

I tried to change the code to print all values to find out the problem and found that it only prints 1 and 2 for the following sample data as input. For a sample code run, I provided row=2, col=2 and values=1,2,3,4

Please let me know why? Also if you question is not clear please say so. I've had a tough day so maybe couldn't explain better.

There is a few restrictions to the code: 1. Function Signature(int **a,int m,int n)

#include<stdio.h>

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

  int i,j;
  int max=a[0][0];
  for(i=0;i<m;i++){
    for(j=0;j<n;j++){
      if(a[i][j]>max){
         max=a[i][j];
      }
      //printf("\n%d",a[i][j]);
    }
  }

return max;
}

int main(){
  int arr[10][10],i,j,row,col;
  printf("Enter the number of rows in the matrix");
  scanf("%d",&row);
  printf("\nEnter the number of columns in the matrix");
  scanf("%d",&col);

  printf("\nEnter the elements of the matrix");
  for(i=0;i<row;i++){
    for(j=0;j<col;j++){
      scanf("%d",&arr[i][j]);
    }
  }

  printf("\nThe matrix is\n");
  for(i=0;i<row;i++){
    for(j=0;j<col;j++){
      printf("%d ",arr[i][j]);
    }
    printf("\n");
  }
  int *ptr1 = (int *)arr;
  printf("\nThe maximum element in the matrix is %d",findMax(&ptr1,row,col));
  return 0;
}
  • 4
    `int **a` isn't real 2D-Array. `int **a` --> `int (*a)[10]` – BLUEPIXY Mar 13 '16 at 12:31
  • 1
    `int arr[10][10]l` --> `int **arr;`... `arr=malloc(row * sizeof(int*));for(int i=0;i – BLUEPIXY Mar 13 '16 at 12:38
  • 2
    You're casting `arr`, and as a result you're accessing it incorrectly. I'm surprised you aren't getting a segmentation fault. Remove the cast and instead fix your declarations to declare the proper pointer types. – Tom Karzes Mar 13 '16 at 12:39
  • 1
    The end of your question reads like that there is more than restriction. Is it allowed to change the declaration / allocation of the array in `main`? – Martin Zabel Mar 13 '16 at 12:39
  • @BLUEPIXY Thank you for the answer. I did the changes and it worked. – Gagan Verma Mar 13 '16 at 14:23
  • @TomKarzes yes thank you. I found the mistake and rectified it. – Gagan Verma Mar 13 '16 at 14:24
  • @MartinZabel Sorry for the misleading line. There was just one restriction. And yes every other change is allowed inside 'main'. – Gagan Verma Mar 13 '16 at 14:26

1 Answers1

0

There is a few restrictions to the code: 1. Function Signature(int **a,int m,int n)

I guess your task is therefore to use an array of pointers, all of which point to allocations?

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

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

  int i,j;
  int max=a[0][0];

  for(i=0; i<m; i++)
  {
      for(j=0; j<n; j++)
      {
        if(a[i][j]>max)
        {
            max=a[i][j];
        }
      //printf("\n%d",a[i][j]);
      }
  }

return max;
}

int main(){
    int **arr;
    int i,j,row,col;
    printf("Enter the number of rows in the matrix");
    scanf("%d",&row);
    printf("\nEnter the number of columns in the matrix");
    scanf("%d",&col);

    arr = malloc(row * sizeof(int*));
    if (!arr)
    {
        printf("arr not malloc'd\n");
        abort();
    }

    for(i=0;i<row;i++)
    {
        arr[i] = malloc(col * sizeof(int));
        if (!arr[i])
        {
            printf("arr[%d] not malloc'd\n", i);
            abort();
        }

        for(j=0;j<col;j++)
        {
          arr[i][j] = i * j;
        }
    }

    printf("\nThe matrix is\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
          printf("%d ",arr[i][j]);
        }
        printf("\n");
    }


    printf("\nThe maximum element in the matrix is %d",findMax(arr, row, col));
    return 0;
}

The task of doing it in a single malloc is left as an exercise to the reader.

Pod
  • 3,938
  • 2
  • 37
  • 45