Thread: simple programme to find the largest of three

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by C_ntua View Post
    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
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    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");
        
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    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.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #19
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    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.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Mozza314 View Post
    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
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple error i can't find /code
    By Alecroy in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2010, 01:00 PM
  2. Find last largest integer in array?
    By schmidtc in forum C Programming
    Replies: 2
    Last Post: 07-05-2010, 01:00 PM
  3. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM
  4. Replies: 5
    Last Post: 04-16-2004, 01:29 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM