Anyone can solve this problem?

Pages: 12
This is a problem that I got from class

Requirements
Ask the user to enter six numbers. Three of the numbers will be odd and three of the numbers will be even. The numbers can be entered in any sequence. Write a program that identifies the smallest number within each odd/even category.

Example run
5
2
4
7
9
6

The smallest even number is 2
The smallest odd number is 5

Assumptions:
1) The user will enter three odd numbers and three even numbers. We just don’t know in what sequence it will be entered.
2) The user will not enter zero because it is neither odd nor even
3) The user can enter negative or positive numbers

Use only what we have covered in class (Nothing more !)
Use the mod operator (%) to determine whether a number is odd or even)

cool, i will try this one and post back :)
We're not here to do your homework for you. Show us what you've done so far or go study.
This probably so easy and you're not thinking into it.

Here's some hints:
*NUMBER % 2 will return 0 or 1. guess which numbers returns a 1.
*You don't need to store anything but the smallest odd or even. So no need to worry about more than 3 variables.
*Positive or negative is not a catch or a problem. Negatives are just smaller numbers. It just warns you to not use unsigned int.
* Remember if else statements and do some comparison with the less than operator (<)
Hi, how about this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

int main()
{
    // parameters
    int numbers[6];
    int smallestOddNumber=-1;
    int smallestEvenNumber=-1;

    // read parameters
    cout << "enter 6 numbers (3 odd and 3 even ones all separated by spaces) ";
    cin >> numbers[0] >> numbers[1] >> numbers[2] >> numbers[3] >> numbers[4] >> numbers[5];

    // calculate result
    for(int i=0; i<6; i++)// initialize parameteres
    {
        if( numbers[ i ] %2 == 0 )
            smallestEvenNumber = numbers[ i ];
        else
            smallestOddNumber = numbers[ i ];
    }
    for(int i=0; i<6; i++)// find smallest odd and smallest even numbers
    {
        if( numbers[ i ] %2 == 0 && numbers[ i ] < smallestEvenNumber )
            smallestEvenNumber = numbers[ i ];
        else if( numbers[ i ] %2 == 1 && numbers[ i ] < smallestOddNumber )
            smallestOddNumber = numbers[ i ];
    }

    // show result
    cout << "smallest odd number is " << smallestOddNumber << endl;
    cout << "smallest even number is " << smallestEvenNumber << endl;

    while( true);// wait...
}


and here is a longer version - basically the same but putting some stuff in extra methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include <iostream>
using namespace std;

bool isOdd(int n)
{
    return n % 2 == 1;
}
bool isEven(int n)
{
    return n % 2 == 0;
}
int someEvenNumberFrom(int numbers[], int size)
{
    for(int i=0; i<size; i++)
        if(isEven( numbers[i] ))
            return numbers[i];
    throw -1;
}
int someOddNumberFrom(int numbers[], int size)
{
    for(int i=0; i<size; i++)
        if(isOdd( numbers[i] ))
            return numbers[i];
    throw -1;
}
int smallestEvenNumberFrom(int numbers[], int size)
{
    int smallestEvenNumber = someEvenNumberFrom(  numbers, size );
    for(int i=0; i<size; i++)
        if(isEven( numbers[i] ) && numbers[i] < smallestEvenNumber )
           smallestEvenNumber = numbers[i];
    return smallestEvenNumber;
}
int smallestOddNumberFrom(int numbers[], int size)
{
    int smallestOddNumber = someOddNumberFrom( numbers, size );
    for(int i=0; i<size; i++)
        if(isOdd( numbers[i] ) && numbers[i] < smallestOddNumber )
           smallestOddNumber = numbers[i];
    return smallestOddNumber;
}

