0
#include <stdio.h>

void average(float age[]);

int main(void)
{
    float *p;
    float avg = 0.0;
    float age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
    p = &age[0];
    avg = average(age);
    printf("\n\nAverage age = %.2f\n\n\n", avg);
    return 0; // this should return the average age from the void average() function
}

void average(float age[])
{
    float *p;
    p = &age[0];
    int i = 0;
    float avg = 0.0 , sum = 0.0;
    for (i = 0; i < 6; ++i) 
    {
        sum += *(p+i);
    }
    avg = (sum / 6);
    //return avg; // this should be a return 0 (to go with the void)
}

Hi I am very very new to C, I am supposed to make the function "average" a void which i have done and then output everything in the main. Im not sure how to go about this, everything I try i get errors. I'd like to do it without creating another function if possible.

MD XF
  • 7,484
  • 7
  • 37
  • 66
Beginner Java
  • 63
  • 1
  • 8
  • 3
    what are the errors? – user3528438 Nov 07 '16 at 21:03
  • 1
    `void` is not returning any value, but you are trying to assign it to `avg`.. – Eugene Sh. Nov 07 '16 at 21:06
  • If you "supposed to make the function "average" a void" (why?), they you have to think about some other alternative way to return the result to `main`. You are not supposed to "call `main`" - it is already callerd for you. You are supposed to somehow return the average back to `main` and print it from there. – AnT Nov 07 '16 at 21:06
  • And what is this title? – Eugene Sh. Nov 07 '16 at 21:07
  • 1
    `void average(float age[])` --> `void average(float age[], float *return_value)` – chux - Reinstate Monica Nov 07 '16 at 21:07
  • 1
    "...this should be a return 0 (to go with the void)"? `return 0` does not go with the `void`. Where did you get that idea? – AnT Nov 07 '16 at 21:07
  • Read [ask] and follow the advice. – too honest for this site Nov 07 '16 at 21:08
  • 2
    Take my word for it; C isn't a trial-and-error language. The "error" part of that phrase will perpetually bite you to unending frustration. [Get a good **book**](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and spend the dues to learn the language. – WhozCraig Nov 07 '16 at 21:10

1 Answers1

2

I'm assuming this is part of an assignment, and the lesson they're trying to teach you is probably how to use pointers to manipulate values. A void function doesn't return anything at all, so your current code doesn't give you an average. Here's the change you should make:

void average(float age[], float *avg);

int main(void)
{
    float *p;
    float avg = 0.0;
    float age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
    p = &age[0];
    average(age, &avg);
    printf("\n\nAverage age = %.2f\n\n\n", avg);
    return 0; // this should return the average age from the void average() function
}

void average(float age[], float *avg)
{
    float *p;
    p = &age[0];
    int i = 0;
    *avg = 0.0;
    float sum = 0.0;
    for (i = 0; i < 6; ++i) 
    {
        sum += *(p+i);
    }
    *avg = (sum / 6);
    //return avg; // this should be a return 0 (to go with the void)
}

You're passing in a pointer to the average function, and it acts on the value of that memory address.

Here's some more information about pointers in C. https://study.cs50.net/pointers

TurnipEntropy
  • 477
  • 7
  • 12