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


Exam 1 Review Exercises

To prepare for Exam 1, I recommend that you look over these exercises taken from the ends of the chapters in your textbook. I will NOT grade these exercises. Some answers are given in the book, the other answers are here. Feel free to discuss the answers with your classmates or me (this is a good place to use the listserve).

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

Chapter 1

Self-Review Exercises: 1.1 (exclude a, b, g, h, I), 1.2 through 1.9.

Chapter 2

Self-Review Exercises: 2.1 through 2.13, (exclude 2.3 a and c).

Exercises: 2.14.

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. How do you enter a newline character from the keyboard? How do you output a newline character from your program?

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

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

0 < length < 10

5. 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.

6. 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;
}

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

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

9. 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.

10. 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

Other notes on chapter 2

There is an example of the "classic" sentinel-controlled loop on pages 73 - 74 of your textbook. Problems 6 and 9 above also illustrate sentinel-controlled loops. Figure 2.22 on pages 96 - 97 of your textbook illustrates a "compressed" version of a sentinel-controlled loop where the input is done in the loop condition. If you understand how this loop works, it is fine to use it. However, last semester a number of students were confused by this example and used it incorrectly in their programs and on exams. My recommendation is to use the "classic" style if you have any questions about how the "compressed" loop works.

Many students also have problems with using cin.get() as illustrated in the example in Figure 2.22. The authors of this textbook have a habit of using C++ features in examples without fully explaining them. This causes problems for students who do not understand all the implications of using that particular feature. If you do not have a good understanding of C++ i/o streams (covered in chapter 11 of this textbook), I would be cautious about using the get() function.

Chapter 3

Self-Review Exercises: 3.1 (exclude parts i, j, m, t), 3.2 (exclude part f), 3.4, 3.5, 3.6 (exclude parts a, c), 3.7, 3.8, 3.9.

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 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 nteger 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.

Other notes on Chapter 3

The following sections of chapter 3 will not be covered on the exam.

3.8 Random number generation

3.12 - 3.14 Recursion is an important topic in more advanced programming courses. I encourage you to study these sections of chapter 3, but they will not be covered on the exam.

3.15 Functions with empty parameter lists

3.16 Inline functions

3.18 Default arguments

3.21 Function Templates


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: February 10, 2001