Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database System

Authors: Elmasri Ramez And Navathe Shamkant

7th Edition

978-9332582705

Students also viewed these Databases questions