This question is in C++.
For each code snippet below, write what the code outputs to the standard output:
(a) queue < int > q ;
q . push (4);
q . push (7);
q . push (2);
q . push (11);
q . push (16);
for ( int i = 0; i < q . size (); i ++)
{
q . push ( q . front () * 2);
q . pop ();
}
while (! q . empty ()) {
cout << q . front () << " ";
q . pop ();
}
(b) void print_vec ( vector < string > vec , int idx ) {
if ( idx == -1) return ;
print_vec ( vec , idx - 1);
cout << vec [ idx ] << endl ;
}
int main ()
{
vector < string > colors = {" blue " , " yellow " , " green " , " red "};
print_vec ( colors , 3);
return 0;
}
(a) queue < int > q ; q: [] q . push (4); q: [4] q . push (7); q: [4,7] q . push (2); q: [4,7,2] q . push (11); q: [4,7,2,11] q . push (16); q: [4,7,2,11,16] Iteration1: q: [7,2,11,16,8] Iteration2: q: [2,11,16,8,14] Iteration3: q: [11,16,8,14,4] Iteration4: q: [16,8,14,4,22] Iteration5: q: [8,14,4,22,32] while loop prints the content of array by removing each value from queue q So, It prints 8 14 4 22 32 =========================================== (b) This code passed the vector to function. This function prints the content of array in the same order in vector So, output is blue yellow green red
Get Answers For Free
Most questions answered within 1 hours.