943,699 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9505
  • C++ RSS
Sep 1st, 2006
1

Calculating Average Temperature

Expand Post »
Hi, I am new to this web site and new to C++ programming. I am still in school and learning everyday. I am not catching on to some things so I was wondering if one can Help me.

My problem I am working right now has to do with averaging temperature in fahrenheit using a loop structure. I cannot get it to calculate no matter what I think it should be. My entries will be 78, 85, 87, 75, 86, 90, 80, 83, 90, 70. Can someone please show me or tell me what I need to do? I'd so very much appreciate it, thanks.

-Sami-
Reputation Points: 16
Solved Threads: 0
Newbie Poster
Starfighter330 is offline Offline
4 posts
since Aug 2006
Sep 1st, 2006
0

Re: Calculating Average Temperature

http://www.daniweb.com/techtalkforum...cement8-2.html
You say you've tried something, so post your best effort so far.

Don't forget this when you do post code.
http://www.daniweb.com/techtalkforum...cement8-3.html
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 1st, 2006
1

Re: Calculating Average Temperature

Sounds like you could use an array to store all those numbers, then have an
C++ Syntax (Toggle Plain Text)
  1. int sum
that you'd use to hold the cumulative sum of the temperatures as you looped through the array.

Something like this:
C++ Syntax (Toggle Plain Text)
  1. int temp[5]; //I forget how to assign values at initialization, if you even do that...
  2. int sum = 0;
  3. int ave = 0; //Calculated average, the output.
  4.  
  5. for(i = 0; i<5; i++)
  6. {
  7. sum = sum + temp[i]; //The new value is the old value plus the number stored in this section of the array.
  8.  
  9. }
  10.  
  11. ave = sum/5; //calculates average of five temperatures.

Shame on me for not having used arrays in long enough to actually forget the details on 'em, but it's the concept that's important here.

If you have a bunch of related numbers or characters or strings, using an array is a good way to organize them so you can access them iteratively. The for loop (or while loop, if you prefer) allows you to move through the array and use all the values if necessary, as long as you are careful to not exceed the size of the array.

There are ways to make the loop not require a hardcoded array length, as well. It'd be worthwhile for you to learn 'em so you know what you CAN do even if you don't know the exact details... I'm sure that others will disagree with me on that point.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Sep 1st, 2006
1

Re: Calculating Average Temperature

Click to Expand / Collapse  Quote originally posted by Salem ...
http://www.daniweb.com/techtalkforum...cement8-2.html
You say you've tried something, so post your best effort so far.

Don't forget this when you do post code.
http://www.daniweb.com/techtalkforum...cement8-3.html


Every time I do this it only calculate the last given temperature inputed and devides it by 10 (since there is suppose to be 10 given entries inputed). Below is my coding

C++ Syntax (Toggle Plain Text)
  1. {
  2.  
  3. //declare variables
  4. double averageTemperature
  5. int degrees
  6. string title
  7.  
  8. //Initialize variables
  9. degrees = 0;
  10.  
  11. //Display The Program Title
  12. cout << " Temperature Counter\n";
  13. cout << " -------------------------\n";
  14.  
  15. //Ask the user for temperature in fahrenheit
  16. cout << "Enter the degrees in fahrenheit or -99 to quit: ";
  17. cin >> degrees;
  18.  
  19. //Repeat the following instructions while degrees is not -99.
  20. while (degrees != -99)
  21.  
  22. {
  23.  
  24. //Increment the count for temperature
  25. if (degrees > 10)
  26. (averageTemperature = degrees / 10);
  27.  
  28. //ask for another temperature to repeat the loop
  29. cout << "Enter the degrees Fahrenheit or -99 to quit: ";
  30. cin >> degrees;
  31.  
  32. }
  33.  
  34. //display the report
  35. cout << endl;
  36. cout << "The average temperature is : " << averageTemperture << endl;
  37.  
  38. //display the histogram plot for each category
  39. cout << endl;
  40. title = "Degrees";
  41. plot(title, degrees);
  42.  
  43. return 0;
  44.  
  45. }
Last edited by WolfPack; Sep 2nd, 2006 at 12:53 am. Reason: Code Tags
Reputation Points: 16
Solved Threads: 0
Newbie Poster
Starfighter330 is offline Offline
4 posts
since Aug 2006
Sep 1st, 2006
1

Re: Calculating Average Temperature

For a while loop, the iterator (typically int i) is incremented at the end of the loop. Then, you can use i + 1 as your denominator in calculating the average, and do the cumulative sum of the contents of the array until the final entry (-99) is reached.
Last edited by Drowzee; Sep 1st, 2006 at 6:33 pm.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Sep 1st, 2006
1

Re: Calculating Average Temperature

> cin >> degrees;
You need to add to a running total of temperatures, then divide that total by 10 to get the average.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 1st, 2006
0

Re: Calculating Average Temperature

Click to Expand / Collapse  Quote originally posted by Salem ...
> cin >> degrees;
You need to add to a running total of temperatures, then divide that total by 10 to get the average.

Where would that go in the code?
Reputation Points: 16
Solved Threads: 0
Newbie Poster
Starfighter330 is offline Offline
4 posts
since Aug 2006
Sep 1st, 2006
1

Re: Calculating Average Temperature

Where would that go in the code?
You could use a for loop to receive input 10 times.

It would go something like
C++ Syntax (Toggle Plain Text)
  1. for (int i=0;i<=10;i++)
  2. sumoftemp += inputtemp; //the running total
Assuming you receive 10 values for input, you would then just divide the sum by 10. This would be obviously be after the loop.
Last edited by VinC; Sep 1st, 2006 at 10:22 pm.
Reputation Points: 28
Solved Threads: 0
Newbie Poster
VinC is offline Offline
16 posts
since Aug 2006
Sep 2nd, 2006
0

Re: Calculating Average Temperature

> for (int i=0;i<=10;i++)
Which loops 11 times...
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 2nd, 2006
0

Re: Calculating Average Temperature

Click to Expand / Collapse  Quote originally posted by Salem ...
> for (int i=0;i<=10;i++)
Which loops 11 times...
Ah you're right. Silly me, that was after the edit as well
Reputation Points: 28
Solved Threads: 0
Newbie Poster
VinC is offline Offline
16 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: illegal call of non-static member function
Next Thread in C++ Forum Timeline: Help me copy this array.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC