I have this code here which is working but when it gets to the very end it throws a run time check and I'm not sure why, also trying to free the memory at the end will result in a different exception "Access violation reading location 0x00000024" and I can't figure out what the problem is
# include<stdio.h>
# include <stdlib.h>
int createTheArray(int *array) {
int y;
printf("Enter the number of random numbers you wish to create and store: ");
scanf_s("%i", &y);
array = (int *)calloc(y,sizeof(int));
if (array == NULL)
printf("Out of memory");
else {
printf("Memory allocated \n");
return y;
}
}
void populateArray(int* array, int y) {
int x;
for (x = 0; x < y; x++) {
array[x] = rand() % 100;
}
for (x = 0; x < y; x++) {
printf("array[%i]: %i\n", x ,array[x]);
}
}
void operateArray(int* array, int y) {
int i,sum=0,high=0,low=array[0];
float average;
for (i = 0; i < y; i++) {
sum = sum + array[i];
if (array[i] > high)
high = array[i];
if (array[i] < low)
low = array[i];
}
average = (float) sum / y;
printf("\n");
printf("Sum: %i \n", sum);
printf("high: %i \n", high);
printf("low: %i \n", low);
printf("Average: %.1f \n", average);
}
main() {
int* array = (int *)calloc(1,sizeof(int));
int y;
y = createTheArray(array);
populateArray(array,y);
operateArray(array, y);
free(array);
return 0;
}