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? int list [] = {5, 7, 6,

ID: 3696307 • Letter: W

Question

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

int list [] = {5, 7, 6, 4, 8, 15, 32, 40};

for (auto num: list)

cout << num / 2 << " ";

cout << endl;

2.What is the output of the following C++ program segment?

string names [] = {"Blair, Cindy", "Johnson, Chris", "Mann, Sheila"};

string str1, str2;

char ch = ',';

int pos, length;

for (auto &str: names)

{

pos = str.find(ch);

length = str.length();

str1 = str.substr(0, pos);

str2 = str.substr(pos + 2, length - pos - 1);

str = str2 + ' ' + str1;

}

for (auto str: names)

cout << str << endl;

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
int main()
{
int list [] = {5, 7, 6, 4, 8, 15, 32, 40};
for (auto num: list)
cout << num / 2 << " ";
cout << endl;
}

OutPut: 2 3 3 2 4 7 16 20

#include <iostream>
#include <string>
using namespace std;
int main()
{
string names [] = {"Blair, Cindy", "Johnson, Chris", "Mann, Sheila"};
string str1, str2;
char ch = ',';
int pos, length;
for (auto &str: names)
{
pos = str.find(ch);
length = str.length();
str1 = str.substr(0, pos);
str2 = str.substr(pos + 2, length - pos - 1);
str = str2 + ' ' + str1;
}
for (auto str: names)
cout << str << endl;
}

OUTPUT:

Cindy Blair

Chris Johnson

Sheila Mann

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