Hello cboard,
I'm experiencing difficulties with the syntax of my c program where its intent it to ask user for high and low temperatures for 4 days, analyze those values to see if they meet requirements(temperatures must not exceed 40 degrees celsisus/fall below -40 celsius and high must be greater than low),provide the average temperature for the 4 days, and then display what the highest/lowest temperature were and on which day they respectively occured on.
My current problem is that my program won't discard the incorrect values after the nested while loop is executed and therefore outputs a false average temperature
Code:
#include<stdio.h>const int NUMS = 4;
int main (void)
{
int i,totalHigh=0,totalLow=0;
int high[4];
int low[4];
float combinedTemperature,meanTemperature;
printf("---=== IPC Temperature Calculator ===---\n");
for(i = 1; i <= NUMS; i++)
{
printf("Enter the high value for day %d: ",i);
scanf("%d", &high[i]);
totalHigh=(totalHigh+high[i]);
printf(" \n");
printf("Enter the low value for day %d: ",i);
scanf("%d", &low[i]);
totalLow=(totalLow+low[i]);
printf(" \n");
while(high[i]<low[i]||high[i]>40||high[i]<-40||low[i]<-40)
{
printf("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");
printf("Enter the high value for day %d: ",i);
scanf("%d", &high[i]);
totalHigh=(totalHigh+high[i]);
printf(" \n");
printf("Enter the low value for day %d: ",i);
scanf("%d", &low[i]);
totalLow=(totalLow+low[i]);
printf(" \n");
}
combinedTemperature=(totalHigh+totalLow);
meanTemperature=(combinedTemperature/8);
}
printf("The average (mean) temperature was: %.2f",meanTemperature);
return 0;
}
Output that would give me a false average temperature since the incorrect temperatures (45,11) are taken into account:
---=== IPC Temperature Calculator ===---
Enter the high value for day 1: 45
Enter the low value for day 1: 11
Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.
Enter the high value for day 1: 6
Enter the low value for day 1: 4
Enter the high value for day 2: 6
Enter the low value for day 2: 4
Enter the high value for day 3: 6
Enter the low value for day 3: 4
Enter the high value for day 4: 6
Enter the low value for day 4: 4
The average (mean) temperature was: 12.00
How do I change my source code to only record correct values? How would I implement a way for my program to display what the highest/lowest temperature were and on which day they respectively occured on?
Thanks in advance for your help