1

In the function returnAvg I need the code to return an array, but it only returns the first element I'm not to familiar with pointer. ar[0] is averaged completely but ar[1] is always 0 why is that happening?

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

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


int main ()
{
    int students = 2, test = 2, i,j;
    int allTest[students][test];
    double ar[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]);
        }
    }
    *ar = returnAvg(allTest, students, test);

    for(i = 0;i<students;i++){
        printf("\nthe average score of student[%d] is : %.2lf\n",i+1, ar[i]);
    }

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

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

2 Answers 2

3

You are trying to return local array to other function that is wrong.

When your function returns its local memory gone..

You need to use Malloc for that array and then return its pointer

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

   avg = malloc(sizeof(double) * students);


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

Dont forget to free that memory after use :)

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

5 Comments

i've only encountered malloc for getting the size of bytes and ints, how would i implement that into my program, malloc(sizeof(int))
"Dont forget to free that memory after use :)" This is why I prefer to pass a pointer/array that was allocated by the caller to a function, as It's his duty to later on free the memory.
You do not need to implement malloc(). Its standard C library function you can just use it
@James Lopez malloc takes the number of bytes to allocate and returns a pointer to that memory, so malloc(2); gets 2 bytes from memory and double *arr=malloc(sizeof(double)*3); gets 3 lengths of double from memory, You can treat the second malloc as double arr[3]; (but check if malloc succeeded) as pointer and array in C are closely related. I suggest You read about pointers as they are very useful and integral part of C/C++ programming.
@riodoro1 yea i agree that its the most professional way to use that. Most APIs of libraries are developed that way.
0

It is not possible to return arrays in C, sorry.

You can return a struct containing an array, but that only works for fixed-size arrays.

The best solution here is to require the caller to make space:

void computeAvg( int students, int test, int input[students][test], double output[students])
{
    // ...
    output[i] = (double)sum/test;
    // ...
}

In the calling code:

double ar[students];
computeAvg(students, test, allTest, ar);

I rearranged the order of your function arguments so that you can write the correct dimensions int input[students][test] instead of putting [2][2] which would be wrong for other values of student and test.


Another possible solution would be to only compute one student's average at a time, and return double. Then your main function does that in a loop:

double studentAvg(int test, int input[test])
{
    int sum = 0;
    for(int j=0;j<test;j++)
        sum += input[j];

    return (double)sum / test;
}

// in main

for (int i = 0; i < students; ++i)
     ar[i] = studentAvg(test, allTest[i]);

2 Comments

Note: I'd recommend using more descriptive variable names. From the list test, allTest, ar it's not very clear what is what unless you were already familiar with the code. For example those could be num_tests, test_scores, averages.
that's very helpful I managed to do something like your second code, when i got stuck on it, i checked yours and my problem was solved. thanks

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.