209k views
2 votes
Read integers from input and store each integer into a vector until -1 is read. Do not store -1 into the vector. Then, output all values in the vector (except the last value) with the last value in the vector subtracted from each value. Output each value on a new line. Ex: If the input is -46 66 76 9 -1, the output is:

-55
57
67

1 Answer

5 votes

Answer:

The program in C++ is as follows:

#include <iostream>

#include <vector>

using namespace std;

int main(){

vector<int> nums;

int num;

cin>>num;

while(num != -1){

nums.push_back(num);

cin>>num; }

for (auto i = nums.begin(); i != nums.end(); ++i){

cout << *i <<endl; }

return 0;

}

Step-by-step explanation:

This declares the vector

vector<int> nums;

This declares an integer variable for each input

int num;

This gets the first input

cin>>num;

This loop is repeated until user enters -1

while(num != -1){

Saves user input into the vector

nums.push_back(num);

Get another input from the user

cin>>num; }

The following iteration print the vector elements

for (auto i = nums.begin(); i != nums.end(); ++i){

cout << *i <<endl; }

User Leprosy
by
4.2k points