Question: Modifying a vector during iteration example: Doubling element values. Complete the following program to double each number in the vector. INPUT: 67 -5 -99 4

Modifying a vector during iteration example: Doubling element values.

Complete the following program to double each number in the vector.

INPUT: 67 -5 -99 4 22

How to Double these Inputs in c++

#include #include using namespace std;

int main() { const int NUM_ELEMENTS = 5; // Number of elements vector userVals(NUM_ELEMENTS); // User values unsigned int i; // Loop index // Prompt user to populate vector cout << "Enter " << NUM_ELEMENTS << " integer values..." << endl; for (i = 0; i < userVals.size(); ++i) { cout << "Value: " << endl; cin >> userVals.at(i); } // Convert negatives to 0 for (i = 0; i < userVals.size(); ++i) { if (userVals.at(i) < 0) { userVals.at(i) = 0; } } // Print numbers cout << "New values:"; for (i = 0; i < userVals.size(); ++i) { cout << " " << userVals.at(i); } cout << endl; return 0; }

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!