Determining largest and smallest number from a loop

Without using arrays or the for function, how would I be able to determine the largest and smallest numbers from a loop? I used -99 as a sentimental and a while loop to allow the user to enter his/her desired numbers.
here i made a code for it. i was to lazy to start doing stuff with stringstreams so if u want that add it yourself.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream> 
using namespace std;
int getmax(int a,int b) {
	int x;
	x=a>b?a:b;
	return (x);
}
int main(){
	bool isset(false);
	int i(1),number1,number2;
	int amount;
	cout<< "how many numbers do you want to enter ? ";
	cin>>amount;
	while(i<=amount) {
		if(i==1) {
			cout<<"enter the "<<i<<"st number ";
			if (isset==false) {
			cin>>number1;
			isset=true;
			}
		    i=i+1;
			cout<<"enter the "<<i<<"nd number ";
		}
		else if (i==3){
			cout<<"enter the "<<i<<"rd number ";
		}
		else if(i>3) {
			cout<<"enter the "<<i<<"th number ";
		}
		cin>>number2;
		number1=getmax(number1,number2);
		i=i+1;
	}
	cout<<"the highest number is " <<number1<<endl;
	system("pause");
	return 0;
}

edit: a for loop works the same as an while loop
for example:
1
2
3
4
for (i=0;i<5;i++)
//is equivalent to
i=0;
while(i<5) {i=i+1;}
Tsk tsk, handing out answers isn't really our thing. I have a feeling this was a homework assignment, and even if it wasn't, what did he learn from this?
he learned from my edit ;d
and i like solving tasks like this.
Last edited on
He likely learned nothing actually. Copy pasta code helps nobody
Well, I suppose the posted code could be educational if someone went through and sorted it out. As posted, it does need quite a bit of tidying up.

#1 As STL is being used, getmax() is not needed as std::max (and std::min) are already available.

http://www.cplusplus.com/reference/algorithm/max/
http://www.cplusplus.com/reference/algorithm/min/

#2 The variables should have better names, to say what they do: e.g. number1 should be max_value, etc.

amount -> count/num_entries

#3 The variables should all be initialized. This is a good habit to get into, as it makes debugging a lot easier. It also allows you to build at max warning level so you can spot those variables which need to be initialized.

#4 The loop logic can be simplified

- you don't have to special case index 1 to set the min and max. If you include <climits>, you can set the values to INT_MAX and INT_MIN (if the min value is init to MAX_INT, then you know the first i/p will always be less than or equal to it)

http://www.cplusplus.com/reference/clibrary/climits/

- or ever better, use the corresponding C++ mechanism: numeric_limits

http://www.cplusplus.com/reference/std/limits/numeric_limits/

- the need for the isset test should vanish in the refactoring.

#5 The code to get the number prefix (-st, -nd, ...) could (and should) be factored out.

#6 The code needs comments, esp. as it's an exercise. Not pointless ones, just restating what the function and variable names should make clear. But ones which call out the structure of the code.

#7 And I would space it out a bit to make the structure a bit clearer. Esp. as it's an exercise. Inc. following the advice of most programming texts, and putting a space either side of all operators to improve readability (not eveyone buys into this last point).

#8 And for Brownie points, you could avoid the "evil" (platform specific/dangerous to use/...) system call.
Last edited on
Rolling all my suggestions into the code, you end up with the following. Note that this code would (should) prob be a bit suspicious to the tutor of a C++ beginners class!

My point is that you should not nick code from forums unthinkingly. I would hope my code would get top marks! And the code about rather less, due to the reinvention of standard functionality, the missed opportunity to factor, poor variable names, etc.

Also, the OP mentioned using a sentinal, rather than a count.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// minmax
//
// Output the minimum and maximum values input.by a user.

#include <iostream> // standard i/o routines
#include <limits>   // for numeric_limits
using namespace std;

// Function prototypes
const char* get_number_prefix(int value);
void wait_for_user();

// Program entry-point
int main() {
    // Get number of values the user wants to input
    int value_count = 0;
    cout << "How many numbers do you want to enter? ";
    cin >> value_count;
    cout << endl;

    // Initialize the min and max functions so that the initial
    // input must always satify the test conditions.
    int min_value = numeric_limits<int>::max();
    int max_value = numeric_limits<int>::min();

    // Read the values, keeping track of which are the minimum
    // and maximum.
    int index = 1;
    while(index <= value_count) {
        cout << "Enter the " << index << get_number_prefix(index)
             << " number : ";
        int value = 0;
        cin >> value;
        min_value = min(min_value, value);
        max_value = max(max_value, value);
        ++index;
    }
    cout << endl;

    // Display minimum and maximum values.
    cout << "The lowest number is " << min_value << endl;
    cout << "The highest number is " << max_value << endl;
    cout << endl;

    // Stop console from closing till user has read output.
    wait_for_user();

    return 0;
}

// Get number prefix for value
const char* get_number_prefix(int value) {
    switch(value) {
        case 1: return "st";
        case 2: return "nd";
        case 3: return "rd";
        default: { /* other cases use default */ }
    }

    return "th";
}

// Wait for user
void wait_for_user() {
    cout << "Press ENTER to continue...";
    // clear input buffer, so ignore does not read eol from
    // earlier input
    cin.sync();
    // read (and ignore) up to first eol.
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
Last edited on
One possible realization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include   <iostream>

int main()
{
   bool empty = true;
   int max, min, value;

   while ( std::cin >> value )
   {
      // here a display of entered value can be inserted if needed
      if ( empty )
      {
         empty = false;
         max = min = value;
      }
      else
      {
         if ( max < value ) max = value;
         if ( value < min ) min = value;
      }
   }

   if ( !empty )
   {
      std::cout << "maximum = " << max
                    << ", and minimum = " << min << std::endl;
   }
   else
   {
      std::cout << "The sequence is empty" << std::endl;
   }

   return ( 0 );
}


i
Topic archived. No new replies allowed.