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

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...
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...
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...
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'.
C++ Create a program which randomly generates 3 sets of x and y Integers and one...
C++ Create a program which randomly generates 3 sets of x and y Integers and one randomly generated Integer. Two of the sets of Integers will represent the end points of a line segment. The other set of Integers and the other Integer will represent the midpoint of a circle and its radius. The coordinates should be randomly generated using a user defined function that returns an Integer value based on from and to parameters; see the function declaration in...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT