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

What is the output of the following c++ program segment? Const int a [ ] {2, 3,

ID: 3584872 • Letter: W

Question

What is the output of the following c++ program segment?

Const int a [ ] {2, 3, 5, 7, 11, 13, 17, 19 };

const char c [ ] { 'R', 'A', 'D', 'A', 'R'};

vector <int> v1 (a + 3, a + 6);

cout << v1.size( ) << endl;

for (unsigned i = 0; i <v1.size ( ); i++) cout << v1[ i ] <<' ';

cout <<endl;

vector <int > v2 (3, -1);

cout<< v2.size ( ) <<endl;

for (unsigned j=0; j<v2.size ( ); j++) v2 [ j ] ++;

for(vector <int>: : const_iterator p =v2.begin ( ); p != v2.end ( ); p++) cout << *p << ' ';

cout << endl;

vector <char> v3 (c, c + sizeof (c) );

cout << v3.size ( ) << endl;

please help explain the output of this program

Explanation / Answer

#include <iostream>

#include <vector>

using namespace std;

int main() {

const int a [ ] {2, 3, 5, 7, 11, 13, 17, 19 }; // const array of integers

const char c [ ] { 'R', 'A', 'D', 'A', 'R'}; //const array of characters

//here the arrays a and c are used to initializt the vectors

vector <int> v1 (a + 3, a + 6); // vector v1 of three integers with start value 7(a+3) and end value 13(a+6)

cout << v1.size( ) << endl; // size of vector v1 = 3

for (unsigned i = 0; i <v1.size ( ); i++) cout << v1[ i ] <<' '; // display vector v1 elements 7 11 13

cout <<endl;

vector <int > v2 (3, -1); //declare vector v2 of size 3 with each value = -1

cout<< v2.size ( ) <<endl; // size = 3, vector elements = -1,-1,-1

for (unsigned j=0; j<v2.size ( ); j++) v2 [ j ] ++; // increment each element by 1 so elements become 0 0 0

//use iterator to iterate the vector v2

for(vector <int>:: const_iterator p =v2.begin ( ); p != v2.end ( ); p++) cout << *p << ' '; //0 0 0

cout << endl;

vector <char> v3 (c, c + sizeof (c) ); // vector v3 starting value is base address of array c ie 'R' and end value is c+5 = 'R'

cout << v3.size ( ) << endl; //size of vector v3 is size of array c = 5

return 0;

}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote