Calculating Average Temperature

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2006
Posts: 4
Reputation: Starfighter330 is an unknown quantity at this point 
Solved Threads: 0
Starfighter330 Starfighter330 is offline Offline
Newbie Poster

Calculating Average Temperature

 
1
  #1
Sep 1st, 2006
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-
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Calculating Average Temperature

 
0
  #2
Sep 1st, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Calculating Average Temperature

 
1
  #3
Sep 1st, 2006
Sounds like you could use an array to store all those numbers, then have an
  1. int sum
that you'd use to hold the cumulative sum of the temperatures as you looped through the array.

Something like this:
  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.
Explainer of control logic and some basics.
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 4
Reputation: Starfighter330 is an unknown quantity at this point 
Solved Threads: 0
Starfighter330 Starfighter330 is offline Offline
Newbie Poster

Re: Calculating Average Temperature

 
1
  #4
Sep 1st, 2006
Originally Posted by Salem View Post
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

  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Calculating Average Temperature

 
1
  #5
Sep 1st, 2006
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.
Explainer of control logic and some basics.
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Calculating Average Temperature

 
1
  #6
Sep 1st, 2006
> cin >> degrees;
You need to add to a running total of temperatures, then divide that total by 10 to get the average.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 4
Reputation: Starfighter330 is an unknown quantity at this point 
Solved Threads: 0
Starfighter330 Starfighter330 is offline Offline
Newbie Poster

Re: Calculating Average Temperature

 
0
  #7
Sep 1st, 2006
Originally Posted by Salem View Post
> 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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Re: Calculating Average Temperature

 
1
  #8
Sep 1st, 2006
Originally Posted by Starfighter330 View Post
Where would that go in the code?
You could use a for loop to receive input 10 times.

It would go something like
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Calculating Average Temperature

 
0
  #9
Sep 2nd, 2006
> for (int i=0;i<=10;i++)
Which loops 11 times...
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Re: Calculating Average Temperature

 
0
  #10
Sep 2nd, 2006
Originally Posted by Salem View Post
> for (int i=0;i<=10;i++)
Which loops 11 times...
Ah you're right. Silly me, that was after the edit as well
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC