Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

What have I done wrong with this statement ?

  • 26-11-2000 08:01PM
    #1
    Moderators, Education Moderators Posts: 1,699 Mod ✭✭✭✭


    if( number1 > (number2 & number3))
    			largest = number1;
    		
    		if( number2 > (number1 & number3))
    			largest = number2;
    		
    		if( number3 > (number1 & number2))
    			largest = number3;
    


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Try this:
    if ((number1 > number2) && (number1 > number3))
       largest = number1;
    
    if ((number2 > number1) && (number2 > number3))
       largest = number2;
    
    if ((number3 > number1) && (number3 > number2))
       largest = number3;
    
    

    But there's a better way to get the largest number.....work it out wink.gif



    [This message has been edited by Enygma (edited 26-11-2000).]


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    Slaanesh, the problem is in your comparison statements. Even though you might think
    number1 > (number2 & number3)
    sees if 'number1 is greater than number2 and number3', this isn't how it works - you can't translate it literally.
    What that statement actually does it compares number1 to the bitwise AND of number2 and number3. When doing comparisons you have to do a comparison for every variable, as Enygma has shown you. However when you have a list of say, 100 numbers to compare, this method obviously becomes unfeasible and then a for-loop is probably your best bet.


  • Registered Users, Registered Users 2, Paid Member Posts: 4,538 ✭✭✭Gerry


    Have they not shown you loops yet over in blanch? That was the first thing we learned.
    Unless you are just looking for the max of 2 numbers, your method is going to be longer and annoying. It would go something like:

    void main(void)
    {
    int array[10] = {1,2,3,4,5,6,7,8,9,10};
    int largest=0;
    largest=array[0];
    for(int i = 1;i<sizeOfArray; i++)
    {
    if array>largest;
    largest=array;
    }
    }



Advertisement