I have a problem with this program, the program must simply output the minimum value contained in a matrix, this must be done by the function that has as parameter the double pointer (**A) that would be the array , the problem is that after inserting the items , the program finishes and returns nothing.
(I don’t even appear written outuput instruction )
#include<stdio.h>
#include<stdlib.h>
int minium_matrix_value(int **A, int rows, int columns) {
int min=A[0][0];
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++)
if(A[i][j]<min)
min=A[i][j];
}
return min;
}
int main(){
int** A;
int i, j;
int result;
int sizer=100, sizec=100;
A = (int**)calloc(sizer,sizeof(int*));
A = (int**)calloc(sizec,sizeof(int*));
printf("insert rows size");
scanf("%d",&sizer);
printf("insert columns size");
scanf("%d",&sizec);
printf("insert matrix elements");
for( i=0; i < sizer; i++ )
for( j=0; j < sizec; j++ ) {
scanf("%d",((A+i)+j));
}
result=minium_matrix_value(A,sizer,sizec);
printf("the minium elements is: %d",result);
return 0;
}
sizer
should be after reading value into that/ // move this after scanf()
// A = (int**)calloc(sizer,sizeof(int*));
// remove this, or memory leak occur
// A = (int**)calloc(sizec,sizeof(int*));
printf("insert rows size");
scanf("%d",&sizer);
printf("insert columns size");
scanf("%d",&sizec);
// move here
A = calloc(sizer,sizeof(int*));
printf("insert matrix elements");
for( i=0; i < sizer; i++ ) { // add { to do multiple things
A[i] = calloc(sizec,sizeof(int)); // allocate row array
for( j=0; j < sizec; j++ ) {
//scanf("%d",((A+i)+j));
scanf("%d",&A[i][j]); // use array style, which should be easier to understand
}
} // add } to do multiple things
Also note that:
scanf("%d",((A+i)+j));
is wrong and it should be scanf("%d",(*(A+i)+j));
if you hate []
operator.malloc()
family is discouraged in C.