vector trouble

The goal is to create 25 random numbers in between 0 and 99 and place them into two lists which tells the user if they are even or odd. I cant get it to put the numbers into even and odd vectors.
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
#include<iostream>
#include<vector>
using namespace std;

int random(int v1)
	//generates 25 random numbers between 0 and 99
{	
	for (int count=0; count<25; count++)  //counts up to 25 so only 25 numbers are generated
	{
		v1 = rand() % 100; // v1 is the range 0 to 99	
		cout<<v1<<endl;
	}
	return(v1); //passes v1 into num variable in the main function
}
int main()
{
	int num=0;
	random(num);
	int even=0;
	int odd=0;
   vector<int>evennum(25);            //this is the vector for even numbers they each could have 25 numbers stored
   vector<int>oddnum(25);             // this is the vector for odd numbers they each could have 25 numbers stored
	// displays the 25 numbers
	int remainder = num % 2; // determines if the remainder is even 

        if (remainder==0)			//if the number is even it is added into the even vector
		{
            evennum[even] = num; //takes in the number of odd numbers and stores them in evennum
            even++; //counts the number of even numbers
			evennum; //outputs the numbers
        }
        else						//if the number is not even it is placed in the odd vector
		{
            oddnum[odd] = num; //takes in the number of odd numbers and stores them in oddnum
            odd++; //count the number of odd numbers
			oddnum; //outputs the numbers
		}

	return(0);
}
Last edited on
Tell us what about it isnt working. Also, surround your code in code blocks.Its the button under Format: that looks like <>.
You have several problems with your code.

Line 8: You termination condition on the for loop is wrong. It should be count < 25

Line 10: You generate the random number, but don't store it anywhere. The value of v1 is lost on the next iteration of the loop.

Line 22: You calculate remainder from num, but num is uninitialized.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.




I fixed line 8 thank you I probably wouldn't have seen it for a while. I initialized num it's passing the random number back to the code.

It now does output but just a line of numbers. I am not quite sure how to store the random number
Topic archived. No new replies allowed.