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...
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'.
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...
Verify using an example that vector a + (vector b * vector c) is not equal...
Verify using an example that vector a + (vector b * vector c) is not equal to (vector a + vector b) * (vector a + vector c) explain the problem that arrises
Write a C++ program that 1) generates a vector containing 10 different random integers with values...
Write a C++ program that 1) generates a vector containing 10 different random integers with values between 1 and 100, then 2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout. Your program output should look like this: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT