I want to write a program that prompt the user enter a number continuously, until the user enter zero or a negative number.And how can I find out the smallest & second largest number?Can I use this method???How can I distinguish the number?
I just write some code.
1 2 3 4 5 6 7 8
int num1;int low;int smallest;
do{
cout << "Please input a number(Use a negative number or zero to stop): ";
cin >> num1;
smallest = num1;
cout << "The smallest number is: " << smallest;
}while(num1>0);
get number
while(number is not less than zero or not zero)
push number to vector
get another number
sort vector
size of vector = n
if(n > 0)
smallest = vector[0]
largest = vector[n-1]
if(n >2)
secondlargest = vector[n-2];
else
secondlargest = vectorr[0]
shadowCODE's pseudocode stores all values and sorts them. That is simple (because it delegates all size comparisons to the sort algorithm).
The other approach, without store&sort:
1. Lets start so that "smallest" holds the "smallest value that has been seen so far".
(If no values has been seen yet, then smallest is as large as possible.)
2. Get a value. IF it is smaller than smallest, THEN update smallest.
3. Repeat step 2.
Largest value is independent from the smallest. You should get proper code for these first.
IF new value is smaller than smallest-so-far
THEN we know that smallest-so-far was smaller than all the rest but larger than the new value.
That is "secondsmallest", is it not?
ELSE IF new value is larger than smallest-so-far but smaller than secondsmallest-so-far
THEN the new value should be secondsmallest
However, consider input "1 5 1". Who is the secondsmallest there? Is it 1 or 5?
However, consider input "1 5 1". Who is the secondsmallest there? Is it 1 or 5
Also, 3,3,2 posed a threat.
That is the reason why i choosed to store all values.
“There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”
- C.A.R. Hoare
@davidc626, in case of, only the user decides when to stop (he doesn't know before inputting 1st number) - you cannot use array or dynamically allocated memory (you can use d.a.m. if user knows total number of input before inputting 1st number). so you have to use any STL container such as vector.
you could use this
-while loop to stop when the user enter 0 or negative numbers, and for counter
-array, counter would be the value of array length
-for loop , to loop the array and find the number you want
int num;int low;int smallest;int number;
cout << "Please input a number(Use a negative number or zero to stop): ";
cin >> num;
smallest=num;
do{
cout << "Please input a number(Use a negative number or zero to stop): ";
cin >> number;
if(number<smallest && number>0){
smallest=number;}
}while(number>0);
cout << "The smallest number is: " << smallest;
Is this no problem???
Also,how to find the second largest?Someone said largest value is independent from the smallest???How?
Now,I can find out the smallest and largest numbers.
int num;int smallest;int number;int largest;int second_largest;
cout << "Please input a number(Use a negative number or zero to stop): ";
cin >> num;
smallest=num;
do{
cout << "Please input a number(Use a negative number or zero to stop): ";
cin >> number;
if(number<smallest && number>0){
smallest=number;}
else if(number>smallest){
largest=number;}
}while(number>0);
cout << "The smallest number is: " << smallest << endl;
cout << "The largest number is: " << largest;