inputting numbers while maintaining the largest and smallest throughout

Write your question here.

hi guys, I bought this book C++ programming: principles and practice using c++.

now, ive been trying to work through the drills and currently on chapter 4(Drill # 6) ive run into a snag.

it wants me to use one 'Double' as an input while have two other variables to maintain the smallest and the largest numbers.

at best, I think it's a good idea to use an array... .

I'm sorry, but this is all I have so far. I really am completely lost as to how to work my way through this one. if anyone could help. It would be very appreciated. Im not necessarily looking to just outright be given a code, but hints and tips would do nicely.

I know this code is garbage, but I feel it would at least give insight into the way im going about 'THINKING' about the problem at hand.

thanks guys


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
#include<iostream>
#include<vector>
#include<string>
#include<cmath>

using namespace std;

int main()
{
	double list[15], smaller, larger;

	cout << "Please enter a value for x: " << endl;

	for (int i = 0; i < 15; i++)
	{
		cin >> list[i];

		if (list[i - 1]> list[i])
		{
			cout << list[i - 1] << " is larger than " << list[i];
			cout << endl;
		}
	}

}
You might want to think about what you would initialize smaller andlarger to and how and when you would change them in the loop.

When you've thought about that, it might make sense to get the first value before the for loop begins.
Well, you do not need the array. Actually, you cannot use one: you do not know how many values user is going to enter. What if he want to enter 1010 numbers? YOu are supposed to accept numbers until | is entered.

Also you do not need to compare entered value with previous. YOu need to compare it with largest and smallest so far if it is larger/smaller, set corresponding value to entered number and display text describing it.
Do you mean as in swapping the numbers?

I did a bubble sort that was a loop inside of a loop and I swapped

if(a>b)
place_holder = a;
a=b;
b= place_holder;

??
You don't need an array. You need three variables:
1. Temporary to hold the latest input.
2. Smallest to remember the smallest value that has been seen so far.
3. Largest to remember the largest value that has been seen so far.

At start no values have been seen yet.
The smallest should thus be at least as large as any input can possibly be.
The largest should thus be at least as small as any input can possibly be.
See http://www.cplusplus.com/reference/limits/numeric_limits/

For each input value:
* If the new value is smaller than smallest, update the smallest.
* If the new value is larger than largest, update the largest.

After input is over, show smallest and largest.


PS.
Your current code includes headers that you don't need. Guideline is to include only the necessary headers.

It is better not to make a habit of using namespace std;. The standard library is in a namespace to reduce name conflicts and that line undoes all that work.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main() {
  using std::cout;
  using std::cin;
  using std::endl;

  // your code

  return 0;
}
Wow! thanks guys, I was finally able to finish it up!!

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>
#include<limits>

using namespace std;


int main()
{
	double smallest = numeric_limits<double>::max();
	double current;
	double largest = numeric_limits<double>::min();

	cout << "Please enter a number" << endl;

	while (cin >> current)
	{
		if (current < smallest)
		{
			smallest = current;
						
		}

		else if (current > largest)
		{
			largest = current;
		}

		cout << smallest << " is the smallest number. " << current << " is the current number. " << largest << " is the largest number" << endl;
	}

	
} 



unfortunately, it wasn't completely obvious to me that I should set my smallest to a large number, and conversely, my largest to a small number. It is somewhat counter-intuitive. My follow up question would be: Is this something that gets learned along the way? Or, in programming is it a case of "The more you see, the more you know"?

Last edited on
The smallest needs to be set to the highest value possible in case the series of numbers are all over the 1000 you have set.
Probably it is the case that the more you program the easier it becomes to find the bugs.
There is the issue of software used for critical applications such as the complex software controlling a modern aircraft where you need good programming design and coding practices to reduce the likelihood of their being a fatal bug.


Last edited on
Logic.

Lets initialize the smallest to X.
The first input is Y and X<Y. Therefore, the smallest does not change.
That is wrong. The Y is the only value that exists (so far) and thus by defition is the smallest. However, our variable smallest holds value X, which is not in the input.

If you do know for sure that every input value is <= X, then it is ok to use X. If.

It is better to play safe and use
auto smallest = std::numeric_limits<double>::max();


There is another method too:
Treat the first input differently. Store it directly to both smallest and largest. Then use your conditional code with the rest of input.
@ Keskiverto. Thank you, I truly appreciate all the help you've given me.

what are the "::" before the max/min?
Last edited on
Did you look at the http://www.cplusplus.com/reference/limits/numeric_limits/

The numeric_limits is a template class.
It is in namespace std.
The class has static member function max().

The operator :: is scope qualifier. See, for example, https://msdn.microsoft.com/en-us/library/b451xz31.aspx
Topic archived. No new replies allowed.