### Having Issues with an Even Odd Number Seperation

/* It compiles and runs, but on output it tells me the index is bigger than the max index */

#include <iostream.h>
#include <lvp\vector.h>
#include <lvp\random.h>

//___________________________________

void sort(vector<int>&sortnum); //Sorts numbers into odd or even

//___________________________________

void even(vector<int>&evennum, vector<int>&oddsnum); //Organizes even numbers

//___________________________________

void odd(vector<int>&oddnum); //Takes odds and organizes them

//____________________________________

int main()
{
randomize();
vector<int>randnum(25);

for (int C=0; C<=24; C++)
randnum[C]=random(100)+1;
cout << "\n\n\n\n\n";
cout << "Randomly Selected Numbers: ";
for (int d=0; d<=23; d++)
cout << randnum[d] << ", ";
cout << randnum[24];

sort(randnum);

return(0);
}

//___________________________________

void sort(vector<int>&sortnum)
{
vector<int>evnum(25, 0);
vector<int>odnum(25, 0);
int cont=0, conte=0;

for (int x=0; x<=sortnum.length(); x++)
{
if (sortnum[x]%2==0)
{
cont++;
evnum.resize(cont);
evnum[x]=sortnum[x];
}
else
{
conte++;
odnum.resize(conte);
odnum[x]=sortnum[x];
}
}

even(evnum, odnum);
}

//____________________________________

void even(vector<int>&evennum, vector<int>&oddsnum)
{
int length=evennum.length();

cout << "\n\n\n";
cout << "Even Numbers: ";
for (int a=0; a<=(length-1); a++)
cout << evennum[a] << ", ";
cout << evennum[length];

odd(oddsnum);
}

//____________________________________

void odd(vector<int>&oddnum)
{
int lengths=oddnum.length();

cout << "\n\n\n";
cout << "Odd Numbers: ";
for (int q=0; q<=(lengths-1); q++)
cout << oddnum[q] << ", ";
cout << oddnum[lengths];
}

 ``123456789101112131415161718192021222324`` ``````void sort(vector&sortnum) { vectorevnum(25, 0); vectorodnum(25, 0); int cont=0, conte=0; for (int x=0; x<=sortnum.length(); x++) { if (sortnum[x]%2==0) { cont++; evnum.resize(cont); evnum[x]=sortnum[x]; } else { conte++; odnum.resize(conte); odnum[x]=sortnum[x]; } } even(evnum, odnum); }``````

uses x as an index into sortnum. You can't also use it as an index into odnum and evnum because they're all different lengths.

Seeing as you're using a vector, you could just say [code]odnum.push_back(sortnum[x]) and not have to worry about the index into odnum.

Jim
Another thought ... if you are going to use `vector::push_back()`, you'll not want to initialise evnum and odnum to have the 25 elements already.

Jim
Sorry, my class hasn't learned that push back bit, so I couldn't use that. I don't suppose there's a more basic fix to my problem?
Yes, you need to keep three index variables, one for each of sortnum, evnum and odnum.

Actually, I just noticed you already have them - cont and conte.

Then you'd be saying something like

 ``12345`` ``````if (sortnum[x] % 2 == 0) { evnum[conte] = sortnum[x]; conte++; }``````

Note that you have to increment conte after using it, as indices are zero-based. If you did it before, you'd be skipping evnum[0]'s assignment.

You also don't need to resize the vectors as they already have 25 elements from their construction.

Jim
Thanks a ton Jim! You were very helpful!
No worries
//Still Produced a max index=0, index = -1 on the output

void sort(vector<int>&sortnum)
{
vector<int>evnum;
vector<int>odnum;
int cont=0, conte=0;

for (int x=0; x<=sortnum.length(); x++)
{
if (sortnum[x]%2==0)
{
evnum.resize(cont);
evnum[x]=sortnum[x];
cont++;
}
else
{
odnum.resize(conte);
odnum[x]=sortnum[x];
conte++;
}
}

even(evnum, odnum);
}
//This is, still produced that on the output. says max=0, and index=-1

void sort(vector<int>&sortnum)
{
vector<int>evnum;
vector<int>odnum;
int cont=0, conte=0;

for (int x=0; x<=sortnum.length(); x++)
{
if (sortnum[x]%2==0)
{
evnum[cont]=sortnum[x];
cont++;
}
else
{
odnum[conte]=sortnum[x];
conte++;
}
}

even(evnum, odnum);
}
You've removed both of the bits of code that were setting the vector size - the constructor initialisation and the calls to resize(). You need one of those, otherwise your vector is zero length and you can't access elements via [].

Seeing as evnum and odnum are initialised empty, I'd add back the resize calls. That covers you for any length of vector you want to create.

Your for loop is also running one-too-many. You need to run from 0 .. length-1, not 0 .. length.

Fix that - I'll have a look at the rest of your code and get back to you.

