Home >  T.A >  cs10 ( Spring 2002 ) >  Lab 5

  CS10 Sec#24,29,30 : Topics covered in Lab #5
T.A: Demetris Zeinalipour
(Spring 2002)
Print this Page   
 [  Class Notes  ::   PseudoCode of Problem  ::   Function VS Functionless Example  ::   Exercise #5 ]



Functions & Procedure Notes

When given a problem, beside the analysis, design of the algorithm and testing and verification (which comes on the end), you should be able to identify the parts of your program that are (or will be) repentantly used within your program. In other words you should try to identify which parts of your program will be redundant, take these fragments of code and place them in appropriate functions and procedures eliminate any kind of redundancy in your code.

Redundant code is costly to maintain!

Moreover splitting your program will make your code cleaner and more understandable
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
   

Top


Is it better to use functions and procedures although the source code lines may increase?

View C++ Source Code WITHOUT FUNCTIONS for above Example here

View C++ Source Code WITH FUNCTIONS for above Example here
Top


Lab 5 Assignment - Exercise
New! - Download lab5.exe (win32) here and see how your program should respond to various inputs,
See Solution - C++ solution here (will be available on coming Saturday)


Write a program that computes the approximate age of an artifact using carbon-14 dating. Allow the user to continue doing this as many times as they want. the carbon-14 dating formula is shown below, where age is the approximate age in years and the percentage of carbon14 remaining is a fraction of 1.0.

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