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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.