Oh, and you need to resize to cont+1 and conte+1

Cheers,
Jim
Last edited on
//I did what you just said. Same results

void sort(vector<int>&sortnum)
{
vector<int>evnum;
vector<int>odnum;
int cont=0, conte=0, lengths=sortnum.length();

for (int x=0; x<=lengths-1; x++)
{
if (sortnum[x]%2==0)
{
evnum.resize(cont);
evnum[cont]=sortnum[x];
cont++;
}
else
{
odnum.resize(conte);
odnum[conte]=sortnum[x];
conte++;
}
}

even(evnum, odnum);
}
In your even and odd methods, the lines that print the last entry are also accessing one element past the end of the vector

 `` `` ``cout << oddnum[lengths];``

Vectors are indexed like arrays, from 0 .. length - 1
If you try to access oddnum[length] you'll get an exception because the vector isn't that long.

Have you disabled exceptions somehow, because your original code should have thrown them all over the place?

Also, I'm not sure what output you're expecting, but I don't see any code that will print the max and the index - it'll print the numbers as generated, then the even numbers and then the odd numbers in separate lists.

Cheers,
Jim
I'm Trying to print:

Randomly Generated Numbers: .....

Even Numbers: ..........

Odd Numbers: .........

where the even and odd bits are the random numbers separated into even and odd.
OK, with a few tweaks it should be doing that ;-)

Try it with the changes above and if it still doesn't work, can you post your code again, together with the output?

Jim
//Code

/* Project Even Odd
by Jacob Wright 2-20-13 */

#include <iostream.h>
#include <lvp\vector.h>
#include <lvp\random.h>

//___________________________________

void sort(vector<int>&sortnum); //Sorts numbers into odd or even

//___________________________________

void even(vector<int>&evennum, vector<int>&oddsnum); //Organizes even numbers

//___________________________________

void odd(vector<int>&oddnum); //Takes odds and organizes them

//____________________________________

int main()
{
randomize();
vector<int>randnum(25);

for (int C=0; C<=24; C++)
randnum[C]=random(100)+1;
cout << "\n\n\n\n\n";
cout << "Randomly Selected Numbers: ";
for (int d=0; d<=23; d++)
cout << randnum[d] << ", ";
cout << randnum[24];

sort(randnum);

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

return(0);
}

//___________________________________

void sort(vector<int>&sortnum)
{
vector<int>evnum;
vector<int>odnum;
int cont=1, conte=1, lengths=sortnum.length();

for (int x=0; x<=lengths-1; x++)
{
if (sortnum[x]%2==0)
{
evnum.resize(cont);
evnum[cont]=sortnum[x];
cont++;
}
else
{
odnum.resize(conte);
odnum[conte]=sortnum[x];
conte++;
}
}

even(evnum, odnum);
}

//____________________________________

void even(vector<int>&evennum, vector<int>&oddsnum)
{
int length=evennum.length();

cout << "\n\n\n";
cout << "Even Numbers: ";
for (int a=0; a<=(length-2); a++)
cout << evennum[a] << ", ";
cout << evennum[length-1];

odd(oddsnum);
}

//____________________________________

void odd(vector<int>&oddnum)
{
int lengths=oddnum.length();

cout << "\n\n\n";
cout << "Odd Numbers: ";
for (int q=0; q<=(lengths-2); q++)
cout << oddnum[q] << ", ";
cout << oddnum[lengths-1];
}

//Output: http://i.imgur.com/Pq1ENCW.png
OK, I see what's going on now.

Your first list is fine - that's output correctly with all 25 numbers.
It's then that you get the error, and the error is generated by the vector class. It says that you've tried to access element index 1, but the maximum valid index is zero.

Translating that, you've got a list with one element and you've tried to access element two.

The problem is that you've not resized the vector to cont+1 and conte+1 in the sort() method resize() calls, you've started with cont and conte == 1. Switch that back, otherwise you're not assigning index 0, and your resize is one too small.

That method should be something like

 ``123456789101112131415161718192021222324`` ``````void sort(vector&sortnum) { vectorevnum; // Initialised to zero length vectorodnum; int cont=0, conte=0, lengths=sortnum.length(); for (int x=0; x<=lengths-1; x++) // Last valid index is [lengths - 1] { if (sortnum[x]%2==0) { evnum.resize(cont+1); // cont is current length of list. Need one more to fit the new value evnum[cont]=sortnum[x]; // Next free index will be current length as is zero-based indexing cont++; // Keep track of the new length } else { odnum.resize(conte+1); odnum[conte]=sortnum[x]; conte++; } } even(evnum, odnum); }``````

Give that a go!

Jim
THANK YOU SO MUCH JIM! It works perfectly now. I've been having a tough week with the new vector stuff, and couldn't get my last assignments to click. I'd have given up already without your help! Thank you very much, you were overly patient and kind.
No worries - glad to help
Topic archived. No new replies allowed.