# simple programme to find the largest of three

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 04-03-2011
jackson6612
Quote:

Originally Posted by C_ntua
The logic is not correct, the reverse is correct.
The rule is when you have integers
Code:

if (INT) = true (for INT !=0)
if (INT) = false (for INT = 0)

so if the "radius of a circle" is 0 it will be false, not true.

When you have
Code:

if (LOGICAL EXPRESSION)
then the compiler just needs to do
Code:

if (LOGICAL EXPRESSION) = true (for LOGICAL EXPRESSION=true)
if (LOGICAL EXPRESSION) = false (for LOGICAL EXPRESSION=false)

Now, I don't see why the compiler will make a logical expression to an integer, then compare that integer with 0 to see if the if-statement is true or not.

The use of the integers is mostly needed in C because there are no boolean type, so when you do something like
Code:

if( trySomething() ) .... ;
then trySomething() will return 0 if it is false or non-zero if it is true and the if-statement will work.

Thanks a lot, C_ntua. It was very kind of you to come up with a detailed reply.

Could you please provide me some short program to check it on my IDE in order to have better understanding?

Regards
Jackson
• 04-05-2011
jackson6612
Hi

I need your advice and opinion. So please help me. In the CODE 1 the largest of three numbers is found and the second CODE 2 also involves the same problem except that now we have four numbers. Don't you think the logic used in CODE 2 more simple and understandable? Any advice. I'm grateful for your guidance and time. (Sorry, if there is problem with the indentation)

CODE 1:

Code:

// program to find the largest of three numbers; assuming all the numbers are distinct

#include <iostream>
#include <conio.h>

using namespace std;

int main()

{
float a, b, c;

cout << "Enter the three numbers" << endl;

cout << "Enter a = ";
cin >> a;

cout << "Enter b = ";
cin >> b;

cout << "Enter c = ";
cin >> c;

if ( (a > b)&&(b > c) )
cout << "a is the largest" << endl;

else if ( (c > b)&&( a > c) )
cout << "a is the largest" << endl;

else if ( (b > a)&&( b > c) )
cout << "b is the largest" << endl;

else if ( (c > a)&&( c > b) )
cout << "c is the largest" << endl;

getch();
}

CODE 2:

Code:

// program to find the largest of four numbers; assuming all the numbers are distinct

#include <iostream>
#include <cstdlib>
#include <windows.h>

using namespace std;

int main()

{

system("cls");

float a, b, c, d;

cout << "Enter the numbers below" << endl;

cout << "a = ";
cin >> a;

cout << "b = ";
cin >> b;

cout << "c = ";
cin >> c;

cout << "d = ";
cin >> d;

{

if ( (a > b) && (a > c) && (a > d) )

cout << "a is the largest" << endl;

else if ( (b > c) && (b > d) ) /* no need to check against "a" */
cout << "b is the largest" << endl;

else if ( (c > d) )
cout << "c is the largest" << endl;

else if ( (d > c) )
cout << "d is the largest" << endl;

else
cout << "Input error" << endl;

}

system("pause");

}

• 04-05-2011
jackson6612
I'm sorry if this is to be considered premature bumping but I need your advice or opinion on the previous a little urgently, so please help me. Thanks.
• 04-05-2011
Mozza314
It looks to me that for code 2 if you have a = 1, b = 2, c = 3, d = 3, then you'll get "Input error". You may indeed consider this an error if you wanted all the numbers to be different, but then if you enter a = 3, b = 3, c = 2, d = 1, it will simply say "b is the largest".

In terms of which logic is more understandable, it seems code 2 is really just an optimised version of code 1 so code 2 would probably be better if it weren't for the caveats I mentioned above.
• 04-06-2011
jackson6612
Quote:

Originally Posted by Mozza314
It looks to me that for code 2 if you have a = 1, b = 2, c = 3, d = 3, then you'll get "Input error". You may indeed consider this an error if you wanted all the numbers to be different, but then if you enter a = 3, b = 3, c = 2, d = 1, it will simply say "b is the largest".

In terms of which logic is more understandable, it seems code 2 is really just an optimised version of code 1 so code 2 would probably be better if it weren't for the caveats I mentioned above.

Hi Mozza

Thanks a lot. I've been learning a lot from you. I hope you would be there to help whenever it's possible for you!

Best wishes
Jackson
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12