Question: The existing code below reads in a random seed and generates a sequence of numbers in the vector vec . It then makes a second

The existing code below reads in a random seed and generates a sequence of numbers in the vector vec. It then makes a second vector, vec2, that is the same size (filled with 0s). It then prints both. Before they are printed, use the transform algorithm to fill vec2 so that each value = c^2 - 1 where c is the corresponding value in vec. A squareMinusOne function is already written - you can use it in your transform call. Challenge: Once you get it written using the squareMinusOne function, try doing it with a lambda function. Replace squareMinusOne in your transform with a lambda function. The general form of a lanbda to take an int x and do work would be:

[] (int x) { //whatever code you want, make sure to return a value }

Note that running this code on your own computer is likely to produce a different random sequence than the one generated by CPPLab. That should not affect your code, just don't be surprised if that happens.

#include #include #include #include #include

using namespace std;

int squareMinusOne(int x) { return x * x - 1; }

int main() { //Fill vector with random numbers 1-10 int seed; cin >> seed; srand(seed);

vector vec; for(int i = 0; i < 5; i++) { vec.push_back( rand() % 10 + 1); }

//Make another vector and size it to match first vector vector vec2; vec2.resize(vec.size());

//Do not modify anything on or above the line below this //YOUR_CODE_BELOW

//YOUR_CODE

//YOUR_CODE_ABOVE //Do not modify anything on or below the line above this

//Print the vector for(int i : vec) cout << i << " "; cout << endl;

//Print the second vector for(int i : vec2) cout << i << " "; cout << endl;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!