0

It is saying that the variable p in the final function int main needs to be a constant but I attempted to change it to a constant and it still didn't run. Any recommendations on ways to fix it?

#define _CRT_SECURE_NO_WARNINGS // Disable warnings (and errors) when using non-secure versions of printf, scanf, strcpy, etc.
#include <stdio.h> // Needed for working with printf and scanf

    // defining a function to read input
    void input(double* array, int p) {
    // looping p times to read p values from user
    for (int i = 0; i < p; i++) {
        printf("Enter a value for #%d: ", i + 1);
        scanf("%lf", &array[i]);
        }
    }

    double processing(double* array, int p) {
    // defining a variable to store sum of all values
    double sum = 0;
    // looping over p values in the array
    for (int i = 0; i < p; i++) {
        sum += array[i];
    }
    // return average = sum / size of array
    return sum / p;
    }

    void output(double average) {
    // displaying average of all the values
    // displaying only one decimal point as given
    printf("The average of the values is %.1lf\n", average);
    }

    int main(void) {
    // Constant and Variable Declarations
    // defining size and initializing it to 10 as mentioned
    int p = 10;
    // size of p
    double array[p];
    
   
    input(array, p);
    double average = processing(array, p);
    output(average);

    return 0;
    } // end main() '''
Jens
  • 69,818
  • 15
  • 125
  • 179
  • Sorry if the post is formatted incorrectly. – Gunnar Patterson Apr 18 '22 at 14:29
  • That’s not what your compiler said. It didn’t say you need a constant. It said you need a constant integer expression, and variables, constant or not, are not constant integer expressions. You can use #define to define a macro named N defined as 10 and use that, or use 10 directly. – gnasher729 Apr 18 '22 at 14:36
  • Gunnar Patterson, Use a C99 compiler or later one that supports variable length arrays like `double array[p];`. – chux - Reinstate Monica Apr 18 '22 at 14:42

1 Answers1

0

Maybe you could call your function so instead of making P a variable:

input(array, 10);

sorry if it doesnt work im new to c as well