0

I tried to do something by combining functions, arrays and pointers, but I got this error, I couldn't figure out why, I would appreciate if you could help.

double findaverage(int howmany, *int grades[]);
#include <stdio.h>

double findaverage(int howmany, *int grades[]) {
    int i;
    int sum = 0;
    for (i = 0; i < howmany; i++) {
        sum = grades[i] + sum;
    }
    return sum / howmany;
}

int main()
{
    const int size = 5;
    int grades[5] = {30,56,23,44,45};
    int average= findaverage(size, grades);
    printf("%d", average);

    return 0;
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
RhoAies
  • 5
  • 3
  • 3
    The function argument `*int grades[]` should be `int grades[]` or `int *grades`. – Weather Vane Jun 18 '22 at 20:27
  • 2
    Aside `return sum / howmany;` will be an integer converted to `double`. There is no floating point division. You would need `double sum = 0;` or `return (double)sum / howmany;` But then you are assigning the function result back to an integer: be careful with your types. – Weather Vane Jun 18 '22 at 20:29
  • 1
    as an aside, having includes after the start of the c code is weird, although correct it is not idiomatic c – pm100 Jun 18 '22 at 21:18

1 Answers1

0

My interpretation on how to transform the comments into code.

  • removed obsolete prototype definition (it is obsolete due to function call order)
  • renamed function from findaverage to calculate_average due to what it does
  • changed *int grades[] to int grades[]
  • moved iterator declaration (int i) into loop as it is not used outside of the loop
  • changed type of sum from int to double
  • used += operation instead of sum = sum + ..., to keep the code short and expressive
  • changed type of average from int to double
  • added helper function to print input array
  • changed output type format, added two digital places and newline to output of average
  • add pointer versions of calculate_average to illustrate/clarify differences

The original data are structured as array. Therefore, they should be accessed in array style. Nevertheless, it is possible to process them in pointer style. See also the answers to What is the difference between char array and char pointer in C? for clarification.

calculate_average.c

#include <stdio.h>

double calculate_average(int howmany, int grades[]) {
    double sum = 0.0;
    for (int i = 0; i < howmany; i++) {
        sum += grades[i];
    }
    return sum / howmany;
}

double calculate_average_pointers(int howmany, int *grades) {
    double sum = 0.0;
    for (int i = 0; i < howmany; i++) {
         // calculate position of next int in memory 'grades + i' and
         // retrieve int from it with dereference operator '*'
        sum += *(grades + i);
    }
    return sum / howmany;
}

// working but bad mix of semantics, array as paramter, pointer arithmetic inside
double calculate_average_pointers_2(int howmany, int grades[]) {
    double sum = 0.0;
    for (int i = 0; i < howmany; i++) {
        sum += *(grades + i);
    }
    return sum / howmany;
}

void print_array(int howmany, int grades[]) {
    printf("[");
    for (int i = 0; i < howmany; i++) {
        char *comma_or_not = i < howmany - 1? "," : "";
        printf(" %d%s", grades[i], comma_or_not);
    }
    printf(" ]\n");
}

int main()
{
    const int size = 5;
    int grades[5] = {30, 56, 23, 44, 45};

    printf("Input: ");
    print_array(size, grades);

    double average = calculate_average(size, grades);

    printf("Average: %.2lf\n", average); // two decimal places

    average = calculate_average_pointers(size, grades);
    printf("Average (pointers):   %.2lf\n", average); // two decimal places

    average = calculate_average_pointers_2(size, grades);
    printf("Average (pointers 2): %.2lf\n", average); // two decimal places

    return 0;
}
$ gcc -Wall calculate_average.c
$ ./a.out                      
Input: [ 30, 56, 23, 44, 45 ]
Average: 39.60
Average (pointers):   39.60
Average (pointers 2): 39.60
$ 
Franck
  • 1,340
  • 2
  • 3
  • 15
  • Why don't we use pointers in arrays? Since I'm new to coding, I haven't been able to figure out exactly where pointers are used and where they are not used. Do you have a chance to explain this issue to me a little? – RhoAies Jun 19 '22 at 07:08
  • @RhoAies I updated the code with two functions which use pointer arithmetic inside. In general, a more common use case for pointers is when the function wants to modify the given parameter value so that the changes to it are visible outside of the function. See https://stackoverflow.com/a/29423983/18980756 as example. – Franck Jun 19 '22 at 09:01