Thread: A little help in displaying a high value in a loop

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    13

    A little help in displaying a high value in a loop

    I've been racking my brain for about 2 hours now, so I decided to ask CProgramming for help. I have a problem I got to do for school and I have basically everything but I can't get one thing.

    The problem is I need to make a loop where I keep entering temperatures. I did this by using a do-while. I then end the loop when I enter 999 as a sentinel or flag to stop. So I enter the numbers like this...

    Enter temperature: 88
    Enter temperature: 66
    Enter temperature: 69
    Enter temperature: 97
    Enter temperature: 57
    Enter temperature: 999

    So after I did this, I ended the loop. Now I was suppose to find the average temp and the high temp. I did the average temp no problem by doing sum of values devided by how many temperatures, but I have no idea how to get the program to display the 97 as the highest temperature. A little help please. Basically I have everything and I just need to know how it displays the largest number entered in the loop. THat's the only bit of code I need. If you need me to I will post the code I currently have, I'd just have to retype it up on this comp.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    Also, I would need to do this the most basic way as possible.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    if ( temp > max_so_far ) max_so_far = temp;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    Here we go...

    Code:
    /* Temperature Program */
    
    #include <iostream.h>
    
    int main()
    {
    	cout << "Temperature Program" << endl;
    	cout << "===================" << endl;
    
    	int Temperature;
    	int NumberOfTemps = 0;
    	int SumOfTemps = 0;
    	const int Sentinel = 999;
    
    	cout << "Enter temperature: (" << Sentinel << " to quit): ";
    	cin >> Temperature;
    
    	do
    	{
    		SumOfTemps = SumOfTemps + Temperature;
    		NumberOfTemps = NumberOfTemps + 1;
    
    		cout << "Enter temperature: (" << Sentinel << " to quit): ";
    		cin >> Temperature;
    	}
    	while (Temperature != Sentinel);
    
    	cout << "Average: " << (SumOfTemps)/(NumberOfTemps) << endl;
    	cout << "High Temperature: " << endl;
    	return(0);
    }
    If someone can fill that in at the bottom, I would be forever greatful.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    Nevermind guys, I was playing around with it and got it. If anyone else has the same problem, here is my finished code...

    Code:
    /* Temperature Program */
    
    #include <iostream.h>
    
    int main()
    {
    	cout << "Temperature Program" << endl;
    	cout << "===================" << endl;
    
    	int Temperature;
    	int HighTemp;
    	int NumberOfTemps = 0;
    	int SumOfTemps = 0;
    	const int Sentinel = 999;
    
    	cout << "Enter temperature: (" << Sentinel << " to quit): ";
    	cin >> Temperature;
    
    	do
    	{
    		SumOfTemps = SumOfTemps + Temperature;
    		NumberOfTemps = NumberOfTemps + 1;
    
    		cout << "Enter temperature: (" << Sentinel << " to quit): ";
    		cin >> Temperature;
    
    		if ((Temperature != 999) && (Temperature > HighTemp))
    		HighTemp = Temperature;
    	}
    	while (Temperature != Sentinel);
    
    	cout << "Average: " << (SumOfTemps)/(NumberOfTemps) << endl;
    	cout << "High Temperature: " << HighTemp << endl;
    	return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. Having problems with my for loop
    By kalibaby in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2004, 05:41 PM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM