0

i try make a program to show the largest number in column a 2d array, but this program have a problem which cannot produce the desired result. in my compiler says :

 3  5   [Note] expected 'int *' but argument is of type 'int (*)[(sizetype)(b)]'

and this my code :

    #include<stdio.h>
    
    int array2d(int *x, int a, int b){
        int i,j;
        int max[j];
        for(i=0; i<a ; i++){
            max[j]= *x;
            for(j=0; j<b ; j++){
                if(*x>max[j])
                {
                    max[j]=*x;
                }
            }
            printf("\nthe maximum value of each column is : %d", max[j]);
        
    }
    int main(){
        int a,b,i,j;
        
        printf("enter the size of array (rows) & (column) : ");
        scanf("%d %d",&a,&b);
        
        int x[a][b];
        
        printf("enter the number : ");
        for ( i = 0; i < a; i++)
        {
            for ( j = 0; j < b; j++)
            {
                scanf("%d",&x[i][j]);
            }
        }
        
        array2d(x,a,b);
        
        return 0;
    }

the program input :

4 4

and input number :

1 3 2 1
8 4 3 2
1 2 3 4
9 8 7 6

And expected this output :

9 8 7 6

What should I do to fix it? I need your opinion and maybe anyone wants to help me to write the right code.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pratama
  • 45
  • 8
  • See [this answer](https://stackoverflow.com/a/41410503/841108) to a related question – Basile Starynkevitch Dec 06 '20 at 09:32
  • i still don't understand it after reading it @BasileStarynkevitch – pratama Dec 06 '20 at 09:34
  • Then you need to read a good book on C programming, such as [*Modern C*](https://modernc.gforge.inria.fr/). Once you have read that book, refer to [this website](https://en.cppreference.com/w/) and read some C draft standard such as [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – Basile Starynkevitch Dec 06 '20 at 10:07
  • Read also the documentation of your C compiler (e.g. [GCC](http://gcc.gnu.org/)...). Compile with all warnings and debug info (so `gcc -Wall -Wextra -g`). Then use your debugger (e.g. [GDB](https://www.gnu.org/software/gdb/)...) to understand the behavior of your program. You could use [GNU emacs](https://www.gnu.org/software/emacs/) to edit your source code. StackOverflow is not a do-my-homework service – Basile Starynkevitch Dec 06 '20 at 10:09
  • Take also inspiration from *existing* open source programs on [github](https://github.com/) or [gitlab](https://gitlab.com/) – Basile Starynkevitch Dec 06 '20 at 10:12

3 Answers3

3

Here you have a working example:

See the pointer arithmetics.

void printArray(int *x, size_t a, size_t b)
{
    if(x && a && b)
    {
        for(size_t i = 0; i < a; i++)
        {
            for(size_t j = 0; j < b; j++)
                printf("%d\t", *(x + i * a + j));
            printf("\n");
        }
    }
}

void array2d(int *x, size_t a, size_t b)
{
    if(x && a && b)
    {
        for(size_t i = 0; i < b; i++)
        {
            int max = *(x + i);
            for(size_t j = 0; j < a; j++)
            {
                if(*(x + j * a + i) > max)
                {
                    max = *(x + j * a + i);
                }
            }
            printf("\nthe maximum value of each column is : %d", max);
        }
    }

}

int main(void)
{
    size_t a = 0,b = 0;
    
    printf("enter the size of array (rows) & (column) : ");
    scanf("%d %d",&a,&b);
    printf("\n");
    

    int x[a][b];

    srand(time(NULL));
    
    for (size_t i = 0; i < a; i++)
    {
        for (size_t j = 0; j < b; j++)
        {
            x[i][j] = rand() % 1000;
        }
    }
    
    printArray(*x, a, b);
    array2d(*x, a, b);
    
    return 0;
}

https://godbolt.org/z/zzaxG6

0___________
  • 60,014
  • 4
  • 34
  • 74
2

You declared a two-dimensional variable length array

    scanf("%d %d",&a,&b);
    
    int x[a][b];

The element type of the array is int[b].

Used in expressions array designators with rare exceptions are converted to pointers to their first elements.

So in this function call

array2d(x,a,b);

the array designator x is converted to pointer to its first element of the type int ( * )[b]. However the corresponding function parameter has the type int *.

int array2d(int *x, int a, int b){

and there is no implicit conversion from the type int ( * )[b] to the type int *. So the compiler issues an error.

Moreover within the function you are using another variable length array max the size of which is set with the uninitialized variable j.

    int i,j;
    int max[j];

There is no need to declare this array to output maximum values.

Also the function has the return type int but returns nothing.

So in any the function is incorrect and does not make a sense.

The function can be declared and defined the following way

void array2d( size_t m, size_t n, int a[m][n] )
{
    for ( size_t j = 0; j < n; j++ )
    {
        int max = a[0][j];

        for ( size_t i = 1; i < m; i++ )
        {
            if ( max < a[i][j] ) max = a[i][j];
        }

        printf( "\nthe maximum value in the column %zu is: %d", j, max );
    }
}

Here is a demonstrative program.

#include <stdio.h>

void array2d( size_t m, size_t n, int a[m][n] )
{
    for ( size_t j = 0; j < n; j++ )
    {
        int max = a[0][j];

        for ( size_t i = 1; i < m; i++ )
        {
            if ( max < a[i][j] ) max = a[i][j];
        }

        printf( "\nthe maximum value in the column %zu n is: %d", j, max );
    }
}

int main(void) 
{
    size_t m, n;
    
    printf( "enter the size of array (rows) & (column) : " );
    scanf( "%zu %zu", &m, &n );
    int a[m][n];

    
    printf( "enter the number : " );
    for ( size_t i = 0; i < m; i++ )
    {
        for ( size_t j = 0; j < n; j++ )
        {
            scanf( "%d", &a[i][j] );
        }
    }
    
    array2d( m, n, a );

    return 0;
}

Its output might look like

enter the size of array (rows) & (column) : 4 4
enter the number : 
1 3 2 1
8 4 3 2
1 2 3 4
9 8 7 6
the maximum value in the column 0 is: 9
the maximum value in the column 1 is: 8
the maximum value in the column 2 is: 7
the maximum value in the column 3 is: 6

If in the loops of the function to use pointers then the function can look the following way

void array2d( size_t m, size_t n, int a[m][n] )
{
    for ( int *p = *a; p != *a + n; ++p )
    {
        int max = *p;

        for ( int ( *q )[m] = a + 1; q != a + m; ++q )
        {
            if ( max < **q ) max = *( *q + ( p - *a ) );
        }

        printf( "\nthe maximum value in the column %zu is: %d", ( size_t )( p - *a ), max );
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

In your code,when you declare a matrix a = rows, b = cols and you use the while loop to make this condtion: "rows>0 or cols>0"(ignored the 0 and negative numbers)

#include<stdio.h>

// Function declaration to input and print two dimensional array
  void input_Matrix(int cols ,int matrix[][cols],int rows);
  void output_Matrix(int cols ,int matrix[][cols],int rows);
//Function to know the maximum of each column
  void Max_Cols(int cols ,int matrix[][cols],int rows);

int main()
{
     int rows,cols;
     do
     {
         printf("Enter the size of rows of this Matrix : ");
         scanf("%d",&rows);
     }while(rows<1);
     do
     {
         printf("Enter the size of cols of this Matrix : ");
         scanf("%d",&cols);
     }while(cols<1||cols!=rows);
     int matrix[rows][cols];

     //Input elements in the matrix
     printf("\nEnter elements of [%d,%d] matrix \n",rows,cols);

     input_Matrix(cols,matrix,rows);
     //Print elements in the matrix

     printf("\nDisplay Elements of [%d,%d] matrix \n",rows,cols);
     output_Matrix(cols,matrix,rows);

     //Print the maximum of each cols
     printf("\n\nMaximum of each column :\n");
     Max_Cols(cols,matrix,rows);

     return 0;
}

/**
 * Function to display elements of two dimensional array (matrix)
 * on console.
 *
 * @matrix  2D array to display as input.
 * @rows    Total rows in 2D matrix.
 * @cols    Total columns in 2D matrix.
 */

 void input_Matrix(int cols ,int (*matrix)[cols],int rows)
 {
      for(int i=0;i<rows;i++)
      {
           for(int j=0;j<cols;j++)
           {
                // (*(matrix + i) + j is equivalent to &matrix[i][j]
                scanf("%d", (*(matrix + i) + j));
           }
      }
 }


void output_Matrix(int cols ,int (*matrix)[cols],int rows)
{
      for (int i=0;i<rows;i++)
      { printf("\n");
           for (int j=0;j<cols;j++)
           {
               // *(*(matrix + i) + j) is equivalent to &matrix[i][j]
               printf("%d ", *(*(matrix + i) + j));
           }
      }
}

void Max_Cols(int cols ,int (*matrix)[cols],int rows)
{
      for (int j=0;j<cols;j++)
      {int *max=matrix[0][j];
           for (int i=0;i<rows;i++)
           {
              // *(*(matrix + i) + j) is equivalent to &matrix[i][j]
              if(*(*(matrix + i) + j)>max)
              {
                   max=*(*(matrix + i) + j);
              }
           }
           printf("\nThe Maximum of column %d is : %d\n",j,max);
      }
}
MED LDN
  • 684
  • 1
  • 5
  • 10