0

I wrote this code with cs50 course to get the average number:

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

const int TOTAL = 4;

int main(void){
    int score[TOTAL];
    for(int i = 0; i < TOTAL; i++){
        score[i] = get_int("score : ");
    }
    printf("%f\n", average(TOTAL, score));
}
float average(int length, int array[]){
    int sum = 0;
    for(int i = 0; i < length; i++){
        sum = sum + array[i];
    }
    return sum / (float) length;
}

but there is bunch of errors appeared:

clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    Average.c  -lcrypt -lcs50 -lm -o Average
Average.c:11:20: error: implicit declaration of function 'average' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    printf("%f\n", average(TOTAL, score));
                   ^
Average.c:11:20: error: format specifies type 'double' but the argument has type 'int' [-Werror,-Wformat]
    printf("%f\n", average(TOTAL, score));
            ~~     ^~~~~~~~~~~~~~~~~~~~~
            %d
Average.c:13:7: error: conflicting types for 'average'
float average(int length, int array[]){
      ^
Average.c:11:20: note: previous implicit declaration is here
    printf("%f\n", average(TOTAL, score));
                   ^
3 errors generated.
make: *** [<builtin>: Average] Error 1

i tried to solve it many times but I failed can anyone help me ?

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
marko
  • 15
  • 4
  • Move the `average` definition to be above `main` or add a forward declaration for the function. – kaylum Feb 16 '22 at 02:59

0 Answers0