# Thread: Temperature analyzer C program help

1. ## Temperature analyzer C program help

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?

2. > for(i = 1; i <= NUMS; i++)
One of the main problems you have is that arrays are 0-based subscripts.

So an array[4] has valid subscripts 0,1,2,3

By doing <= NUMS, you're running off the ends of both arrays.

3. Originally Posted by Salem
> for(i = 1; i <= NUMS; i++)
One of the main problems you have is that arrays are 0-based subscripts.

So an array[4] has valid subscripts 0,1,2,3

By doing <= NUMS, you're running off the ends of both arrays.
So you would suggest "for(i=1;i < NUMS; i++)" right? I only initialized the loop control variable as '1' with that condition because I couldn't figure out how my program would prompt for day 1, day 2, day 3 and day 4 sequentially otherwise.

If I use "for(i=1; i < NUMS; i++)", it won't ask for day 4

4. Originally Posted by BlackUmbreon
So you would suggest "for(i=1;i < NUMS; i++)" right?
No.

Originally Posted by BlackUmbreon
If I use "for(i=1; i < NUMS; i++)", it won't ask for day 4
The right way is:
Code:
```…
for(i = 0; i < NUMS; i++)
{
printf("Enter the high value for day %d: ", i + 1);
scanf("%d", &high[i]);
totalHigh = totalHigh + high[i];
…```

5. Originally Posted by WoodSTokk
No.

The right way is:
Code:
```…
for(i = 0; i < NUMS; i++)
{
printf("Enter the high value for day %d: ", i + 1);
scanf("%d", &high[i]);
totalHigh = totalHigh + high[i];
…```
Thanks, didn't even cross my mind that I could increment i within the printf function. Would you know how I could change my code to only record/read valid temperatures?

6. Originally Posted by BlackUmbreon
Thanks, didn't even cross my mind that I could increment i within the printf function. Would you know how I could change my code to only record/read valid temperatures?
Code:
```prompt for input

while input is invalid
print error message
prompt for input

process input```

7. Originally Posted by Matticus
Code:
```prompt for input

while input is invalid
print error message
prompt for input

process input```
Thanks

Updated code:
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 = 0; i < NUMS; i++)
{
printf("Enter the high value for day %d: ",i+1);
scanf("%d", &high[i]);

printf("                                  \n");

printf("Enter the low value for day %d: ",i+1);
scanf("%d", &low[i]);

while(high[i]>low[i]&&high[i]<40&&high[i]>-40&&low[i]>-40)
{
totalHigh = totalHigh + high[i];
totalLow = totalLow+low[i];
break;
}

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+1);
scanf("%d", &high[i]);

printf("                                  \n");

printf("Enter the low value for day %d: ",i+1);
scanf("%d", &low[i]);

while(high[i]>low[i]&&high[i]<40&&high[i]>-40&&low[i]>-40)
{
totalHigh = totalHigh + high[i];
totalLow = totalLow+low[i];
break;
}
printf("                                  \n");
}
combinedTemperature=(totalHigh+totalLow);
meanTemperature=(combinedTemperature/8);
}
printf("The average (mean) temperature was: %.2f",meanTemperature);
return 0;
}```

8. Code:
```while(high[i]>low[i]&&high[i]<40&&high[i]>-40&&low[i]>-40)
{
totalHigh = totalHigh + high[i];
totalLow = totalLow+low[i];
break;
}```
There is no purpose for this "while", as you're explicitly breaking out of the loop the very first time through. You do this again later in the code.

You also may want to calculate the mean temperature outside of the main loop, after all the values have been entered.

Instead of using the magic number 8 when calculating the mean, you should be using NUMS (times two).

If you spend some time planning out the logic on paper before writing code, your program could be written better.

9. Originally Posted by Matticus
Code:
```while(high[i]>low[i]&&high[i]<40&&high[i]>-40&&low[i]>-40)
{
totalHigh = totalHigh + high[i];
totalLow = totalLow+low[i];
break;
}```
There is no purpose for this "while", as you're explicitly breaking out of the loop the very first time through. You do this again later in the code.

You also may want to calculate the mean temperature outside of the main loop, after all the values have been entered.

Instead of using the magic number 8 when calculating the mean, you should be using NUMS (times two).

If you spend some time planning out the logic on paper before writing code, your program could be written better.
Yeah, I found the logic/semantics is by far the concept that's giving me the most trouble about programming soo far. I read that magic number link you provided and will practice writing flowcharts before source code in the future.

Does calculating the mean temperature outside of the main "for loop" make the code easier to read or is it for another reason? Regarding the while loop, are you suggesting I remove both break statements?

10. Originally Posted by BlackUmbreon
Yeah, I found the logic/semantics is by far the concept that's giving me the most trouble about programming soo far. I read that magic number link you provided and will practice writing flowcharts before source code in the future.
Writing code is only a piece of programming. Problem solving and logic development are necessary skills, so it is good that you will practice these. In fact, once you have a solution sufficiently broken down, converting it to code is generally pretty straight-forward.

Originally Posted by BlackUmbreon
Does calculating the mean temperature outside of the main "for loop" make the code easier to read or is it for another reason?
You want to read all values before you calculate the mean. This means the calculation should occur after the for loop has completed.

Originally Posted by BlackUmbreon
Regarding the while loop, are you suggesting I remove both break statements?
If you remove the break, those while loops will become infinite loops, since the control variables are not updated within them. To maintain functionality, turn them into ifs instead.

11. Originally Posted by Matticus
Writing code is only a piece of programming. Problem solving and logic development are necessary skills, so it is good that you will practice these. In fact, once you have a solution sufficiently broken down, converting it to code is generally pretty straight-forward.

You want to read all values before you calculate the mean. This means the calculation should occur after the for loop has completed.

If you remove the break, those while loops will become infinite loops, since the control variables are not updated within them. To maintain functionality, turn them into ifs instead.
Thanks, that makes sense. So basically never use break statements inside nested loops and instead always use if statements if break statements are needed.

Oh, and would you know how I would be able to display the highest/lowest temperatures and on which day they each occured on? Would I need to access and compare index values?

12. Originally Posted by BlackUmbreon
So basically never use break statements inside nested loops and instead always use if statements if break statements are needed.
Not quite. My advice was tailored to your specific situation, and not intended to be a univeral rule. Break statements should be used when needed to break out of a loop.

Code:
```while( condition )
{
do something, but condition is never updated
break
}```
... is equivalent to, and better expressed as:

Code:
```if( condition )
{
do something
}```
Exiting a loop can be done by monitoring its condition:

Code:
```while( condition )
{
do something; condition should be updated at some point
}```
... or using a break statement when necessary; for example:

Code:
```while( condition )
{
do something; condition should be updated at some point
if something goes wrong
break
}```
Originally Posted by BlackUmbreon
Oh, and would you know how I would be able to display the highest/lowest temperatures and on which day they each occured on? Would I need to access and compare index values?
You would print out each element of each array in order. The for loop is perfectly suited for this task. The index variable (used to access each element of an array) can also be used to print the day. In fact, you utilize each of these concepts in your code already.

13. Originally Posted by Matticus
You would print out each element of each array in order. The for loop is perfectly suited for this task. The index variable (used to access each element of an array) can also be used to print the day. In fact, you utilize each of these concepts in your code already.
Sorry for the way late message, something came up yesterday :/. Anyway, you were referring to parallel arrays for my question about displaying the high/low temperatures right? I think I worded the question incorrectly if that's the case. I meant to say how would I display only the highest/lowest temperatures of the 4 days (i.e The highest temperature was "12 " on day "4 ", The lowest temperature was "-5 " on day "3 ").