# Accumulator and Temperature

• 03-26-2006
liquidcourage1
Accumulator and Temperature
Ok. I have to write a code that averages temperatures over a 7 day period. I have to write it with the for, while, and do while loops. I've just started loops and I'm a little confused.

I think the biggest problem for me is that I can only use a single variable to store all 7 temperatures before they are averaged.

All I need is a push in the right direction. I don't want anybody to write the entire code. I'm just new to loops. Any help is greatly appreciated.
• 03-26-2006
Daved
How would you do it on a calculator? You wouldn't store each value separately on the calculator, you'd enter them one at a time and add them up as you go.
• 03-26-2006
liquidcourage1
That's the idea. So could it look something like this?

Code:

```{ int days=7; int temp; int total;    //accumulator for temps double average; { total=0;    //initialize accumulator for (int temp=1; temp++) { int tempScore; cout<<"Enter temperature "<<temp; cin>>tempScore; total +=tempScore; } average=total/days; cout<<"Average temperature over 7 day period: "<<average<<".\n\n";```
• 03-26-2006
liquidcourage1
Ok... I'm getting the temp to read temp2 instead of Temp 1 (that's how it should start anyway). And I'm stuck in a loop.
• 03-26-2006
Daved
That's the general idea, but you need to work on your for loop syntax some more.
• 03-26-2006
liquidcourage1
Update
I've gotten out of the loop, but I still can't figure out how to label the Temperature so that it begins at "Temperature 1" instead of some random number.

It's also taking the form of whatever number entered prior. For example, if I enter 52 for the first number it displays:

"Enter Temperature 52"

Code:

``` //Program is used to average Temp. over a 7 day period //                        By:  Carlton Lee #include <iostream> using namespace std; int main() {         int total;  //accumulator for temps         double average;         {         total=0;    //initialize accumulator         for (int temp=1; temp <=7;temp++)         {                 int temp;                 cout<<"Enter temperature  "<<temp<<" :\n";                 cin>>temp;                 total +=temp;         }         average=total/7; cout<<"Average temperature over 7 day period: "<<average<<".\n\n"; } return 0; }```
• 03-26-2006
jlharrison
Maybe something like this? I havent ran this code but it should get you on the right track.

Code:

```#include "stdafx.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int days; double totalScores = 0, tempScore=0, average; for(days = 0; days < 7; days++)         {     cout<<"Enter temperature\n ";     cin>>tempScore;     totalScores = tempScore + totalScores;     }   average=totalScores/days; cout<<"Average temperature over 7 day period: "<<average<<".\n\n";   return 0; }```
• 03-26-2006
Daved
You probably want a different variable for the loop counter and the temperature input.
• 03-26-2006
liquidcourage1
Here's what jlharrison and I have come up with (Thanks J!).

Code:

```#include <iostream> using namespace std; int main() { int days, average; double totalScores = 0, tempScore=0; for(days = 0; days < 7; days++)         {     cout<<"Enter temperature "<<days+1<<":\n";     cin>>tempScore;     totalScores = tempScore + totalScores;     }   average=totalScores/days; cout<<"Average temperature over 7 day period: "<<average<<".\n\n";   return 0; }```
It works for what we need it for. Variable "average" was changed to an "int" so we wouldn't have decimals in the average.

Now we have to figure out the "while and do while" loops.
• 03-26-2006
Dave_Sinkula
Quote:

Originally Posted by liquidcourage1
Now we have to figure out the "while and do while" loops.

http://www.daniweb.com/techtalkforums/post94143-2.html