0

I tried using the code David show to average a set of scores in an array.

#include <stdio.h>
#include <cs50.h>

float average (void);
int main(void)

{
    int n = get_int("Total scores: ");
    int scores[n];
    for(int i = 0; i < n; i++)
    {
        scores[i] = get_int("Score %i\n", i+1);
    }
    printf("Average %f", average);
}

float average(int n, int scores[])
{
    int sum = 0;
    for(int i = 0; i < n; i++)
    {
        sum = sum + scores[i];
    }
    return sum / (float) n;
}

This is the error I get.

arrayPractice2.c:14:26: error: format specifies type 'double' but the argument has type 'float (*)(void)' [-Werror,-Wformat]
    printf("Average %f", average);
                    ~~   ^~~~~~~
arrayPractice2.c:17:7: error: conflicting types for 'average'
float average(int n, int scores[])
      ^
arrayPractice2.c:4:7: note: previous declaration is here
float average (void);
      ^

I never define average as a double so I don't understand why it says there is a conflict. Any help you could provide would be greatly appreciated.

2 Answers 2

2

The error you are getting is not saying that you are passing a double instead of a float, but that a double is expected. You are passing average, which is not a variable but a function. Your prototype declaration for the function (float average (void)) is also throwing an error because it does not match the function, which need has a definition of float average (int n, int scores[]). So, you need to apply two fixes to get this to work:

First, correct your prototype declaration of average to be float average (int n, int scores[]);. Remember that your prototype declaration has to be identical to the function's declaration that comes later in your code or the compiler will think that you have two different functions.

Then, change the line printf("Average %f", average); to printf("Average %f", average(n, scores);. This will pass the necessary parameters that the function average requires, allowing it to return the average that you want to print. Remember, to call a function that has declared parameters, you need to provide the parameters.

Hope this helps and makes sense. If it does, please feel free to click the check mark next to my answer. If not, let me know and I will try to help further.

3
  • For clarity, the "function prototype" is the statement at the top of the code that tells the compiler that the function is coming later. In this case, the erroneous prototype is float average (void);. It needs to be identical to the first line of the actual function, except that it will end with a semicolon. Nice explanation, @Robert S. Pratt
    – Cliff B
    Commented Mar 31, 2021 at 1:54
  • Super helpful! I don't completely understand why the printf("Average%f, average(n, scores); needs the parameters provided. Isn't a function just the output of its parameters run through its code? Are there ever instances where you can omit specific parameters to make a function do different things?
    – Multiplify
    Commented Mar 31, 2021 at 2:00
  • average needs these parameters because it doesn't have the values to perform its work without them. It is like cooking dinner without the ingredients, the chef can always perform the work but only if you give them the stuff they need. Yes, there is a way to omit parameters--this is called function overloading, where you write a function with the same declaration, but different required parameters that performs essentially the same job with different requirements (think making a hamburger vs cheeseburger) . C does not support function overloading to my knowledge, though. Commented Mar 31, 2021 at 2:06
-1

The code shown is passing the address of the address function to print. I suspect this is a typo, the intent is to call the average function, with the return value being used by print. Note this line has an extra set of parenthesis:

printf("Average %f", average());
1
  • 1
    It has nothing to do with passing an address, but with an error of declaration and type. There are two declarations of average, one requiring no parameters and one requiring two. In both the question and this answer, a nonexistent function is being called that does not return the float the compiler is expecting (which cannot then be cast to the double that printf is expecting). Commented Mar 31, 2021 at 1:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .