-1

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;

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Res
  • 13
  • 2
  • 5
    C and C++ are different language. Would you mind choosing one? – MikeCAT Nov 05 '20 at 19:14
  • Start with a much smaller matrix, like 2x2 and then print the elements after you read them in. Pretty soon you'll find your issue. – AndyG Nov 05 '20 at 19:17
  • 2
    Why do you overwrite `A` immediately after the first assignemtn? And why do you take user input for `sizer` and `sizec` _after_ using them? Also note that `A` is only an array of uninitialized pointers, you never actually allocate an array of ints. – Lukas-T Nov 05 '20 at 19:17
  • You are mixing an attempt to build a 'jagged array' (array of pointers to 1D arrays) with a 1D array indexed as 2D. The `scanf("%d",((A+i)+j))` is wrong. – Weather Vane Nov 05 '20 at 19:18
  • The code is C code; it doesn't seem to use any C++ features. As such, tag it with just C, unless you particularly like to earn the ire of those who might otherwise help you. – Jonathan Leffler Nov 05 '20 at 19:21

1 Answers1

1
  • You only allocated arrays to store pointers to rows. You have to allocate arrays for storing values of each rows.
  • Using the value of 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.
  • Casting result of malloc() family is discouraged in C.
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Ok thanks i corrected the errors, but i didn't understand why it's not good to use calloc to allocate elements. With new [] what are you referring to? You mean there is a better way to allocate items? – Res Nov 05 '20 at 21:06
  • and one last thing I'm sorry, but why do you use a double pointer (int **) in the calloc for sizer allocation and only a pointer (int *) for sizec? – Res Nov 05 '20 at 21:14
  • @AbdallaElAwad Casting is for C++ support. I removed casting because C is selected. `(int**)` was used because the destination of assignment `A` is `int**` and `(int*)` was used because the destination `A[i]` is `int*`. – MikeCAT Nov 05 '20 at 22:50