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.