Returning an array in a two dimensional array func

2019-09-17 22:17发布

问题:

I want to return the average of the two dimensional array with a different array using a function, the program runs fine, but it returns a big negative number, how do i return the array or apply pointers to my function? where do i add the pointer to make it work?

I encounter this:
warning: passing argument 1 of 'returnAvg' makes pointer from integer without a cast [enabled by default]|

#include <stdio.h>                                                             
#include <stdlib.h>                                                            
#include <math.h>                                                              

void returnAvg(int allTest[2][2],int students,int test);                       



int main ()                                                                    
{                                                                              

    int students = 2, test = 2, i,j;                                           
    int allTest[students][test];                                               


    for(i=0;i<students;i++){                                                   
        for(j=0;j<test;j++){                                                   
            printf("Student [%d] test [%d] score was> ",i+1,j+1);              
            scanf("%d",&allTest[i][j]);                                        
        }                                                                      
    }                                                                          
    returnAvg(allTest[2][2],students,test);                                    


    return 0;                                                                  
}                                                                              
void returnAvg(int allTest[2][2],int students,int test){                       
    int i,j;                                                                   
    int avg[students];                                                         

    for(i=0;i<students;i++){                                                   
        int sum = 0;                                                           
        for(j=0;j<test;j++){                                                   
            sum += (allTest[i][j]);                                            
        }                                                                      
        avg[i] = sum/test;                                                     
    }                                                                          
    printf("the average is %d, %d", avg[0],avg[1]);                            
    return;                                                                    
} 

回答1:

The way you used to pass the array to the function returnAvg is wrong! The simplest way I see it's to pass the array as a pointer. This because this kind of array is a chunk or contiguous memory areas!

I think the array and the vector may be allocated using a different way! Maybe using C++ new or C malloc; but this will become your next step!

The way to retrieve the vector containing the avg will be discussed below!

I've compiled your code under a 64 bit system adding this code into your main:

for(i=0;i<students;i++)
    for(j=0;j<test;j++)                                                   
        printf("%p\n",&(allTest[i][j]));

The output shall be something like this:

0x7fff0cd89e60
0x7fff0cd89e64
0x7fff0cd89e68
0x7fff0cd89e6c

This indicates what I said! All elements are contiguous!

We understand that there's a "base" pointer that points the first element. In the output is 0x7fff0cd89e60 (that is the pointer to allStudent[0][0]).

The relationship between this pointer and all pointers of the element of the array is:

0x7fff0cd89e60 + sizeof(int) * (i*test+j)

Stating the pointer arithmetic we can modify your function as:

void returnAvg(int * allTest,int students,int test){                       
    int i,j;                                                                   
    int avg[students];                                                         

    for(i=0;i<students;i++){                                                   
        int sum = 0;                                                           
        for(j=0;j<test;j++){                                                   
            sum += (allTest[i*students+j]);                                            
        }                                                                      
        avg[i] = sum/test;                                                     
    }                                                                          
    printf("the average is %d, %d", avg[0],avg[1]);                            
    return;                                                                    
} 

You may call this function in your main as:

returnAvg(&(allTest[0][0]),students,test);  

Now we may see how to pass the avg array to the main!

Here the code where you may also modify the number of students and tests results!

void returnAvg(int *avg, int * allTest,int students,int test);

int main ()                                                                    
{                                                                              

    int students = 2, test = 2, i,j;                                           
    int allTest[students][test];                                               

    int avg[students];

    /*
    for(i=0;i<students;i++)
    for(j=0;j<test;j++)                                                   
           printf("%p\n",&(allTest[i][j]));
    */

    for(i=0;i<students;i++){                                                   
        for(j=0;j<test;j++){                                                   
            printf("Student [%d] test [%d] score was> ",i+1,j+1);              
            scanf("%d",&allTest[i][j]);                                        
        }                                                                      
    }                       


    returnAvg(avg,&(allTest[0][0]),students,test);                                    
    for(i=0;i<students;i++){                                                   
        printf("Student %d average: %d\n", i+1, avg[i]);
    }

    return 0;                                                                  
}

void returnAvg(int * avg, int * allTest,int students,int test){                       
    int i,j;                                                                   

    for(i=0;i<students;i++){                                                   
        int sum = 0;                                                           
        for(j=0;j<test;j++){                                                   
            sum += (allTest[i*test+j]);                                            
        }                                                                      
        avg[i] = sum/test;                                                     
    }                                                                          

    return;                                                                    
} 


回答2:

Your code is good except:

returnAvg(allTest[2][2],students,test);

should be

returnAvg(allTest,students,test);

You don't have to give size of attTest here, because you are not defining it here, just a parameter providing to the function. You may see you working code here.



回答3:

 returnAvg(allTest[2][2], students, test);

is wrong since allTest[2][2] evaluates to an int and the function expects an array int [2][2].

You need to use:

 returnAvg(allTest, students, test);


回答4:

Here a quick update of your code, allTest arg should be reworked too

#include <stdio.h>                                                             
#include <stdlib.h>                                                            
#include <math.h>                                                              

static void returnAvg(int allTest[2][2],int students,int test, int *avg){      
    int i,j;                                                                   

    for(i=0;i<students;i++){                                                   
        int sum = 0;                                                           
        for(j=0;j<test;j++){                                                   
            sum += (allTest[i][j]);                                            
        }                                                                      
        avg[i] = sum/test;                                                     
    }                                                                          
    printf("the average is %d, %d", avg[0],avg[1]);                            
}                                                                              

int main ()                                                                    
{                                                                              
    int students = 2, test = 2, i,j;                                           
    int allTest[students][test];                                               
    int avg[students];                                                         

    for(i = 0; i < students; i++){                                             
        for(j = 0; j < test; j++){                                             
            printf("Student [%d] test [%d] score was> ", i + 1, j + 1);        
            scanf("%d", &allTest[i][j]);                                       
        }                                                                      
    }                                                                          
    returnAvg(allTest, students, test, avg);                                   


    return 0;                                                                  
}