Home >  T.A >  cs10 ( Fall 2001 ) >  Lab 4

  CS10 Sec#24 : Topics covererd in Lab #4
T.A: Demetris Zeinalipour
(Fall 2001)

Print this Page   
 [  Academic Dishonesty Policy  ::   Lecture Notes  ::   Lecture Example  ::   Statements Table  ::   Exercise #4 ]



Academic Dishonesty Policy

If you haven't attended last Lab please sign the Cheating Policy Sheet.
Top


Problem Solving

When given a problem, one should transform the description of the problem into the solution of the problem using the following steps:
  1. Understand the problem thoroughly. Eliminate any ambiguities in the problem description.
  2. Analysis.
    • Identify the inputs needed for the problem.
    • Identify any special restraints or conditions of the input (Ex. range of values).
    • Identify what output or outputs the problem will produce.
    • Know how the output should be formatted.
    • Be sure to understand any formulas given to be used in the problem.
  3. Design of Algorithm. An algorithm is a step-by-step set of instructions. Use pseudocode, a semiformal English-like language that shows the order of the steps to be taken in an outline type form (as opposed to a paragraph).
  4. Testing and Verification. Be sure that the program meets the problem requirements and the output produced is correct.
    When testing:
    • Use a large set of inputs, one or two inputs producing a correct output, does not guarantee that the program ALWAYS produces a correct output.
    • Choose inputs that test each possible path through the program. (When using conditions, every possible result of the condition should be tested.)
    • Test to make sure that the special restraints identified earlier are accounted for. Choose test input that falls inside the range, right at the edge of the range, and outside the range.
Top


Problem Solving Example:

Write a program that allows the user to calculate the area of a rectangle as many times as they want.

A) Input: length, width, continue/quit
B) Restrictions: inputs must be positive
C) Output: area
D) Formula: area = length * width

Pseudocode for algorithm (there are many different ways to write pseudocode, two are shown):
Pseudocode Example 1 Pseudocode Example 2
1. Ask user to input length 1. Repeat until the user wants to stop
2. Read in length 2. Repeat until the length is positive
3. If length is not positive, return to step 1 3. Ask user to input length
4. Ask user to input width 4. Read in length
5. Read in width 5. Repeat until the width is positive
6. If width is not positive, return to step 4 6. Ask user to input width
7. Calculate area   7. Read in width
8. Output area   8. Calculate area
9. Ask user if they want to do it again 9. Output area
10. Read in answer 10. Ask user it they want to do it again
11. If the answer is to continue, return to step 1 11. Read in user's response
   


Selection Structure Program - make sure they have used both an  if and a  switch

View C++ Source Code for above Example here
Top


Statements Reference Table

Top


Lab 4 Assignment-Exercise

Write a program that lets the user enter a letter, then output the number associated with the given letter on the telephone. No number corresponds to either Q or Z. For these letters, your program should print a message indicating that they are not used on a telephone. ABC=2, DEF=3, GHI=4, JKL=5, MNO=6, PRS=7, TUV=8, WXY=9. Your program will only recognize upper case letters, for all other characters output that it is an invalid character. After outputting the digit (if there is one associated with the character entered), ask the user if they desire to see the written form of the digit and print it if they desire.

For example, if the user entered W, your program would print something like:

W is associated with the digit 9

Would you like to see the digit written out? (y/n): y

9 is written as nine


Use both an  if  structure and a  switch  structure in this program.

Notice: Be sure to put all your information (login, lab section, …) at the top of your program. Your lab programs DO NOT get turned in electronically. I will come around and check you off individually.

Top