Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following functions related to vectors 1. Write a function called read

ID: 3706790 • Letter: W

Question

Write the following functions related to vectors 1. Write a function called readVector that takes a vector& as a parameter and return void. Read a number of ints in from 2. Write a function called printvector that takes a vector& as a parameter and prints out all of the elements of the vector 3. Write a function called halfvector that takes two vector& as parameters and returns void. In this funciton, add the sum of stdin (use cin), and add them to the vector, until the value -1 is read. Do not add -1 to the vector. with a comma and space after them, and a newline at the end each two elements of the first vector to the second vector. For example, if [1,2, 3,4], and [l are passed in, the second vector should become [3,7]. You may assume that the first vector will always have an even number of elements in it.

Explanation / Answer

#include <iostream>
#include <vector>
using namespace std;
void readVector( vector<int> &v1) {
int n;
cout<<"Enter the number (-1 to quit): "<<endl;
cin >> n;
while(n!=-1) {
v1.push_back(n);   
cout<<"Enter the number (-1 to quit): "<<endl;
cin >> n;
  
}
}
void halfVector( vector<int> &v1, vector<int> &v2) {
for(int i=0;i<v1.size();i+=2) {
int sum = v1[i]+v1[i+1];
v2.push_back(sum);
}
}
void printVector( vector<int> v1) {
for(int i=0;i<v1.size();i++) {   
cout<<v1[i]<<" ";
}
cout<<endl;
}
int main()
{
vector<int> v1;
vector<int> v2;

readVector(v1);
halfVector(v1, v2);

printVector(v1);
printVector(v2);

return 0;
}

Output: