COMPUTER SCIENCE I

First Test on Analysis, Design and Basic C++ Coding

Fall 1999

 

Please put the answers for this test on separate paper.

 

1. A specialty grocery store wants its own check out program. The preliminary version of this program should allow a clerk to enter in both the item code (Upper case or lower case letters) and the price of the item for as many items as a customer wants to purpose. (Later versions of this program will read from a scanner but that is not to be dealt with now.) Each week some set of items will go on sale for 10% less than the sticker price. These will have item codes in the range ‘A’ to ‘D’ and ‘W’ to ‘Z’. For example, if an item has a code of ‘C’ and a list price of $10.00, the price to the customer should be $9.00. The output should consist of the total cost to the customer and the number of items purchased. Your tasks are to:

 

  1. On your own paper, do a complete interface analysis and determine if there are any constants. Note: Do NOT write a problem narrative – although you should make sure you completely understand the problem yourself or ask the instructor. (15 points).
  2. State which, if any, patterns will be needed. ( 8 points)
  3.  

  4. Will any sentinel values be needed? If so, propose good one(s). If none are needed, explain why. (5 points)
  5.  

  6. Write the code for this program. You do not need an algorithm. Write your code carefully because it will be graded partially on the basis of style. (25 points)

 

Be sure you understand the problem! If there is anything you do not understand, SEE THE INSTRUCTOR!!!

 

2. For some unknown reason management wants the program in number 1 to stop when the user enters an item code of ‘M’ or ‘N’. Rewrite only the while test condition in the code above so to handle this. (5 points)

 

 

3. For the following two lines of code use the formal language used in class to explain what a computer does when it executes the code. (10 points)

 

a) double x;

 

b) cin >> y;

 

4. Below are two code fragments. For each one:

 

a) Demonstrate what will happen (including what will be output) by performing a trace according to the methodology described in the text and in class.

 

b) How many times will a user have to enter a value? Be sure to explain how your trace shows the answer to this.

 

2 Traces (14 points total) 2 Explanations (8 points total)

 

 

4.1) int count = 0;

int num;

int sum = 5;

while ( count < 5)

{ cout << "Please enter a number";

cin >> num;

sum += num;

}

cout << "The sum is: " << sum;

 

 

 

4.2) int count = 0;

int num;

if ( count < 10)

{ cout << "Please enter a number";

cin >> num;

count++;

}

cout << count;

 

 

5. Write a program using a loop instruction other than the ‘while’ that reads in 10 grades and outputs the sum. Be sure to use the coded style of the text and the instructor. (10 points)