0

Basically, I need to do what the title says. I've done it a couple of times fully inside of main() and it's rather simple, but never inside of a separate function, so I'm having some trouble on how to handle pointers when facing a multidimensioal (in this case 2D) array. The full title of the task is: Write a separate function that finds the biggest element of a 2D array that consists of dimensions n*m , where 2<=n<=5 and 2<=m<=8. You must fill in the matrix in main() with data type of int , and print out the result of the function using "%d".

This is what I've come up with so far:

#include <stdio.h>

int search(int **M , int x , int y){
    
    int i , j;
    
    int max;
    
    for(i=0; i<x; i++){
        for(j=0; j<y; j++){
            if(M[i][j] > max){
                max = M[i][j];
            }
        }
    }
    
    return max;
}

int main(){
    
    int n , m;
    
    do{
        scanf("%d" , &n);
        
    }while(n<2 || n>5);
    
    do{
        scanf("%d" , &m);
        
    }while(m<2 || m>8);
    
    int A[n][m];

    int i , j;

    for(i=0; i<n; i++){
        for(j=0; j<m; j++){
            scanf("%d" , &A[i][j]);
        }
    }

    int r = search(**A , n , m);
    
    printf("RESULTS:\n");
    printf("%d" , r);
    
    return 0;
}

And this is what errors my compiler displays when I run the program:

main.c:48:24: warning: passing argument 1 of ‘search’ makes pointer from integer without a cast [-Wint-conversion]

main.c:7:9: note: expected ‘int **’ but argument is of type ‘int’

Basically, it runs the program, you can successfully input the dimensions and the elements of array but it never runs throughout the function properly. So yeah, could use some help here if possible.

Toky
  • 3
  • 2
  • `int r = search(**A , n , m);` should be `int r = search(A , n , m);` because A is a double pointer but when you dereference it you get the value inside and that means `**A` is equivalent to `A[0][0]` which is just the first element. – Jerry Jeremiah Mar 16 '21 at 20:24

3 Answers3

1

VLA for the win :-)

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

int search(int rows, int cols, int M[rows][cols]) { // VLA, M must be after rows/cols
    int max = M[0][0];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (M[i][j] > max) {
                max = M[i][j];
            }
        }
    }
    return max;
}

int main(void) {
    int n, m;
    //scanf("%d%d", &n, &m);
    n = 4; m = 7;

    int A[n][m];                             // VLA
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            A[i][j] = rand() % 1000;
            printf("%4d", A[i][j]);
        }
        puts("");
    }

    int r = search(n, m, A);
    printf("RESULTS: %d\n", r);

    return 0;
}

The function search() takes a VLA argument with the specified values.
After that just treat M as usual for array.

pmg
  • 106,608
  • 13
  • 126
  • 198
0

Arrays in C are not the same as in Java. So let's say that you have an Array

array = {{1,2},{3,4}}

In order to access it's element all you need to do is iterate once throughout the array. Elements are stacked in the memory one after the other. In order to get 3 all you need to do is :

array[2]

So just picture the array as one Dimensional, and all you need is the total number of elements, which you get by multiplying its Dimensions.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

You have to change this line removing **

int r = search(**A , n , m);

Becomes

int r = search(A , n , m);
Rudy Barbieri
  • 389
  • 1
  • 4
  • 16