# Thread: Its "Bugging" me for a week!

1. ## 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. 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

3. 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.

4. Originally Posted by Eman
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. Originally Posted by Greek_89
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.

6. Originally Posted by nimitzhunter
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. ...
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!

8. Originally Posted by Greek_89
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

9. Originally Posted by Greek_89
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?

10. Originally Posted by Eman
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. Originally Posted by nimitzhunter
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!