Accumulator and Temperature

This is a discussion on Accumulator and Temperature within the C++ Programming forums, part of the General Programming Boards category; Ok. I have to write a code that averages temperatures over a 7 day period. I have to write it ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    38

    Question 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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    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";

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    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.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    That's the general idea, but you need to work on your for loop syntax some more.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    38

    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;
    }

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    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;
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You probably want a different variable for the loop counter and the temperature input.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    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.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21