Okay, I got abotu 5 problems solved today and I am on the hardest part! Basically my code ask the user to enter series of random numbers and then use sentinel to mark the end of numbers needed to be entered. Now I want to be able to tell my user the largest and the smallest number she/he entered. Is there a function that can do this? like a sort or something? Here's my code:

//This program lets the user enter series of random numbers
//Enter -99 to end the series
//Display the largest and smallest number entered
#include <iostream>
using namespace std;

int main ()
{
//declare variables
	int numCount = 1; // number counter
	int numbers; //to hold the numbers entered
	int numAccum; // accumulator

//ask for number inputs
	cout << "Please enter random numbers\n";
	cout << "I will sort them out and tell you\n";
	cout << "the largest and the smallest number you entered\n";
	cout << "When you are done just enter -99 so I can process your inputs\n\n";
	cout << "Enter random number " << numCount << ": ";
	cin >> numbers;

//enter the sentinel -99 to end the number inputs

	while (numbers !=-99)
	{
		numAccum += numbers;
		numCount++;
		cout << "Enter random number " <<numCount << ": ";
		cin >> numbers;
	}

//sort out the numbers and pull out the largest and the smallest
	
	cout << "The largest number is: \n";
         WHAT FUNCTION DO I USE HERE??? OR ANY jumpstart on looping this?



return 0;
}

To get the largest and mimimum #, we could create/use a Max() function after we've declared/initialized i & count to zero:

int Max()
{
int largest = Numbers[0];
for(int i = 0; i < count; i++)
if( largest < Numbers[i] )
largest = Numbers[i];
return largest;
}

This is comparing through an array, but you could also other 'FOR' loops to accomplish what you want.

The chapter I am right now does not cover array so I dont think I am able to use arrays with this. :(

So check the number currently being input against the current extrimum.

Gosh I am so sorry, what do you mean by extrimum?

Ok, since you haven't completed arrays yet, try something like this:

cout<<"Max of 2 and 1 is: "<<max(2,1)<<endl;
cout<<max<<endl:

The above max function should print(cout) 2. A link that you wanna keep in mind(will help you later on) is: http://msdn2.microsoft.com/en-us/library/wh15hsex(VS.71).aspx.
Another way to see your highest valie from a range done:

#include <iostream>

using namespace std;

int main()
{
    
    const int MAX_NUM_INTEGERS = 10;
    long int integers[10];
    long int i = 0, maximumValue = 0;

    cout << "Please enter 10 integers : \n\n";

    while ( i < MAX_NUM_INTEGERS)
    {
        cin >> integers[i];
        i++;
    
        if (integers[i - 1] > maximumValue)
        {
            maximumValue = integers[i-1];
        }
    }

    cout << "\nThe largest number you have entered is " << maximumValue << "\n\n";

    system("pause");
    return 0;
}

See if you can find out whats going on-that program above required user input of 10 integers and find the maxinum of the numbers. See how you can adopt it to your specific program.[vietbong87]

that code might work if we set a specific number of random integers to compare. What if we let the user to enter as many integers as we want? or only 5 numbers? We can't set a constant variable with this am I right?

So do u want to limit the number of inputs that that the user can do? You can choose either option.

this is not working at all. the user should be able to enter as many numbers as he wants and then enter a SENTINEL when he is done. Then the program should look through the numbers he entered and come back and tell him what is the highest number he entered and what is the lowest number he entered.

Gosh I am so sorry, what do you mean by extrimum?

The min and the max. Two variables you set to something initially -- perhaps system limits or the first element of an array when an array is used -- that you check each subsequent entry against.

Ughh, I dont get it. I have a handful of numbers that the user entered sitting inside the variable numbers. Now, I need to be able to see those numbers and assign the big one to maxValue. But this code doesn't even attempt to assign anything on maxValue! why?

//declare variables
	int numCount = 1;		// number accumulator
	int numberEntered;      //to hold the numbers entered
	int minValue;
	int maxValue;

//ask for number inputs
	cout << "Please enter random numbers\n";
	cout << "I will sort them out and tell you\n";
	cout << "the largest and the smallest number you entered\n";
	cout << "When you are done just enter -99 so I can process your inputs\n\n";

//enter the sentinel -99 to end the number inputs

	while (numberEntered !=-99)
	{
		numCount += numberEntered;
		numCount++;
		cout << "Enter random number: ";
		cin >> numberEntered;
	}
		numCount = maxValue;	
		cout << "Big number: " << maxValue << endl;

Ughh, I dont get it. I have a handful of numbers that the user entered sitting inside the variable numbers. Now, I need to be able to see those numbers and assign the big one to maxValue. But this code doesn't even attempt to assign anything on maxValue! why?

//declare variables
	int numCount = 1;		// number accumulator
	int numberEntered;      //to hold the numbers entered
	int minValue;
	int maxValue;

//ask for number inputs
	cout << "Please enter random numbers\n";
	cout << "I will sort them out and tell you\n";
	cout << "the largest and the smallest number you entered\n";
	cout << "When you are done just enter -99 so I can process your inputs\n\n";

//enter the sentinel -99 to end the number inputs

	while (numberEntered !=-99)
	{
		numCount += numberEntered;
		numCount++;
		cout << "Enter random number: ";
		cin >> numberEntered;
	}
		numCount = maxValue;	
		cout << "Big number: " << maxValue << endl;

Where do you see anywhere where you've assigned anything to it?

You declare it, and it is not assigned anything so its value is undefined. Later you assign this undefined value to NumCount. And then you print the undefined value.

hmm...u dont know the arrays part...
i think u cn try this..

[B]int min, max;[/B]
                cout << "Enter random number " << numCount << ": ";
	cin >> numbers;
[B]min=numbers;
max=numbers;[/B]

	while (numbers !=-99)
	{
		numAccum += numbers;
		numCount++;
		cout << "Enter random number " <<numCount << ": ";
		cin >> numbers;
                                [B]if(numbers>max)
                                       max=numbers;
                                if(numbers=-99)
                                   break;
                                if(numbers<min)
                                   min=numbers;[/B]
	}

[B]cout<<"Maximum value"<<max;
cout<<"Minimum value"<<min;[/B]

hope dis is of sum help

Dave Sinkula gave you the best advice, but with the fewest words.

What he meant is that there are only two numbers you need to remember every time the user enters a number: the smallest entered so far and the largest entered so far. Hence:

#include <cstdlib>  // the atoi() function
#include <limits.h> // INT_MAX and INT_MIN

int smallest = INT_MAX;
int largest  = INT_MIN;
int i;
std::string s;

while (true) {
  std::cin >> s;
  if (s.empty()) break;
  i = std::atoi( s.c_str() );

  if (i < smallest) smallest = i;
  if (i > largest)  largest  = i;
  }

std::cout << "The smallest number you entered is " << smallest << ".\n";
std::cout << "The largest number you entered is "  << largest  << "." << std::endl;

The smallest starts out with the largest possible int value, so whatever you enter as the first number is going to be smaller than or equal to it.

Hope this helps.

<edited to use simpler code>

sorry i did sum mistake in d above program...
below is the corrected one n its working perfectly...

int numCount = 1; 
int numbers; 
	
cout << "Please enter random numbers\n";
cout << "I will sort them out and tell you\n";
cout << "the largest and the smallest number you entered\n";
cout << "When you are done just enter -99 so I can process your inputs\n\n";

int min, max;
cout << "Enter random number " << numCount << ": ";
cin >> numbers;
min=numbers;
max=numbers;

while (numbers !=-99)
{
	numCount++;
	cout << "Enter random number " <<numCount << ": ";
	cin >> numbers;
                if(numbers>max)
                         max=numbers;
	if(numbers==-99)
                         break;
                if(numbers<min)
                         min=numbers;
}

cout<<"Maximum value: "<<max;
cout<<"\nMinimum value: "<<min;

i hope it is of sum help...!!!

hmm...u dont know the arrays part...
i think u cn try this..
. . .
hope dis is of sum help

sorry i did sum mistake in d above program...
below is the corrected one n its working perfectly...
. . .
i hope it is of sum help...!!!

Trace, two things:
1) Read this so people can understand you
2) If you give people the answers to their homework, they learn nothing. Please help them find their own answers.