int main()
{
    // parameters
    int numbers[6];

    // read parameters
    cout << "enter 6 numbers (3 even and three odd ones all separated by space) ";
    cin >> numbers[0] >> numbers[1] >> numbers[2] >> numbers[3] >> numbers[4] >> numbers[5];

    // calculate and show result
    cout << "smallest odd number is " << smallestOddNumberFrom( numbers, 6 ) << endl;
    cout << "smallest even number is " << smallestEvenNumberFrom( numbers, 6 ) << endl;

    while( true);// wait...
}
Last edited on
my version... idk... i could refine and make it more concise.... but that was time consuming :( haha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using std::cout;
using std::endl;
int even[3];
int odd[3];
void sort (int * p);
void crunch(int * p);
int main()
{
	int choice [6];
	
	
	for (int i = 0; i < 6; i++)
	{
		cout << "#: ";
		std::cin >> choice[i];
	} 

	crunch(choice);
	sort(even);
	sort(odd);
	return 0;	
}
	
void crunch( int * p)
{

	
	for ( int i=0; i < 6; i++)
	{
		
		if ( *(p+i) % 2 == 0)
			if (i < 3)
			even[i] =*(p+i);
				else 
				even[i-3] = *(p+i);
			else 
			{
			if ( i < 3)
			odd[i] = *(p+i);
			else
				odd[i-3] = *(p+i);	
			}

			
				
	}
}

void sort(int * p)
{
int g;
for( int i = 0; i < 1; i++)
{
	for (int ii = 1; ii < 3; ii++)
	{

	if (*(p+i) > *(p+ii))
	{
		g = *(p+i);
		*(p+i) = *(p+ii);
		*(p+ii) = g;
	}
	}
}
for (int i = 0; i < 3;  i++)
{
cout << *(p+i) << " ";
}
cout << endl;
}



Last edited on
nice, cdoubleplus. I was looking to do everything outside of main. but I'm glad I saw yours :)
and also... i thought i would sort them lol...
Last edited on
@kash kow ken
hey nice - this works! you devide up the numbers into odd and even ones and then sort each group smallest first :) but you still have to return the smallest even and odd numbers to the user, something like

The smallest even number is 2
The smallest odd number is 5


cdoubleplus, yes, I didn't read the whole thing correctly... haha I thought I had to divide them into even and odds... then take those and sort them from small to big haha.

having a static variable is nice too... i didn't think of that... also how you used the streams on cin... I didn't think of that either... but I'm starting today on the iostream library.

Last edited on
Why are you people outright doing his homework for him? Do you WANT more mediocre programmers on the market?

Also, kash kow ken, why are you using global variables? Do you not know how to pass arrays as parameters?

Well, since you people have already posted solutions to his problem, I'll post mine. Why did none of you check your input to make sure the numbers were actually even and odd? What if the user types in 6 even numbers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
  Project: Take 3 even numbers and 3 odd numbers and determine which of the even/odd numbers is the smallest.
  Author: packetpirate
  Last Updated: 01-29-2011
*/

#include <iostream>
using std::cout;
using std::cin;
#include <limits>

bool isEven(int x);
int findLowEven(int x[]);
int findLowOdd(int x[]);

int main(int argc, char* argv[])
{
    int x[6];
    int temp;
    
    cout << "Enter 3 even numbers..." << std::endl;
    for(int i = 0;i < 3;i++)
    {
        cout << i+1 << ": ";
        cin >> temp;
        if(isEven(temp))
        {
            x[i] = temp;                
        }
        else
        {
            cout << "That number is not even. Please try again..." << std::endl;
            i--;
        }
    }
    
    cout << "Enter 3 odd numbers..." << std::endl;
    for(int i = 3;i < 6;i++)
    {
        cout << i+1 << ": ";
        cin >> temp;
        if(!isEven(temp))
        {
            x[i] = temp;                 
        }
        else
        {
            cout << "That number is not odd. Please try again..." << std::endl;
            i--;    
        }
    }
    cout << std::endl;
    
    for(int i = 0;i < 6;i++)
    {
        cout << x[i] << " ";        
    }
    cout << std::endl << std::endl;
    
    cout << "The lowest even number is: " << findLowEven(x) << std::endl;
    cout << "The lowest odd number is: " << findLowOdd(x) << std::endl;
    cout << std::endl;
    
    cin.get();
    cout << "Press <ENTER> to continue...";
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}

