0

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;                                                                    
} 
3
  • avg should be a parameter of returnAvg Commented Apr 13, 2015 at 4:32
  • okay i see, but why does it need to be inside the functions parameter? instead of being inside the braces. Commented Apr 13, 2015 at 4:44
  • returnAvg(allTest[2][2],students,test) should not have [2][2] in it. returnAvg(allTest,students,test) is correct way, you are not defining size of allTest here. Commented Apr 13, 2015 at 5:09

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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;                                                                    
} 

5 Comments

Very nice, but how come when i change students or test integers to a higher number like 3,5,10 ect. it does not complete the function correctly?
If you change the value of test and or students the program should be run good! What's your dubts?
i changed test = 3, avg[2] always equals 0, and avg[1] is avg[0]+avg[1]. I don't know how i'm getting that output.
i set students=3,test=2 input 100,150,25,50,10,20. output was : 125, 30, 0 . i dont understand, do u know what it is?
I've corrected it into the code above. The problem was that in the function returnAvg the variable i should be multiplied by test and not by students!!! ;)
1
 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);

Comments

0

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;                                                                  
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.