thats true that people doesn't learn if you feed them the code - ONLY if they are not serious about learning it and just want to pass the course. In my case, this homework deadline was first week of October. Up to now I still don't get it and I am still trying to get a grip of it. I spend 4 to 6 hours a day learning C++. Reading the books over and over again. The prolem is, the book says so much and doesn't show it in action (that is code samples). People will just possibly quit and give up especially for someone like me that never encountered <iostream> in my english vocabulary - if you know what I mean:). But if you start something up where they could sit there and analyze the code, then try to understand, then as we go we could refer back to the code and figure out what did I do wrong and learn from there. To me anyway thats how I learn. :=)

So why did Trace put a break; on line 23? Also, why did you use == on line 22? Why can't we not use = for assigning -99 to it? I dont understand those part.

I am glad i found this forum by the way:) There is so much to learn and read here.. thank you

Tracethepath's code is a little unorganized. He is using the number -99 as the SENTINEL code to tell the program you have finished entering numbers. So if the number is -99 he breaks out of the loop. The only problem with that is that you cannot enter the number -99 as part of your list.

In my code, I goofed. I am using the empty string as the SENTINEL, but the code that reads input should be

std::getline( std::cin, s );

That way, the user just enters numbers, and when he is done he just presses ENTER once more.

Hope this helps.

Hi Duos. Thanks for the input but I don't know what your header means. We are not on that part yet and all I have gone so far with the class and books are the very basic like loops, if else, very few functions.

while (true) {
std::cin >> s;
if (s.empty()) break;
i = std::atoi( s.c_str() );

that code, I would not know how to read it.

Heh, sorry to be confusing. (I'm starting to be a little disappointed with some of the C++ classes people are taking.)

The #include <iostream> statement means that the compiler sticks all the code and definitions and stuff in the "iostream" file into your program. That way you can use cin and cout.

I used <limits.h> just because it defines the largest and smallest possible numbers. If you want, get rid of the line that includes the file and replace INT_MAX with some really, really big number, and INT_MIN with some really, really negative number.

If you haven't learned about converting between int and std::string yet then use tracethepath's method of using some specific number as the SENTINEL. It doesn't have to be -99 or even the same number every time --you could ask the user for a number to use as the SENTINEL.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.