Question

This question is in C++. For each code snippet below, write what the code outputs to...

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;

}

Homework Answers

Answer #1
(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



Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that reads a string and outputs the number of lowercase vowels in the...
Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be: There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy Where XXXX is the count of lowercase...
Trace the C++ program below showing all output in the order that it is displayed. If...
Trace the C++ program below showing all output in the order that it is displayed. If anything happens that makes it impossible to accomplish an operation or the results of so doingare unpredictable, describe what happens and abort the program at that point. Assume the Queue class is fully defined in an appropriate header and implementation file. The effect of the functions (methods) is as follows - Constructor - creates an empty queue #include <iostream> #include using namespace std; #include...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using linked lists. Currently I am able to input processes and store / display them properly. The issue begins somewhere after I have displayed the processlist (I get a segmentation fault (core dumped) or the code doesnt seem to run). I have marked the location of where I think the issue begins with a comment. Please fix the code so that it is working properly....
C++ question. Please explain the code and how it resulted in the output. Explain each line...
C++ question. Please explain the code and how it resulted in the output. Explain each line along with the aspects of "buffers" if possible. Everything is listed below. Code below: #include <iostream> #include <string> using namespace std; int main() {    cout << boolalpha;    cout << static_cast<bool>(1) << endl;    cout << static_cast<bool>(0) << endl;    string s = "AAARGH!!!";    if (s.find("AAA")) { cout << 1 << endl; }    if (s.find("RGH")) { cout << 2 << endl;...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
in C# What Will Be The Output Of The Following Code Snippet:? using System; namespace ProgrammingExercise...
in C# What Will Be The Output Of The Following Code Snippet:? using System; namespace ProgrammingExercise { class FindOutput   { static void Main(string[] args)   { int i; int div = 8, num = 32; for (i = 0; i <= 10; i++) { if ((num / div * 3)== 6) { Console.WriteLine( i + " "); continue; } else if (i != 5) Console.Write(i + " "); else break;   }   Console.ReadLine();   } } }
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT