| | |
Calculating Average Temperature
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2006
Posts: 4
Reputation:
Solved Threads: 0
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-
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-
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
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
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
Sounds like you could use an array to store all those numbers, then have an that you'd use to hold the cumulative sum of the temperatures as you looped through the array.
Something like this:
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.
C++ Syntax (Toggle Plain Text)
int sum
Something like this:
C++ Syntax (Toggle Plain Text)
int temp[5]; //I forget how to assign values at initialization, if you even do that... int sum = 0; int ave = 0; //Calculated average, the output. for(i = 0; i<5; i++) { sum = sum + temp[i]; //The new value is the old value plus the number stored in this section of the array. } 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."
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
•
•
Join Date: Aug 2006
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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)
{ //declare variables double averageTemperature int degrees string title //Initialize variables degrees = 0; //Display The Program Title cout << " Temperature Counter\n"; cout << " -------------------------\n"; //Ask the user for temperature in fahrenheit cout << "Enter the degrees in fahrenheit or -99 to quit: "; cin >> degrees; //Repeat the following instructions while degrees is not -99. while (degrees != -99) { //Increment the count for temperature if (degrees > 10) (averageTemperature = degrees / 10); //ask for another temperature to repeat the loop cout << "Enter the degrees Fahrenheit or -99 to quit: "; cin >> degrees; } //display the report cout << endl; cout << "The average temperature is : " << averageTemperture << endl; //display the histogram plot for each category cout << endl; title = "Degrees"; plot(title, degrees); return 0; }
Last edited by WolfPack; Sep 2nd, 2006 at 12:53 am. Reason: Code Tags
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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."
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
You could use a for loop to receive input 10 times.
It would go something like
Assuming you receive 10 values for input, you would then just divide the sum by 10. This would be obviously be after the loop.
It would go something like
C++ Syntax (Toggle Plain Text)
for (int i=0;i<=10;i++) sumoftemp += inputtemp; //the running total
Last edited by VinC; Sep 1st, 2006 at 10:22 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Programming a gui in C
- Next Thread: Help me copy this array.
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






