Vectors

Howdy, I am the beginner in C++ . I need to write a program which will sort a vector into two vector which will have name odd and even numbers . The size of the vector is 100
My problem is that its saying that I am going out of range .

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


void fillingVector( std::vector<int>&array){
	for (size_t i=0;i<array.size();i++){
		array[i]=i;
	}
}
void output(std::vector<int>&array){
	for (size_t i=0;i<array.size();i++){
		std::cout<<array[i]<<" ";
	}
}
void isOdd(std::vector<int>&array,std::vector<int>&arraOdd,int oddSize){
	for (size_t i=0;i<array.size();i++){
		if (array[i]%2!=0){
			arraOdd[oddSize++]=array[i];/// I think I have problem here but I do not understand what is wrong with it
		
		}
	}
}
void isEven(std::vector<int>&array,std::vector<int>&arrayEven,int evenSize){
	for (size_t i=0;i<array.size();i++){
		if (array[i]%2==0){
			arrayEven[evenSize++]=array[i]; /// I think I have problem here but I do not understand what is wrong with it
		}
	}
}

int main ()
{
	size_t N;
	std::cout<<"Enter the size of the vector: ";
	std::cin>>N;
	std::vector<int>Test(N);
	fillingVector(Test);
	output(Test);
	size_t odd=5,even=5;
	std::vector<int>Odd(odd);
	std::vector<int>Even(even);
	isOdd(Test,Odd,odd);
	std::cout<<std::endl;
	output(Odd);
	return 0;
}
1) change your Odd and Even vectors to be default constructed.
2) in isOdd (BTW this is strange names which does not tell about they purpose) and isEven functions:
a) remove odd/evenSize parameter
b) change adding element to array line to following: arraOdd.push_back(array[i]);

BTW: another version of your program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
#include <numeric>


int main()
{
    std::vector<int> array(100);
    std::iota(array.begin(), array.end(), 0);
    std::vector<int> even, odd;
    std::partition_copy(array.begin(), array.end(),
                        std::back_inserter(even),
                        std::back_inserter(odd),
                        [](int x){return x%2 == 0;});
    std::copy(even.begin(), even.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
    std::copy(odd.begin(), odd.end(), std::ostream_iterator<int>(std::cout, " "));
}
http://ideone.com/mmULlz
Use push_back on the odd and even vectors.

Also, your isOdd call in the main is not helping matters at all. You are indexing out of bounds and the program does not like that.
Here's the issue:

arraOdd[oddSize++]=array[i];

is going out of bounds because "arraOdd" has a length of 5 and you are putting 5 as it's index (which is already out of range because arraOdd only goes up to 4) and then you are incrementing it with each iteration. Even if you change it to i, it will still go out of range because it's looping through test which has a size of 10.

You should make a local variable called j and set it to 0. Then only increment it in your for loop if the if condition is met. So your code should look something more like:

1
2
3
4
5
6
7
8
9
10
11
int j=0;
void isOdd(std::vector<int>&array,std::vector<int>&arraOdd,int oddSize){
        int j=0;
	for (size_t i=0;i<array.size();i++){
		if (array[i]%2!=0){
			arraOdd[j]=array[i];
                        j++;
		
		}
	}


You'll need to do something like this with your isEven function as well.
Topic archived. No new replies allowed.