COSC 1320 - C++ Programming
Bob Comer, Professor, CIT/COSC


Exam 1 Review Exercises

General Information on Exams

See the course syllabus for important information on exams. Here are some more general notes on exams:

I recommend that everyone use the official textbook for this course, C++ Programming by D. S. Malik. If you have Starting Out with C++ Brief 2nd Edition by Tony Gaddis, click here for review questions. If you have C++ How to Program by Deitel and Deitel, click here for review questions.

Review Questions from Malik textbook

To prepare for Exam 1, I recommend that you look over these exercises taken from the Malik book. I will NOT grade these exercises.

The answers to selected Exercises from the textbook are in Appendix H. Click here for answers that are not in the textbook. Feel free to discuss the answers with your classmates or me (this is a good place to use the class listserve).

Note: There will be no questions over strings on Exam 1. Strings will be covered on exam 2.

Chapter 1

Exercises: 1, 8.

Chapter 2

Exerxises: 1-6, 8-10, 12, 14, 17, 20.

Chapter 3

The use of files will not be covered on exam 1 (it will be covered on a later exam). The C++ string class will not be covered on the exam.

Exercises: 1-4, 7.

Programming Exercises: 2, 5.

Other questions:

1. Write a C++ statement that displays the value of floating-point variable rate in a field 8 characters wide with exactly one digit to the right of the decimal.

2. How do you enter a newline character from the keyboard?

3. How do you output a newline character from your program?

Chapter 4

Exercises: 1-11.

Programming Exercises: 1.

Other questions:

1. A company pays its sales people $200 per week plus 9 percent of their gross sales for the week. Write declarations for variables to hold a salesperson's gross weekly sales and weekly pay. Write a statement to calculate the weekly pay and store it in your variable.

2. Given float variables predictedHigh and actualHigh, write a C++ statement or statements that print:

3. Write a C++ statement that means the same thing as this math expression:

0 < length < 10

Chapter 5

EOF-controlled loops will not be covered on exam 1 (they will be covered on a later exam).

Exercises: 1-7, 9, 10, 12, 17, 23, 25, 26.

Other questions:

1. The following loop is supposed to print the odd integers from 1 through 5.

count = 1;
while (count != 6)
{
   count++;
   cout << count << ' ';
}

a. What is printed by the loop?
b. Rewrite the loop so that it works correctly.

2. When the following code is inserted in a program and executed, it reads the first price and then the computer "hangs". Find the problem and correct it.

int quantity;
float price, cost;

cout << "Enter item price (or -1 to quit): ";
cin >> price;
while (price > 0);
{
   cout << "Enter quantity for this item: ";
   cin >> quantity;
   cout << endl;
   cost = price * quantity;
   cout << "The cost for " << quantity
        << " items at " << price
        << " each is " << cost
        << endl;
   cout << "Enter item price (or -1 to quit): ";
   cin >> price;
}

3. Write C++ code to read 20 integers and count how many times the integer 100 appears in the input. You must use a loop.

4. Write a loop that sums the integers from 1 through 10.

5. Write a loop that reads a list of integers from the keyboard and counts how many negative integers are input and how many positive integers are input. The loop should stop when zero is input. Then print the count of negative numbers and the count of positive numbers with appropriate labels.

6. Write a switch statement to convert a letter grade to a numeric value based on the following table. Use char variable ltrGrade and int variable numGrade.

letter grade

Numeric value

A

4

B

3

C

2

D

1

F

0

Notes on error checking

Some students get in the habit of doing input validation in "tight" validation loops as in the following example. In a loop like this the user is forced to stay in the validation loop until they enter a valid value. If they get totally confused they have no way to break out of the loop.

// Find the average of a list of grades.

// Valid grades are in the range 0 - 100.

// The list ends with a sentinel of -1.

int grade, sum = 0, count = 0;

cout << "Enter next grade (or -1 to stop): ";

cin >> grade;

while ( grade != -1 )

{

while ( grade < 0 || grade > 100 )

{

cout << "Invalid grade - please re-enter" << endl;

cout << "Valid grades are in the range 0 - 100" << endl;

cout << "Enter next grade (or -1 to stop): ";

cin >> grade;

}

sum = sum + grade;

count++;

cout << "Enter next grade (or -1 to stop): ";

cin >> grade;

}

cout << "average is" << ((float) sum / count) << endl;

The following example of error checking gives the user more flexibility.

// Find the average of a list of grades.

// Valid grades are in the range 0 - 100.

// The list ends with a sentinel of -1.

int grade, sum = 0, count = 0;

cout << "Enter next grade (or -1 to stop): ";

cin >> grade;

while ( grade != -1 )

{

if ( grade < 0 || grade > 100 )

{

cout << "Invalid grade - please re-enter" << endl;

cout << "Valid grades are in the range 0 - 100" << endl;

}

else

{

sum = sum + grade;

count++;

}

cout << "Enter next grade (or -1 to stop): ";

cin >> grade;

}

cout << "average is" << ((float) sum / count) << endl;

 

Chapter 6

Exercises: 1, 3, 4, 6, 8.

Programming Exercises: 2.

Chapter 7

Exercises: 1-3, 5, 8.

Programming Exercises: 2.

Other questions:

1. Write a possible prototype for function func() below.

int main()
{
   double d;
   float f = 6.2;
   int i = 3;
   d = func( i, f );
   cout << "The result is " << d << endl;
   return 0;
}

2. Write an example call for the following function.

float square( float num)
{
   return num * num;
}

3. True or False: To use a constant (sometimes called a constant variable) throughout your program, it should be declared inside your main() function.

4. Write a function that receives a float parameter called num and calculates and returns the fourth power of num (num * num * num * num). Try using a local variable in your function.

5. Write a function that prints the sentence:

Your age is ___ years.

In the blank space in the sentence it should print the value of an integer parameter called age.

6. Write a function that prompts the user to enter a number of hours worked and a pay rate. Your function should then input two values into float parameters called hoursWorked and payRate. Your function will need to supply the two input values to the calling function.

7. The following program will not compile. Find the problem and fix it.

#include <iostream.h>

void PrintCurved( int grade );

int main()
{
   int studentNo, grade;
   cout << "Enter student number and grade:";
   cin >> studentNo >> grade;
   PrintCurved(grade);
   return 0;
}

void PrintCurved( int grade )
{
   cout << "The curved grade for student ";
        << studentNo << " is " 
        << grade + 10 << endl;
}

8. Write a function that receives two float values as parameters and "returns" each value halved. Write an example call for your function. Write declarations for all variables that you use.

9. Write an overloaded version of your function in the previous problem that works on int values.

10. Write a statement that causes all floating point numbers to be printed with exactly 1 place to the right of the decimal point.


Return to C++ Home Page

Copyright: Ó 2001 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to:
Bob Comer
Last updated: October 2, 2001