Thread: Calculating the average of average of a series of numbers using functions

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    8

    Post Calculating the average of average of a series of numbers using functions

    Hello, I have written this programme to calculate the average of a series of numbers. It works when I do not use functions but doesn't when I use functions. Could someone help me find out why using functions is not correct for this task ?
    Thanks in advance for your help.

    Code:
    CODE WITH FUNCTIONS THAT DOESN'T WORK
    //This programme is just a modification of chapter15_exercice3.c such that the task is accomplished using functions
    
    #include<stdio.h>
    
    float average(int n, int numbers[n]) //a function that returns the average of the elements stored in a array 
    {
    
    int i, sum = 0;
    for (i = 0; i < n; i++)
    {
    sum += numbers[i];
    } 
    
    return sum / n;
    }
    
    
    
    main()
    {
    int n,i;
    printf("Please enter the number of numbers you wish to calculate the average\n");
    scanf("%d", &n);
    int numbers[n];
    for (i = 0; i < n; i++)
    {
    printf("Enter number %d\n",i+1);
    scanf("%d", &numbers[i]);
    }
    
    printf("Here are the numners that you've enter\nThe average of the following numbers are to be calculated\n");
    for (i = 0; i < n; i++) //just to display to the user the numbers that they have entered
    {
    printf("%d, ", numbers[i]);
    }
    
    printf("\n");
    
    
    printf("The average of the numbers that you've entered is %.2f\n", average(n, numbers[n])); 
    
    return 0;
    
    }
    Result:
    chapter15_exercice3.c: In function ‘main’:
    chapter15_exercice3.c:40:1: warning: passing argument 2 of ‘average’ makes pointer from integer without a cast [enabled by default]
    printf("The average of the numbers that you've entered is %.2f\n", average(n, numbers[n]));
    ^
    chapter15_exercice3.c:5:7: note: expected ‘int *’ but argument is of type ‘int’
    float average(int n, int numbers[n]) //a function that returns the average of the elements stored in a array
    ^


    CODE WITHOUT FUNCTIONS THAT WORKS
    Code:
    #include<stdio.h>
    /*
    float average(int n, int numbers[n])
    {
    
    int i, sum = 0;
    for (i = 0; i < n; i++)
    {
    sum += numbers[i];
    } 
    
    return sum / n;
    */
    
    
    
    main()
    {
    int n,i;
    printf("Please enter the number of numbers you wish to calculate the average\n");
    scanf("%d", &n);
    int numbers[n];
    for (i = 0; i < n; i++)
    {
    printf("Enter number %d\n",i+1);
    scanf("%d", &numbers[i]);
    }
    
    printf("Here are the numners that you've enter\nThe average of the following numbers are to be calculated\n");
    for (i = 0; i < n; i++)
    {
    printf("%d, ", numbers[i]);
    }
    
    printf("\n");
    
    int sum = 0;
    float average;
    for (i = 0; i < n; i++)
    {
    sum += numbers[i];
    } 
    average = sum / n;
    printf("The average of the numbers that you've entered is %.2f\n", average);
    
    return 0;
    
    }
    Result:
    Please enter the number of numbers you wish to calculate the average
    5
    Enter number 1
    4
    Enter number 2
    4
    Enter number 3
    4
    Enter number 4
    4
    Enter number 5
    4
    Here are the numners that you've enter
    The average of the following numbers are to be calculated
    4, 4, 4, 4, 4,
    The average of the numbers that you've entered is 4.00

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    90
    It's not such a big deal, first change the prototype of the function:
    Code:
    int numbers[n]
    This is an actual integer, not an array of integers, since n is an expected number.
    It should actually be:
    Code:
    int numbers[]
    Because when calling the function you'll be giving it the length.

    Now the thing is that when you call the function numbers[n] is not an array but an integer, you should be calling the function with the actual array that is:
    'numbers'

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Note that on line 43 you have integer division, so you will never get rational part with it.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    8
    Thanks @Fiskker and @vart for enlighting me on that, my code now works
    Last edited by Kanga; 06-25-2015 at 02:33 PM.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    725
    Quote Originally Posted by Fiskker View Post
    It's not such a big deal, first change the prototype of the function:
    Code:
    int numbers[n]
    This is an actual integer, not an array of integers, since n is an expected number.
    It should actually be:
    Code:
    int numbers[]
    Because when calling the function you'll be giving it the length.
    This is actually not a problem. numbers is a pointer to int in all these cases:
    Code:
    float average(int n, int numbers[n]);
    float average(int n, int numbers[]);
    float average(int n, int *numbers);

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    8
    Thanks @christop for enlighting me on function prototypes, i guest i did not quit unerstand function prototype very well. Thanks man.

  7. #7
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by christop View Post
    This is actually not a problem. numbers is a pointer to int in all these cases:
    Code:
    float average(int n, int numbers[n]);
    float average(int n, int numbers[]);
    float average(int n, int *numbers);
    It's not a problem per se, but why would you put the length of the array in there, unnecessary, specially because you pass that length in the first parameter, right?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by christop View Post
    This is actually not a problem. numbers is a pointer to int in all these cases:
    Code:
    float average(int n, int numbers[n]);
    float average(int n, int numbers[]);
    float average(int n, int *numbers);
    But in the case of the OP's code, it is a problem, since 'n' is not a valid identifier in the scope of the function prototype.

    Code:
    #include <stdio.h>
    
    int sum_array(int numbers[n], int total);
    
    int main(void)
    {
        int i,sum,n = 10;
        int numbers[n];
    
        for(i = 0; i < n; i++)
            numbers[i] = i+1;
    
        sum = sum_array(numbers,n);
    
        printf("The sum is %d\n",sum);
    
        return 0;
    }
    
    int sum_array(int numbers[n], int total)
    {
        int i,sum=0;
    
        for(i = 0; i < total; i++)
            sum += numbers[i];
    
        return sum;
    }
    Code:
    /*
    main.c|3|error: 'n' undeclared here (not in a function)
    */

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    725
    Quote Originally Posted by Matticus View Post
    But in the case of the OP's code, it is a problem, since 'n' is not a valid identifier in the scope of the function prototype.

    Code:
    #include <stdio.h>
    
    int sum_array(int numbers[n], int total);
    
    int main(void)
    {
        int i,sum,n = 10;
        int numbers[n];
    
        for(i = 0; i < n; i++)
            numbers[i] = i+1;
    
        sum = sum_array(numbers,n);
    
        printf("The sum is %d\n",sum);
    
        return 0;
    }
    
    int sum_array(int numbers[n], int total)
    {
        int i,sum=0;
    
        for(i = 0; i < total; i++)
            sum += numbers[i];
    
        return sum;
    }
    Code:
    /*
    main.c|3|error: 'n' undeclared here (not in a function)
    */
    Where did you get that code from? It's not the OP's code.

    OP's code has this function declaration:
    Code:
    float average(int n, int numbers[n])
    Which is valid and self-documenting.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by christop View Post
    Where did you get that code from? It's not the OP's code.

    OP's code has this function declaration:
    Code:
    float average(int n, int numbers[n])
    Which is valid and self-documenting.
    Touche - I neglected to notice that 'n' was its own parameter. (My example was intended to be a mock-up, but an invalid one as it turns out.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 27
    Last Post: 02-14-2015, 11:17 AM
  2. Replies: 3
    Last Post: 11-14-2014, 10:46 PM
  3. calculating average
    By coolca in forum C Programming
    Replies: 1
    Last Post: 10-11-2006, 05:55 PM
  4. Calculating an average
    By Andy123 in forum C Programming
    Replies: 11
    Last Post: 03-02-2006, 10:40 AM
  5. calculating average
    By Sharlene in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2003, 03:10 PM

Tags for this Thread