Even/odd vector problem

Good morning everyone, first post here but I have been lurking for a bit and you all offer wonderful advice and support. As a brief introduction, I am a sophomore engineering student who is currently taking an 8-week Intro to C++ class online. Through the first half of the term I have discovered that coding is a bit of a struggle for me, even through I am dedicating the necessary time. Despite I have been able to complete all the assignments thus far.

My issue is our latest program which is described below; the problem is I am totally confused by the concept of a vector and have no idea how to start coding this program. Any guidance would be appreciated, thank you!


Write a program that reads an unspecified number of integers from STDIN, terminated with EOF.

The only output of your program (so no prompts or other output please!) should be all of the odd values output in the reverse of the order they were input in (one value per line) followed by all of the even values also output in the reverse of the order they were entered (one value per line.)

Your program should make use of the vector, as described in class and the textbook.

For example, if your input was:

-5

-4

-3

-2

-1

0

1

2

3

4
5

then your output would be:

5

3

1

-1

-3

-5

4

2

0

-2

-4
Tutorial (vector): http://www.mochima.com/tutorials/vectors.html

Define two vectors; put all the odd values into the first vector, and all the even values into the second.
Print them out in reverse order.
Last edited on
Thank you for all the suggestions so far. Using my textbook and an example I found online I have build the code below. I am stuck at this point, the program compiles but only accepts one input then hangs. Very frustrated at this point and any advice would be greatly appreciated.

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
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;


int main ()
{
	int input = 0;
	
		cout << "Enter a number(-9 to quit): "; 
		cin	>> input;    
	
		vector <int> even; // declare a vector
	    while (cin >> input)
		{
			if ( input % 2 == 0) 
			{
	        even.push_back(input);
			}
		else
		{
	    vector <int> odd; // declare a second vector
	    while (cin >> input)
		{
		if (input % 2 != 0)
		{
	        odd.push_back(input);
		}
		}
	    vector <int>::iterator It;
	 
	    for (It = even.begin(); It != even.end(); ++It)    // REVERESE THESE TO REVERSE OUTPUT???
	        cout << *It << '\t'; // output the current value that It is *pointing to
	 
	    // print a new line
	    cout << endl;
	 

	    for (It = odd.begin(); It != odd.end(); ++It)
	        cout << *It << '\t'; // output the current value that It is *pointing to
	 
	    cout << endl;
	    cin.get();
	    return 0;
		}
	}
}	
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>

int main ()
{
    // declare both the vectors before the loop
    // we need these to be alive till the end, where we print out their contents
    std::vector<int> even_numbers ;
    std::vector<int> odd_numbers ;

    int number = 0;
    
    // for each number that the user enters till a -9 is entered (or input fails)
    while( std::cin >> number && number != -9 ) // no prompts or other output please!
    {
        if( number%2 == 0 ) even_numbers.push_back(number) ; // even number
        else odd_numbers.push_back(number) ; // odd number
    }

    // all of the odd values output in the reverse of the order
    // they were input in (one value per line)
    // so we use reverse_iterators: rbegin() and rend() 
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    for( auto iter = odd_numbers.rbegin() ; iter != odd_numbers.rend() ; ++iter )
        std::cout << *iter << '\n' ;

    // followed by all of the even values also output in the reverse ...
    for( auto iter = even_numbers.rbegin() ; iter != even_numbers.rend() ; ++iter )
        std::cout << *iter << '\n' ;
}

http://coliru.stacked-crooked.com/a/7ac58fbdb01ab78c
Last edited on
Thank you for you reply JLBorges. The way you have this coded makes more sense to me, although I am confused on how the auto iter works. This looks like it is very close to running although my compiler gives me an warning citing "warning: ‘auto’ changes meaning in C++11; please remove it". Also that iter what not declared...Please help. The topic of vectors is very confusing to me.
With C++98, specify the actual type instead of auto

1
2
3
4
5
for( /*auto*/ std::vector<int>::reverse_iterator iter = odd_numbers.rbegin() ; iter != odd_numbers.rend() ; ++iter )
        std::cout << *iter << '\n' ;

for( /*auto*/ std::vector<int>::reverse_iterator iter = even_numbers.rbegin() ; iter != even_numbers.rend() ; ++iter )
        std::cout << *iter << '\n' ;


You are a saint.
Topic archived. No new replies allowed.