9 Replies - 7959 Views - Last Post: 27 October 2006 - 08:34 AM Rate Topic: -----

#1 jonathan3174  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-October 06

Loop - Highest / Lowest

Posted 26 October 2006 - 08:58 PM

I have an assignment asking me to create a loop using a while loop that lets a user enter a series of integers. -99 to stop.

I got the original coding done to let the user enter continous numbers, but I am unsure how to assign the numbers the users enter to a variable that will determine what is highest and lowest.

Any insight is greatly appreciated. Here is what I have so far.
#include<iostream>
#include<iomanip>
using namespace std;
void main()

{
int number = 0;
int highest = 0;
int lowest = 0;
cout<< "Enter A Whole Number (-99 to quit): \n";
cin>>number;
while (number != -99)
{
cout<< "Enter another whole number (-99 to quit): \n";
cin>>number;
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Loop - Highest / Lowest

#2 ifoam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 26-October 06

Re: Loop - Highest / Lowest

Posted 26 October 2006 - 09:32 PM

you have to make number an array. so it can store multiple values. an array would be a simple solution but you have to know the size of it. you can initailize it to 1000 because i doubt you will enter more than that.

let me work on it for a little bit
Was This Post Helpful? 0
  • +
  • -

#3 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Loop - Highest / Lowest

Posted 26 October 2006 - 09:36 PM

after you ask the first time for number, you can assign that value to two more variable (high & low). then within the while loop all you have to do (after you get the next number) is just go through two if statements (or if + else if) testing to see if 'number' is higher then 'high' or lower than 'low', and if they are then reassign the value of high/low to the current value of number.
Was This Post Helpful? 0
  • +
  • -

#4 ifoam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 26-October 06

Re: Loop - Highest / Lowest

Posted 26 October 2006 - 09:47 PM

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	 int num[1000], input, x=0;
	 int highest = 0;
	 int lowest = 0;
	 
	 cout<< "Enter A Whole Number (-99 to quit): \n";
	 cin >> input;
	 while(input!=-99)
	 {
		num[x]=input;
		cout << "Enter another whole number (-99 to quit): \n";
		cin >> input;
		x++;
	 }
}




you just need to do the max and min functions.

This post has been edited by ifoam: 26 October 2006 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Loop - Highest / Lowest

Posted 26 October 2006 - 09:52 PM


#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	 int input;
	 int highest = 0;
	 int lowest = 0;
	
	 cout<< "Enter A Whole Number (-99 to quit): \n";
	 cin >> input;
	 highest = input;
	 lowest = input;
	 while(input!=-99)
	 {
		cout << "Enter another whole number (-99 to quit): \n";
		cin >> input;
		if(input >= highest)
		  highest = input;
		else if(input<= lowest)
		  lowest = input;
	 }
   cout<< "Highest number:" << highest<<endl;
   cout<<"Lowest number:"<<lowest<<endl;

}



Thats how you could do it using the if statement. i added a print at the bottom just as an example.

This post has been edited by Antiokus: 26 October 2006 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 jonathan3174  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-October 06

Re: Loop - Highest / Lowest

Posted 26 October 2006 - 09:53 PM

Hmm...we didnt even touch on array's in class....
Was This Post Helpful? 0
  • +
  • -

#7 ifoam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 26-October 06

Re: Loop - Highest / Lowest

Posted 26 October 2006 - 09:59 PM

well, you cant assign different values to the same varible because they were over right each other.

int array[5];

is like typing:
int num1, num2, num3, num4, num5;



look at Antiokus code

i didn't read it right the first time. all you need is the max and min so his will work better

sorry!
Was This Post Helpful? 0
  • +
  • -

#8 jonathan3174  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-October 06

Re: Loop - Highest / Lowest

Posted 26 October 2006 - 10:08 PM

That works perfect, however it includes -99 as a number, not as a end of the loop...

I should be able to add one more else if for that part of the code I believe...
Was This Post Helpful? 0
  • +
  • -

#9 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Loop - Highest / Lowest

Posted 27 October 2006 - 08:19 AM

you shouldnt need to. the while(input != -99) tells the system to leave the loop if input is -99, you shouldnt need to test for it within the loop.
Was This Post Helpful? 0
  • +
  • -

#10 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Loop - Highest / Lowest

Posted 27 October 2006 - 08:34 AM

disreguard that last post. I didnt understand the problem, but i see now. when you enter -99 to end the input it sets that to lowest.

heres what i came up with for testing for the -99:
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	 int input;
	 int highest = 0;
	 int lowest = 0;
	
	 cout<< "Enter A Whole Number (-99 to quit): \n";
	 cin >> input;
	 highest = input;
	 lowest = input;
	 while(input!=-99)
	 {
		cout << "Enter another whole number (-99 to quit): \n";
		cin >> input;
		
		if(input != -99)
		{
		   if(input >= highest)
			 highest = input;
		   else if(input<= lowest)
			 lowest = input;
		}
		else if (input == -99)
		{
			 cout<< "Highest number:" << highest<<endl;
			 cout<<"Lowest number:"<<lowest<<endl;
			 
			 cin.get();
			 cin.get();
			 exit(1);
		}   
	 }

   return 0;

}


works over here, but test it too and make sure maybe i didnt get just a one time thing.
Was This Post Helpful? 1

Page 1 of 1