Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ 1. Consider the loops below. Loop A: int i = 0; while( EXPR){ i++; } cout <<

ID: 665198 • Letter: C

Question

C++

1. Consider the loops below.

Loop A:
int i = 0;
while( EXPR){
i++;
}
cout << i << endl;

Loop B:
int i = 0;
do{
i++;
} while(EXPR);
cout << i << endl;

For each condition, what is the value of i that gets printed out after the execution of each loop?

2.

What will be printed when this loop is executed? Notice the lack of braces, the lack of new lines, and the spaces printed.

int x;
for (x = 5; x <= 14; x += 3)
cout << x << “ “;
cout << x;

3.

What is a sentinel?

4.

Each of the different scopes in this program is labeled. Pay attention to which variables are in scope for each scope.

#include <iostream>
using namespace std;

// Scope A


// This function determines how many people can fit on an elevator
// Assumes a person weighs 200 pounds and takes up 4 square feet of space
int fillElevator(int poundLimit, int areaLimit){
// Scope B
int people = 0; // number of people so far
int pounds = 0; // collective weight of people
int space = 0; // amount of space people take up
while((pounds + 200) < poundLimit && (space + 4) < areaLimit){
// Scope C
people++;
pounds += 200;
space += 4;
}
return people;
}


// This program calculates how many people
// can fit on an elevator with a given weight
// limit and a given size
int main(){
// Scope D
int areaOfElevator = 30; // elevator has an area of 30 ft squared

for(int maxPounds = 800; maxPounds < 2000; maxPounds += 400){
// Scope E
int numPeople = fillElevator(maxPounds, areaOfElevator);
cout << "The elevator can fit " << numPeople << " people." << endl;
}
return 0;
}

For each symbol below, list all of the scopes where that symbol would be “in scope”. Do not put spaces or punctuation in your answer and list them in alphabetical order. For example, if a variable were in scope A, B and E, put ABE. You will get it wrong if you don't follow these instructions.

fillElevator  
space  
poundLimit    
maxPounds  
numPeople  
areaOfElevator

5.

If you put using std::cout at the top of your source file, you can still define a function named cout and override the definition found in namespace std.

False

5.

Which of the following is NOT an acceptable way to signify you want to use the string class from namespace std?

using namespace std::string;

6.

What is the output of this program?

#include <iostream>

namespace Scanner {
void printError();
}

namespace Parser {
void printError();
}

void printError();


int main(){
{
using Scanner::printError;
Scanner::printError();
printError();
Parser::printError();
}
}

void Scanner::printError(){
cout << “You’ve encountered a scanning error” << endl; // SPE
}

void Parser::printError(){
cout << “You’ve encountered a parsing error” << endl; // PPE
}

void printError(){
cout << “You’ve encountered an error” << endl; // GPE
}

You’ve encountered a scanning error
You’ve encountered a scanning error
You’ve encountered a scanning error

You’ve encountered a scanning error
You’ve encountered a scanning error
You’ve encountered a parsing error

You’ve encountered a scanning error
You’ve encountered an error
You’ve encountered a parsing error

You’ve encountered a scanning error
You’ve encountered an error
You’ve encountered a scanning error

EXPR A    B    i < 0       i < 5       i <= 0      

Explanation / Answer

1.Answer:

EXPR

A   

B   

i < 0

0

1

i < 5

5

5

i <= 0

1

1


2. Answer:

5 8 11 14 17 //inside for loop printing is 5 to 14 and outer of for is 17, seperate by space

3) Answer:

A value used to mark the end of iteration or mark the end of a list of values.

//every loop ends with target value and a string array is delimited with sentinel character ''(null)

4) Answer:

fillElevator – ABC // fillElevator is function, it start with Scope A and ends with C
space – BC // local variable delaration at B and accessed in C
poundLimit – ABC //It is a formal parameters accessible in A to C scope
maxPounds – DE //Local variable to only begining and end of for loop
numPeople – E //Declared inside loop so scope is E
areaOfElevator - DE // declare in scope D and accessing in E

5. Answer: True //overriding an existing function is concept of polymorphism in C++

6. Answer:

You’ve encountered a scanning error

You’ve encountered a scanning error

You’ve encountered a parsing error

7) Answer:

using std::string; //error, because “using” is keyword for including “namespace”, namespace word is missing here

EXPR

A   

B   

i < 0

0

1

i < 5

5

5

i <= 0

1

1