Please put name on back of last page

CSC101 Exam 1 Fall 2004 Miller

Closed Book if there was any question

1. What is the output of the following slightly modified book example? (Output should look like what you would get if you ran the program. Produce the correct number of lines.)

// Fig. 2.27: fig02_27.cpp

#include <iostream>

using std::cout;

using std::endl;

int main()

{

for ( int x = 1; x <= 10; x++ ) {

if ( x == 5 )

continue;

cout < x < " ";

} //

cout < "\nUsed " < endl;

return 0;

} // end function main

2. By now you have probably guessed that the first “problem” was just to give you some working code to help with the rest of the test. This is another offered for the same purpose. When you dig through this code you will find a statement that reads:

average = static_cast< double >( total ) / gradeCounter;

If average is a double, total is an integer = 876 and gradeCounter = 10, is an integer, then the code line given above would assigned a value of ______to average.

// Fig. 2.9: fig02_09.cpp

// Class average program with sentinel-controlled repetition.

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

using std::fixed;

#include <iomanip> // parameterized stream manipulators

using std::setprecision; // sets numeric output precision

// function main begins program execution

int main()

{

int total; // sum of grades

int gradeCounter; // number of grades entered

int grade; // grade value

double average; // Average with decimal point

// initialization phase

total = 0; // initialize total

gradeCounter = 0; // initialize loop counter

// processing phase

// get first grade from user

cout < "Enter grade, -1 to end: "; // prompt for input

cin > grade; // read grade from user

// loop until sentinel value read from user

while ( grade != -1 ) {

total = total + grade; // add grade to total

gradeCounter = gradeCounter + 1; // increment counter

cout < "Enter grade, -1 to end: "; // prompt for input

cin > grade; // read next grade

} // end while

// termination phase

// if user entered at least one grade ...

if ( gradeCounter != 0 ) {

// calculate average of all grades entered

average = static_cast< double >( total ) / gradeCounter;

// display average with two digits of precision

cout < "Class average is " < setprecision( 2 )

< fixed < average < endl;

} // end if part of if/else

else // if no grades entered, output appropriate message

cout < "No grades were entered" < endl;

return 0; // indicate program ended successfully

} // end function main

3. Fill in the blanks in each of the following sentences about the C++ environment.

a) The ______program transfers the executable image of a C++ program from disk to memory.

b) The ______program combines the output of the compiler with various library functions to produce an executable image.

c.C++ programs are normally typed into a computer using a(n) program ______.

d.In a C++ system, a(n) ______program executes before the compiler’s translation phase begins.

4. Write a statement (or comment) to accomplish each of the following: assume the using statements have been written.

a.State that the program will read two numbers and determine if they are equal.

(Use a comment)

b. Declare the variables b and c to be integer.

c.Assign the values the user enters to b and c.

d.Prompt the user to enter two integers.

e.If the values entered are the same write the message “SAME”.

f.Return a value from main indicating that the program terminated successfully

5.Identify and correct the errors (Correct it or point to it and say what’s wrong) in the following line of code.

If ( a => 0 ); cout > “a is non-negative. /n;

6.Write four different C++ statements that each add one to the variable x.

7.Given that sum is an integer that has been set to zero, use a for loop to calculate the sum of all of the odd integers from 1 to 99 ( 1 and 99 included in sum).

8. Work the previous problem using a while loop.

9.Identify the errors in each of the following

a. while (c <= 5){

product =* c;

b. If (gender = 1);

cout < “Woman” endl;

else;

cout < “Man” endl;

10.Give a code segment to produce the following output

1 5 10 15 20 25

2 30 35 40 45 50

3 55 60 65 70 75

.

.

.

40 980 985 990 995 1000

Define the variables you use, then give just the code segment that produces the required output.