I'm trying to create 2 dynamic arrays from the user input but the compiler is throwing a stack smashing array. This is my code:
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#define NUMS 3
// Put your code below:
int main() {
int high, low;
int high_temp[3];
int low_temp[3];
int total_temp = 0;
double median = 0;
printf("---=== IPC Temperature Analyzer ===---\n");
for (int i = 1; i <= NUMS; i++) {
printf("Enter the high value for day %d: ", i);
scanf("%d/n", &high);
printf("Enter the low value for day %d: ", i);
scanf("%d/n", &low);
if(!((high > low && high < 41) && (low < high && low > -41))) {
printf("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low");
i = i - 1;
}else{
high_temp[i] = i;
low_temp[i] = i;
}
}
for (int i = 0; i < sizeof(high_temp); i++){
//total_temp = total_temp + high_temp[i] + low_temp[i];
printf("The high value: %d", high_temp[i]);
printf("The low value: %d", low_temp[i]);
printf("-----");
}
}
And this is the output. The error happens when I'm trying to print out the elements in each array. I'm doing this to see if any of the illegal values creeped into the array. I'm suppose to take their medians afterwards.
---=== IPC Temperature Analyzer ===---
Enter the high value for day 1: 8
Enter the low value for day 1: -2
Enter the high value for day 2: 41
Enter the low value for day 2: -4
Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low
Enter the high value for day 2: 9
Enter the low value for day 2: -4
Enter the high value for day 3: 5
Enter the low value for day 3: 11
Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low
Enter the high value for day 3: 11
Enter the low value for day 3: 5
*** stack smashing detected ***: terminated