Question

In: Computer Science

Using c++, 1: Create a vector of 20 randomly generated integers, then 2. Create a new...

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.

Solutions

Expert Solution

#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;
}


Related Solutions

Declare an integer array of size 20 and assign the array with 20 randomly generated integers...
Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100]. Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program. Finally, print out the shuffled array. (25 points) #include<iostream> using namespace std; const int NUM = 20; int main() {             //...
C++ on randomly generated integers stored in vectors. you will use routines from STL <algorithm> to...
C++ on randomly generated integers stored in vectors. you will use routines from STL <algorithm> to implement these algorithms. Using the following functions with their description const int DATA_SIZE = 200; const int SEARCH_SIZE = 100; const int DATA_SEED = 7; const int SEARCH_SEED = 9; void genRndNums( vector<int>& v, int seed ) { This routine generates random integers in the range [ LOW = 1, HIGH =200 ] and puts them in vector v. Initializes the random number generator...
c++ code Reverse the order of elements in a vector of integers using two functions: (a)...
c++ code Reverse the order of elements in a vector of integers using two functions: (a) The first function takes the input vector a and return a vector b with the input data but in the reverse order, e.g. input: 1, 2, 3 and output 3, 2, 1. (b) The second function is a recursive one and it reverses the input data in place (without using an additional vector).
Directions: Using a vector of integers that you define. Write a C++ program to run a...
Directions: Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 1. Write a function called getValidAge that allows a user...
Using a vector of integers that you define. Write a C++ program to run a menu...
Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 2. Write a function called getValidAge that allows a user to...
2. Vector Element Calculation Write a MATLAB script which will randomly generate a 20-element vector with...
2. Vector Element Calculation Write a MATLAB script which will randomly generate a 20-element vector with integer values ranging between −100 to +100. Using LOOP command, determine the number of negative elements in the vector. For example, given A = [15 −6 0 -8 −2 5 4 −10 -3 −5], the number of negative numbers are 6.
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
Create a vector of 100 integers PRG from 1 to 500. Find the max and min...
Create a vector of 100 integers PRG from 1 to 500. Find the max and min and print those out. Please use c++.
Use R code to do the following!!!! 1. Create a vector v1 that will contain integers...
Use R code to do the following!!!! 1. Create a vector v1 that will contain integers from -30 to 60. 2. Copy v1 into a vector v2 and add names 'odd' or 'even' based on the value. 3. Copy v1 into a vector v3 and if the number can be divided by 3, replace it by 'NA'. 4. Assign the mean of v3 to v4 ignoring the 'NA'.
Create a vector of 100 integers PRG from 1 to 500. Find the max and min...
Create a vector of 100 integers PRG from 1 to 500. Find the max and min and print those out. Please use c++. Please use #include<iostream>, std::min_element, and std::max_element.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT