954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using a loop to find highest and lowest values

I am trying to write a program that allows the user to input positive integers between 0 and 1000 and then outputs the highest value and the lowest value. The user should enter -1 to signify the end of their input. I am supposed to use a loop to achieve this.

I am not allowed to use arrays and I am not sure which loop to use. Here is what I have so far:

#include <iostream>
using namespace std;

int main ()
{
	int first_input, largest, smallest

cout << "Enter numbers between 0 and 1000 and i will tell you the highest and"<<endl;
cout << " lowest numbers. Enter -1 to signal the end of your data entered." << endl;
	cin >> first_input

		if (first_input > 1000)
		{
		cout << "Please enter a positve number lower than 1000." << endl;
			cin >> first_input
		}
dwhite459
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Do you know anything about data structures, because this sounds like homework to me. Anyway I'm not giving you code because of the last statement but I will help you understand what you have to do. You're going to want to put all the numbers in vector, then you are going to want to put the first number of that vector in an integer and compare it to the next value that is in the stack, I would use a for loop. I'll give you code right now because it's hard to describe

int compareValue;
int lowestint;
int highestint;
//inside your while loop
for(int i = 1; i < stack.size();i++)//stack is whatever you put your entered values in
{
compareValue = stack[0];
if(compareValue < stack[i])
{
  lowestint = compareValue;
}
else
{
  lowestint = stack[i];
}
if(compareValue > stack[i])
{ 
  highestint = compareValue;
}
else
{
  highestint = stack[i];
}

}

This is a start, I know I said I wouldn't give you code but it just seems easier to give this part to you and make you figure it out. I may be wrong too but that sounds like the pseudocode you should use when writing the program.

At the end of the program you are going to want to have these two lines to show your highest and lowest values shown:

cout << "Highest value is:" << highestint << endl;
cout << "Lowest value is:" << lowestint << endl;
sonsofliberty84
Newbie Poster
12 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

I don't understand why people (sonsofliberty84) insist on difficult solutions to simple problems, but many people like to water their lawn by blowing up the dam.

A simple array, a simple loop, 2 IF statements, and formatted is all you need. No vectors necessary.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

@WaltP

This is what the OP said:
I am not allowed to use arrays and I am not sure which loop to use. Here is what I have so far:

@OP

You could try this:

int min = 1000, max = 0, inputNumber;
 
        do {
                cin >> inputNumber;
 
                if (inputNumber <= min && inputNumber != -1)
                        min = inputNumber;
                
                if (inputNumber >= max)
                        max = inputNumber;
                                                
        } while (inputNumber != -1);
 
        cout << "max:" << max << "\n" << "min:" << min;
myk45
Posting Whiz
319 posts since Sep 2010
Reputation Points: 57
Solved Threads: 40
 

It's not a difficult solution when he said he can't use arrays, try reading the whole message that the OP puts up next time

sonsofliberty84
Newbie Poster
12 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

You're right, I missed the array part. But if he can't use an array, why would a vector be OK? Isn't it just a high-faluting array?

That's like saying if you can't use a loop, use recursion... :icon_rolleyes:

After each input, check to see if it's the highest/lowest so far...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

if you read my post, I said that it is probably homework for a class, because I had a similar problem back when I was in college and I had to use something other than an array. I would use an array personally but when you're told not to use something it sounds like the person is trying to get other people to do their homework/classwork for them when they should just do their own work or ask a classmate.

sonsofliberty84
Newbie Poster
12 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

And if you read my post you'd see that this can be done without storing any numbers at all -- no arrays, vectors, heaps, nothing. Just read and test, read and test....

1) And if I didn't read your post, how would I have known you suggested a vector? :icon_wink:
2) After 4 years and 5000 posts, do you really think I can't tell a homework problem by now? :icon_rolleyes:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: