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

  CS10 Sec#25, # 29 : Topics covererd in Lab #5
T.A: Demetris Zeinalipour
(Winter 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 lab8.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 asks the user for two integers. The user should also specify "even" or "odd" (you may choose how they specify this). Now output all the specified types of numbers within the given range. Do not assume that they always enter the smallest value for the range before the largest range value. For example, if they enter 29 and 6 and they specify "odd", then your program should output all the odd values between 6 and 29 (inclusive):
7

9

11

13

15

17

19

21

23

25

27

29
Try to use a  function  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