Hey guys, if someone can help me on this one, I'd be thankful. The thing is that this is a sample work. I gotta do the same thing but with 4 variables, organize them according to the correct order, from lowest to greatest or viceversa, it doesn't matter. And I can't use logical operators, that's another thing. Hope you get what I'm trying to do, here's the code:

``````#include <iostream>
#include <string>

using namespace std;

int main(){
int x, y, z;
int max, mid, min;

cin>>x>>y>>z;

if(x>y){
if(x>z){
max = x;
if(y>z){
mid = y;
min = z;
}else{
mid = z;
min = y;
}
}else{
//x>y, z>x, z>y
max = z;
mid = x;
min = y;

}
}else{
//y>x
if(y>z){
max = y;
if(x>z){
mid = x;
min = z;
}else{
mid = z;
min = x;
}
}else{
//y>x, z>y, z>x
max = z;
mid = y;
min = x;
}
}

cout<<"Max: "<<max<<endl;
cout<<"Mid: "<<mid<<endl;
cout<<"Min: "<<min<<endl;

int stop;
cin>>stop;
return 0;
}``````
3
Contributors
11
Replies
12
Views
8 Years
Discussion Span
Last Post by VernonDozier

Does it have to have the nested if-else statements or can you just use a vector to store all the ints in and then sort the vector?

Yes, it must have the nested if stataments because in that part is where we are working with!

Any single if statement using the && operator can be turned into a nested if statement.

``````bool A, B;

if (A && B)
{
// code to execute if A && B
}

// equivalent to above
if (A)
{
if (B)
{
// code to execute if A && B
}
}``````

Assigning the result to another variable can be useful sometimes.

``````bool A, B, AandB;

AandB = false;
if (A && B)
{
AandB = true;
}

// equivalent to above
AandB = false;
if (A)
{
if (B)
{
AandB = true;
}
}``````

Then you can save that result and do an else that would be a bit more complicated with the nested if.

``````if (AandB)
{
// code to execute if A && B true
}
else
{
// code to execute if A or B is false.
}``````

The || operator is similar.

Edited by VernonDozier: n/a

He said you can't use any logical operators.

He said you can't use any logical operators.

These aren't logical operators. These are the nested-if equivalent to logical operators, but in the alternative code, neither || nor && is used in the code.

Yes, that's right...I know what toy are trying to tell me, thanx. The thing is that I can't use those logical operator whatsoever...there is where I'm confused...

I did this with using the numbers 1 2 3 4 for testing. You can merg all of this into 1 if "tree" if you want but I was getting confused enough when working this out.

``````#include <iostream>

using namespace std;

int main()
{
int n1, n2, n3, n4; //n for number
int o1, o2, o3, o4; //o for order

cout << "Input number 1 of 4: ";
cin >> n1;
cout << "Input number 2 of 4: ";
cin >> n2;
cout << "Input number 3 of 4: ";
cin >> n3;
cout << "Input number 4 of 4: ";
cin >> n4;

if( n1 >= n2 )
{
if( n1 >= n3 )
{
if( n1 >= n4 )
{
o1 = n1;
if( n2 >= n3 )
{
if( n2 >= n4 )
{
o2 = n2;
if( n3 >= n4 )
{
o3 = n3;
o4 = n4;
}
else
{
o3 = n4;
o4 = n3;
}
}
}
else if( n2 < n3 )
{
if( n3 >= n4 )
{
o2 = n3;
if( n2 >= n4 )
{
o3 = n2;
o4 = n4;
}
else
{
o3 = n4;
o4 = n2;
}
}
}
else if( n2 < n4 )
{
if( n4 >= n3 )
{
o2 = n4;
if( n2 >= n3 )
{
o3 = n2;
o4 = n3;
}
else
{
o3 = n3;
o4 = n2;
}
}
}
}
}
}
if( n1 < n2 )
{
if( n1 >= n3 )
{
if( n1 >= n4 )
{
o1 = n2;
o2 = n1;
if( n3 >= n4 )
{
o3 = n3;
o4 = n4;
}
else
{
o3 = n4;
o4 = n3;
}
}
}
}
if( n1 < n2 )
{
if( n1 < n3 )
{
if( n1 >= n4 )
{
o3 = n1;
o4 = n4;
if( n2 >= n3 )
{
o1 = n2;
o2 = n3;
}
else
{
o1 = n3;
o2 = n2;
}
}
}
}
if( n1 < n2 )
{
if( n1 < n3 )
{
if( n1 < n4 )
{
o4 = n1;
if( n2 >= n3 )
{
if( n2 >= n4 )
{
o1 = n2;
if( n3 >= n4 )
{
o2 = n3;
o3 = n4;
}
else
{
o2 = n4;
o3 = n3;
}
}
else
{
o1 = n4;
if( n3 >= n2 )
{
o2 = n3;
o3 = n2;
}
else
{
o2 = n2;
o3 = n3;
}
}
}
else
{
if( n3 >= n4 )
{
o1 = n3;
if( n2 >= n4 )
{
o2 = n2;
o3 = n4;
}
else
{
o2 = n4;
o3 = n2;
}
}
else
{
o1 = n4;
o2 = n3;
o3 = n2;
}
}
}
}
}

cout << o1 << " " << o2 << " " << o3 << " " << o4 << endl;

system("PAUSE");
return 0;
}``````

Wow, that's a really long code, but I'm really thankful. It worked!
They are organize in a way that the output is from the greatest to the lowest number, but what happens if the user wants to input those numbers in different order like, 2, 4, 1, 3 ? the output then would be 1 2 3 4 or 4 3 2 1. That is what I don't I haven't been able to do that....Do you understand ?

It worked!

??? Did you try it out with a variety of input? 4 1 2 3 for example? You even mention that it doesn't work for 2 4 1 3.

The code has some flaws, including some unreachable statements. For example, lines 60 to 76 can be deleted with no effect because either the condition on line 26 will be true or the condition on line 43 will be true, so the if-else if-else if block from lines 26 to 76 will never get beyond the second code block and execute the third. Stick a cout statement after line 61 and see if you can get it to display. It won't, no matter what input you give it.

Lines 100 to 102 can be deleted. The if condition on line 101 is the same as the previous if condition on line 80. Both blocks of code will execute or neither will, so you don't need the second test. Same with lines 121 to 123 for the same reason. Line 80 can be replaced with the word "else" since either the condition on line 19 will be true and the condition on line 80 will be false or vice versa.

There are a few ways to do this problem. You should spend a good half hour sitting down with pencil and paper going over this. Think of it as a 20 Questions game. Your friend picks four numbers, doesn't show you what they are and you can ask yes or no questions. From the answers you get, you decide what the next question is. Ask the right questions in an orderly fashion and you'll figure out what the order is. This is an algorithm problem before it becomes a programming problem. Venn diagrams could come in handy possibly. Once you get the method done, the coding is not complex, but it might be tedious and you need to be very detail oriented.

Edited by VernonDozier: n/a

First of all the output will always be greatest to smallest.
Second I didn't spend a whole lot of time taking out the repeat conditions and I mentioned on the post that he would have to do some merging if he wanted it to look cleaner.

First of all the output will always be greatest to smallest.
Second I didn't spend a whole lot of time taking out the repeat conditions and I mentioned on the post that he would have to do some merging if he wanted it to look cleaner.

No, the output will not always be greatest to smallest. Enter 4 1 2 3 and see what you get. You'll get 4, then 3 random values because o2, o3, and o4 will never be initialized. It has nothing to do with the code being unclean. The code simply does not work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.