5. Show the output of each of the following C++ program segments: int j; int one
ID: 3768537 • Letter: 5
Question
5. Show the output of each of the following C++ program segments:
int j;
int one[5], two[10];
for (j = 0; j < 5; j++)
one[j] = 5*j + 3;
cout << “One cntains: “;
for (j = 0; j < 5; j++)
cout << setw(3) << one[j];
cout << endl;
for (j = 0; j < 5; j++)
{ two[j] = 2*one[j] - 1;
two[j + 5] = one[4 - j] – 1 + two [j];
}
cout << “Two cntains: “;
for (j = 0; j < 10; j++)
cout << setw(3) << two[j];
cout << endl;
**********************************************************************************
const int n = 6;
int L[n] = {3, 5, 5, 4, 9, 9};
bool valu = true;
int k = 0;
while ( (k <n-1) && (valu) )
{
if (L[k] > L[k+1])
{
valu = false;
cout << setw(2) << k << setw(2) <<L[k] << endl;
}
else
k++;
}
if (valu)
cout << “ The list is OK” << endl;
Explanation / Answer
SEGMENT 1:
Output:
One cntains: 3 8 13 18 23
Two cntains: 5 15 25 35 45 27 32 37 42 47
SEGMENT 2:
Output:
Try this code:
segment 1:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int j;
int one[5], two[10];
for (j = 0; j < 5; j++)
one[j] = 5*j + 3;
cout << "One cntains:";
for (j = 0; j < 5; j++)
cout << setw(3) << one[j];
cout << endl;
for (j = 0; j < 5; j++)
{
two[j]=2*one[j]-1;
two[j+5] = one[4-j] - 1 + two[j];
}
cout << "Two cntains:";
for (j = 0; j < 10; j++)
cout << setw(3) << two[j];
cout << endl;
return 0;
}
segment 2:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const int n = 6;
int L[n] = {3, 5, 5, 4, 9, 9};
bool valu = true;
int k=0;
while ( (k <n-1) && (valu) )
{
if (L[k] > L[k+1])
{
valu = false;
cout << setw(2) << k << setw(2) <<L[k] << endl;
}
else
k++;
}
if (valu)
cout << "The list is OK" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.