I did the below program in C. It works fine up to number 4, if you type number 5 forward I'm getting Segmentation fault: 11 error? Why? I cannot find where is the error. Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int sum, i, input;
int array [input];
float average;
sum = average = 0;
int size = sizeof (array) / sizeof(array[0]);
printf("Type the value of your array : ");
scanf("%d", &input);
printf("The size of your array is : %.2d \n", input);
for (i = 0; i < input; i++)
{
array[i] = rand() % 100 + 1;
}
// loop for printing results
for (i = 0; i < input; i++)
{
printf("Element %d; %d \n", i, array[i]);
}
for(i = 0; i < input; i ++){
sum = sum + array[i];
}
average = (float)sum / i;
printf("The average of array values is %.2f \n", average);
return 0;
};
int input; int array[input];is OH SO VERY WRONG!