简体   繁体   中英

Passing a 2D array into a function

I am a newbie and I am doing functions practice. After executing following code, I get the output with a bunch of garbage values. What could be the mistake? Am I passing the 2d arrays into the functions properly?

#include <stdio.h>
void populArray (int array[][6], int rows);
void computtotal (int array[][6], int rows);
int main()
{
    int arr[10][6],ave,max;
    populArray( arr, 10);
    computtotal( arr, 10);

    return 0;
}
void populArray (int array[][6],int rows)
{
    int i,j;
    for (i=0;i++;i<rows)
    {
        for (j=0;j++;j<6)
        {
            printf("enter %d and %d th elemnt",i,j);
            scanf("%d",&array[i][j]);
        }
    }
 } 
 void computtotal (int array[][6], int rows)
 {
    int i,j,sum=0;
    for (i=0;i<rows;i++)
    {
        for (j=0;j<6;j++)
        {
            sum=sum+array[i][j];
            array[i][5]=sum;
            sum=0;
         }
     }
     for (i=0;i<rows;i++)
     {
        for (j=0;j<6;j++)
        {
            printf("%d",array[i][j]);
         }
         printf("\n");
     }

 }

In populArray()

for (i=0;i++;i<rows)
{
    for (j=0;j++;j<6)

You have swapped the condition check. It should be

for (i=0;i<rows;i++)
{
    for (j=0;j<6;j++)

NOTE: Use the standard definition of main()

int main(void) //if no command line arguments.
for (i=0;i++;i<rows) 

Is never executed because the condition i++ returns false.

Should be

for (i=0;i<rows;i++)

same for

for (j=0;j++;j<6)

First of all variables ave and max are declared but not used.

int arr[10][6],ave,max;

Remove them.

Instead of the magic numbers 10 and 6 it is better to use named constants. You could use a variable length array if the compiler supports them or define the constants using the #define directice. For example

#define ROWS 10
#define COLS 6

int main( void )
{
    int arr[ROWS][COLS];
    //...

Function populArray contains invalid records of loops

void populArray (int array[][6],int rows)
{
    int i,j;
    for (i=0;i++;i<rows)
             ^^^^^^^^^^
    {
        for (j=0;j++;j<6)
                 ^^^^^^^^
        //...

I think you mean the following

void populArray( int array[][6], int rows )
{
    int i,j;

    for ( i = 0; i < rows; i++ )
    {
        for ( j = 0; j < 6; j++ )
        {
            printf( "enter %d and %d th elemnt",i,j );
            scanf( "%d", &array[i][j] );
        }
    }
} 

It seems that in function computtotal you are trying to calculate the sum of all elements in a row and write it in the last element of the row. Otherwise the function does not make a great sense.

If I am right then the function can look like

void computtotal( int array[][6], int rows )
{
    int i, j;

    for ( i = 0; i < rows; i++ )
    {
        int sum = 0;

        for ( j = 0; j < 6; j++ )
        {
            sum = sum + array[i][j];
        }

        array[i][5] = sum;
    }

    for ( i = 0; i < rows; i++ )
    {
        for ( j = 0; j < 6; j++ )
        {
            printf( "%d ", array[i][j] );
            //      ^^^^^
        }
        printf( "\n" );
    }
}

And according to the C Standard function main without parameters shall be declared like

int main( void )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM