Thread: Its "Bugging" me for a week!

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    25

    Lightbulb Its "Bugging" me for a week!

    Hi everyone

    Im on the peak to explode! :P

    For a week now i am trying to find an algorithm to get 6 numbers which 3 of them should be odd and the other even..Then determine which one from even is smallest and which one from odd is the smallest..

    So far i can only ask the user to enter 6 numbers(the user should enter the odd/even in any sequence (odd, even, odd, odd etc)..I CANNOT figure out how using the % operator can determine which numbers are odd or even and THEN figuring out which is the smallest from odd and from even!

    I tried A LOT (and i mean A LOT) of combination but nothing!!

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    you should have attempted something at least...
    if n % 2==0 it is even otherwise >=1 it is odd

    EDIT:
    i think it is easier to store the numbers in an array.
    in fact, this may sound wacky
    but why not create 2 arrays, each 3 elements.
    if it is even store in its even array, odd in its odd array. So if statement.
    then using another loop find the smallest numbers from both arrays.

    post the code you tried
    Last edited by Eman; 01-29-2011 at 01:08 PM.
    You ended that sentence with a preposition...Bastard!

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    if ( number%2 == 0 )
       even;
    else 
       odd;
    I guess you want to separate them into two vector, one odd and one even. Then apply bubble sort on these two vectors and sort them from in ascending order, or descending, up to you. Then the first, or last, element is your smallest one.

    EDIT:: I just repeated what Eman said. Eman's idea is better, you don't have to sort the array. But it's still a good practice on sorting.
    Last edited by nimitzhunter; 01-29-2011 at 01:13 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    25
    Quote Originally Posted by Eman View Post
    you should have attempted something at least...
    if n % 2==0 it is even otherwise >=1 it is odd

    EDIT:
    i think it is easier to store the numbers in an array.
    in fact, this may sound wacky
    but why not create 2 arrays, each 3 elements.
    if it is even store in its even array, odd in its odd array. So if statement.
    then using another loop find the smallest numbers from both arrays.

    post the code you tried
    the one with the "if n%2 ==0..." i've tried it already..however i dont know how to proceeed after that..I mean if i have to check for all the 6 numbers to find the lowest odd/even, it will take me more than 10 "if...else if statements".

    I thought about arrays. However the exercise assumes that we use ONLY the mod% operator and "if..else..else if statements"

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Greek_89 View Post
    the one with the "if n%2 ==0..." i've tried it already..however i dont know how to proceeed after that..I mean if i have to check for all the 6 numbers to find the lowest odd/even, it will take me more than 10 "if...else if statements".

    I thought about arrays. However the exercise assumes that we use ONLY the mod% operator and "if..else..else if statements"
    Ok, no array. First you create two variable lowest_even and lowest_odd to store the lowest value of even and odd, or whatever you want to call them
    Code:
    int lowest_even = 100; // using 100 as a sentinel
    int lowest_odd = 100;
    int num;
    Then, use while to read in the number.
    Code:
    while( scanf("%i",&num))
    {
      if ( num % 2 == 0 && num < lowest_even)
           lowest_even = num;
      else if ( num < lowest_odd)
           lowest_odd = num; 
    }
    EDIT:: the sentinel value is no problem if you enter value below it. To work around it, just assign the first even read in to lowest_even, and the first value read in for odd to lowest_odd. Didn't notice that this's in the C++ forum, forget the scanf then and just use cin.
    Last edited by nimitzhunter; 01-29-2011 at 03:50 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    25

    Thumbs up

    Quote Originally Posted by nimitzhunter View Post
    Ok, no array. First you create two variable lowest_even and lowest_odd to store the lowest value of even and odd, or whatever you want to call them
    Code:
    int lowest_even = 100; // using 100 as a sentinel
    int lowest_odd = 100;
    int num;
    Then, use while to read in the number.
    Code:
    while( scanf("%i",&num))
    {
      if ( num % 2 == 0 && num < lowest_even)
           lowest_even = num;
      else if ( num < lowest_odd)
           lowest_odd = num; 
    }
    EDIT:: the sentinel value is no problem if you enter value below it. To work around it, just assign the first even read in to lowest_even, and the first value read in for odd to lowest_odd. Didn't notice that this's in the C++ forum, forget the scanf then and just use cin.
    ok i made it FINALLY. I used a slightly different approach though..take a look at this little piece: "
    Code:
     printf( "\nEnter number 3 : " );
        int num3;
        scanf("%d", &num3 );
        
        int even3;
        int odd3;
        
        //Check whether number is even or odd
        if ( (num3 % 2) == 0 )
        {
           even3 = num3; // Set num3 as even if remainder =0
           if ( (even3 < even) && (even3 < even2) )
           smallest = even3;
        }
        else 
        {
             odd3 = num3;  // Set num3 as odd if remainder != 0
             if ( (odd3 < odd) && (odd3 < odd2) ) // Check if odd2 is smaller than the previous odd...
             smallest_odd = odd3;  // ... and set it as the smallest

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    ...
    s. However the exercise assumes that we use ONLY the mod% operator and "if..else..else if statements"
    doesn't mean you can't challenge yourself. And no you it wouldn't take more than 10 if statements lol

    well since nimitzhunter showed you another way of writing it, it is all cool!
    You ended that sentence with a preposition...Bastard!

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Greek_89 View Post
    ok i made it FINALLY. I used a slightly different approach though..take a look at this little piece: "
    Code:
     printf( "\nEnter number 3 : " );
        int num3;
        scanf("%d", &num3 );
        
        int even3;
        int odd3;
        
        //Check whether number is even or odd
        if ( (num3 % 2) == 0 )
        {
           even3 = num3; // Set num3 as even if remainder =0
           if ( (even3 < even) && (even3 < even2) )
           smallest = even3;
        }
        else 
        {
             odd3 = num3;  // Set num3 as odd if remainder != 0
             if ( (odd3 < odd) && (odd3 < odd2) ) // Check if odd2 is smaller than the previous odd...
             smallest_odd = odd3;  // ... and set it as the smallest
    great..why do you have even and even2? one variable is enough to hold the next smaller value... same with odd. but good job
    You ended that sentence with a preposition...Bastard!

  9. #9
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Greek_89 View Post
    ok i made it FINALLY. I used a slightly different approach though..take a look at this little piece: "
    Code:
     printf( "\nEnter number 3 : " );
        int num3;
        scanf("%d", &num3 );
        
        int even3;
        int odd3;
        
        //Check whether number is even or odd
        if ( (num3 % 2) == 0 )
        {
           even3 = num3; // Set num3 as even if remainder =0
           if ( (even3 < even) && (even3 < even2) )
           smallest = even3;
        }
        else 
        {
             odd3 = num3;  // Set num3 as odd if remainder != 0
             if ( (odd3 < odd) && (odd3 < odd2) ) // Check if odd2 is smaller than the previous odd...
             smallest_odd = odd3;  // ... and set it as the smallest
    good job. It wasn't hard, was it?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    25
    Quote Originally Posted by Eman View Post
    great..why do you have even and even2? one variable is enough to hold the next smaller value... same with odd. but good job
    well even and even2..even-'n' represent each integer. Each integer i assign it as a even or odd..since i want to compare each single one, i give them a unique name

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    25
    Quote Originally Posted by nimitzhunter View Post
    good job. It wasn't hard, was it?
    well to be honest i was kinda tricky cause the whole week i tried more than 8 algorithms and i could get it work well... :O I tried "switch" ... "else..if"..."nested ifs"...but thanx for your help. i appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Day of the week in year program advanced
    By sgreddy in forum C Programming
    Replies: 2
    Last Post: 07-05-2010, 09:50 AM
  2. structural fault any ideas
    By kaijuu in forum C Programming
    Replies: 17
    Last Post: 04-17-2007, 02:43 PM
  3. day of week
    By s_ny33 in forum C Programming
    Replies: 18
    Last Post: 11-02-2005, 11:56 AM
  4. Force end
    By Redattack34 in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 11:16 PM
  5. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM