user374112
user374112

Reputation: 17

Passing an array to function error?

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

double avrage(int array[5]);

int main(void)
{
    int arr[5] = {10,20,30,40,50};
    puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
    printf("Avg = %f",avrage(arr));
    return EXIT_SUCCESS;
}

double avrage(int array[5])
{
    int i,sum=0;
    double avg=0;
    for(i=0;i<5;i++)
    {
        sum = sum + array[i];
    }
    avg = sum / 5;
    return avg;
}

I have written code for passing an array to a function to get average of array element . but i am getting 3 errors that I am not understanding Can somebody help me to resolve these errors forgive me for my poor English

Errors:

  1. Description Resource Path Location Type conflicting types for 'avrage' Array_Argument.c
    /Array_Argument/src line 58 C/C++ Problem

  2. Description Resource Path Location Type expected declaration or statement at end of input Array_Argument.c /Array_Argument/src line 67 C/C++ Problem

  3. Description Resource Path Location Type too few arguments to function 'avrage' Array_Argument.c /Array_Argument/src line 54 C/C++ Problem

Upvotes: 0

Views: 83

Answers (1)

user2736738
user2736738

Reputation: 30926

avg = sum / 5.0; Otherwise integer arithmetic will truncate the result. Apart from that code is alright.

Ultimately your code boils down to double avrage(int *array). Arrays decays into pointer to the first element.

Upvotes: 2

Related Questions