C++
write a program using parallel vectors and a function which fills each of them with 500 random numbers between 1 and 100. The program should then pass both vectors to a function which will return an integer indicating a count of how many times both vectors had even numbers in the same location. So if vector01[0] contained 4 and vector02[0] contained 12, you would add one to count.If vector01[1] contained 3 and vector02[1] contained 4, you would not add one to count.
main would display something like :
The Vectors contain 128 cells where both values are even.
Above was the question: I already have the code written:
#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int even = 0;
srand(time(0));
vector <int> vector01;
vector <int> vector02;
for (int i = 0; i < 500; i++)
{
vector01.push_back(rand() % 100 + 1);
vector02.push_back(rand() % 100 + 1);
if (vector01[i] % 2 == 0 && vector02[i] % 2 == 0)
even++;
}
cout << "The vectors contain " << even << " cells where both values are even." << endl;
return 0;
}
The code completely works, but there is just one problem.
Now the question asked us to pass the vector values into a function and calculate the total even numbers, I can't figure out how to do that at all! Would you please help me with this using the basic vector code and the variables I have? Thank you!
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images
- Please write the following in very simple C++ code: Write a function that calculates the average of an array, where the smallest element in the array is dropped. If there are multiple copies of the smallest value, just ignore one copy. You can assume the array has at least two items in it. The function header is as follows. int getAverage (int a[], int size)arrow_forwardWrite in C++ Please and Thank you. Write a program that gets a list of integers (which are chosen from values between 0 and 8 inclusive) from the user, and stores the factorial of each integer in a vector. The first integer indicates how many numbers are in the list and should not be stored in the vector. The program should print out the vector values. For example, given the following input: 5 1 2 3 0 4 The first number, 5, means there will be 5 numbers in the vector and we are going to store the factorials of the remaining numbers. Therefore, the following output will be produced: 1 2 6 1 24 which is the result of 1!, followed by 2!, then 3!, 0!, and finally 4!arrow_forwardIntroduction to Programming (C++): Create a program with three vectors (be sure to use: #include <vector>) : One vector for the last name One vector for the first name One vector for the age of the person (whole number) **You determine what sentinel value to use to stop input** So input different kinds of people (first name, last name, and age) until you get to the sentinel value and then output the list of people Calculate the average age of the people and then calculate the Standard Deviation of the ages. Expect to see the means, a function to display the list of names and ages, a function to get the average, and a function to calculate the Standard Deviation **Please use at least 10 people**arrow_forward
- In C++, define an integer vector and ask the user to give you values for the vector. Because you used a vector, so you don't need to know the size. user should be able to enter a number until he wants(you can use while loop for termination. for example if the user enters any negative number, but until he enters a positive number, your program should read and push it inside of the vector). the calculate the average of all numbers he entered.arrow_forwardUsing C++ Programming language: Assume you define a vector in the following way: vector<int> vec; Assign the value 10 to the first element of this vector. What is the statement you would use?arrow_forwardC++ This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int vector and the ratings in another int vector. Output these vectors (i.e., output the roster). Ex: Enter player 1's jersey number: 84 Enter player 1's rating: 7 Enter player 2's jersey number: 23 Enter player 2's rating: 4 Enter player 3's jersey number: 4 Enter player 3's rating: 5 Enter player 4's jersey number: 30 Enter player 4's rating: 2 Enter player 5's jersey number: 66 Enter player 5's rating: 9 ROSTER Player 1 -- Jersey number: 84, Rating: 7 Player 2 -- Jersey number: 23, Rating: 4 ... (2) Implement a menu of options for a user to modify the roster. Each option is represented by a single character. Following the initial 5 players' input and roster output, the program outputs the menu.…arrow_forward
- Write c++arrow_forward1. [True or False] True or false? Consider that we call the function using the below statement: [x, y ,z] = f(vec, i) a. x is the maximum value of all elements of the input vector. True O Falsearrow_forward1- Write a user-defined function that accepts an array of integers. The function should generate the percentage each value in the array is of the total of all array values. Store the % value in another array. That array should also be declared as a formal parameter of the function. 2- In the main function, create a prompt that asks the user for inputs to the array. Ask the user to enter up to 20 values, and type -1 when done. (-1 is the sentinel value). It marks the end of the array. A user can put in any number of variables up to 20 (20 is the size of the array, it could be partially filled). 3- Display a table like the following example, showing each data value and what percentage each value is of the total of all array values. Do this by invoking the function in part 1.arrow_forward
- In c++ Please Write the InOrder() function, which receives a vector of integers as a parameter, and returns true if the numbers are sorted (in order from low to high) or false otherwise. The program outputs "In order" if the vector is sorted, or "Not in order" if the vector is not sorted. Ex: If the vector passed to the InOrder() function is [5, 6, 7, 8, 3], then the function returns false and the program outputs: Not in order Ex: If the vector passed to the InOrder() function is [5, 6, #include <iostream>#include <vector>using namespace std; bool InOrder(vector<int> nums) { /* Type your code here */} int main() { vector<int> nums1(5); nums1.at(0) = 5; nums1.at(1) = 6; nums1.at(2) = 7; nums1.at(3) = 8; nums1.at(4) = 3; if (InOrder(nums1)) { cout << "In order" << endl; } else { cout << "Not in order" << endl; } vector<int> nums2(5); nums2.at(0) = 5; nums2.at(1) = 6;…arrow_forwardAnswer in C++ code please! (Duplicate Elimination with vector) Use a vector to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the vector only if it isn't a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Begin with an empty vector and use its push_back function to add each unique value to the vector.arrow_forwardc++arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education