bool isEven(int x)
{
    if(!(x%2))
    {
        return true;
    }
    else
    {
        return false;    
    }
}

int findLowEven(int x[])
{
    int low = x[0];
    
    for(int i = 0;i < 2;i++)
    {
        if(x[i+1] < low)
        {
            low = x[i+1];
        }
    }
    
    return low;
}

int findLowOdd(int x[])
{
    int low = x[3];
    
    for(int i = 3;i < 5;i++)
    {
        if(x[i+1] < low)
        {
            low = x[i+1];          
        }        
    }
    
    return low;
}
Last edited on
@packetpirate
:( don't be angry with us plz - its just fun to solve those problems :) and in the end iCPP sees how it works and is motivated for next lesson?!

@kash kow ken
yeah your devision of the numbers into two sets and sorting has done the work - now you just have to take the first number of each set? so it was correct... we both would get all points I guess

@iCPP
how many points do we get for that problem solving? :)
Last edited on
@cdoubleplus
But you're not doing him any favors by doing the work for him. If you don't want the market to be flooded with talentless programmers, then you need to stop doing their work for them. They're never going to learn anything by having others do their homework for them.

Is this how project manager nightmares start?
@iCPP
ok - u heared what uncle packetpirate said? never introduce your questions whith words like this: "This is a problem that I got from class..." if you want us to answer your quests :)
@cdoubleplus

<facepalm>
That's not what gave him away... I simply don't help people who don't do some work themselves first.
cdoubleplus:
Read the forum rules.
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.


You are violating them right now. He's not supposed to post homework questions, and you are not supposed to give the solutions. In programming classes the students are supplied with the knowledge they need to solve their problems.

It's stickied, and the title is "read before posting".
http://cplusplus.com/forum/beginner/1/
Last edited on
@cdoubleplus: Alright, next time I won't introduce like that :). This is my second hw in C++ program. I'm really new with C++. I've just learned some from high school. I just want to have a whole to program to see how I can figure out, and also prepare for the next lesson. Because after I turn it in, I won't see the solution from the teacher. And I don't know how many point for this.

@All: I really appreciate for your time. I'll review carefully all of your works

Thanks again
I am disappoint...

I feel like .. a pit of despair .... enveloping ........... me.......

Seriously. Stop that.
ultifinitus wrote:
Seriously. Stop that.

Why? This is so much fun! I want to try too!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

struct IsOdd { bool operator()(int n){ return n%2!=0; } };
struct IsEven { bool operator()(int n){ return n%2==0; } };

int main()
{
    vector<int> numbers;
    vector<int> odd;
    vector<int> even;

    cout << "enter your numbers (CTRL-Z to stop):\n";

    copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(numbers));

    remove_copy_if(numbers.begin(),numbers.end(),back_inserter(odd),IsEven());
    remove_copy_if(numbers.begin(),numbers.end(),back_inserter(even),IsOdd());

    cout << "odd min: " << *min_element(odd.begin(),odd.end()) << endl;
    cout << "even min: " << *min_element(even.begin(),even.end()) << endl;

    cout << "\nhit enter to quit...";
    cin.clear();
    cin.get();
    return 0;
}
Ooh Ooh! My turn!!
1
2
3
4
5
6
7
8
9
10
11
#include "stdio.h"
#define BIG (0U-1)
int main(){
	unsigned o=BIG,e=BIG,x,i=0;
	char s[80];
	while(i++<6&&fgets(s,80,stdin)){
		sscanf(s,"%u",&x);
		(x%2?e:o)>x&&(*(x%2?&e:&o)=x);//SHOOP DA WOOP!
	}
	printf("max even: %u\nmax odd: %u\n",e,o);
}
Last edited on
wow :o ...//SHOOP DA WOOP! how does this work?
Last edited on
Pages: 12