In: Computer Science
Using c++,
1: Create a vector of 20 randomly generated integers, then
2. Create a new vector that will only store the even numbers from the original vector.
3. Display the original vector and the vector of even integers to the console.
#include <iostream>
#include <string>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
vector<int> vec1;
vector<int> vec2;
// Generating original vector of 20 random numbers between 1 and 100
for(int i = 0;i<20;i++)
{
// Pushing each random number generated to vec1
vec1.push_back(1+rand()%1000);
}
cout<<"Content of the original vector:"<<endl;
// Looping throgh each value in the first vector
for(int i = 0;i<20;i++)
{
// Printing value to console
cout<<vec1[i]<<" ";
}
// Printing new line
cout<<endl;
// Looping through each value in the original vector
for(int i = 0;i<vec1.size();i++)
{
// If the value at index i is even
if(vec1[i]%2==0)
{
// Pusing this value to second vector
vec2.push_back(vec1[i]);
}
}
cout<<"\nContent of the vector of even integers:"<<endl;
// Looping throgh each value in the second vector
for(int i = 0;i<vec2.size();i++)
{
// Printing value to console
cout<<vec2[i]<<" ";
}
// Printing new line
cout<<endl;
return 0;
}

