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

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

Print this Page   
 [  Class Notes  ::   PseudoCode of Problem  ::   Function VS Functionless Example  ::   Math in C++  ::   Exercise #6 ]



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


Math operations in C++

Example.

/* sin example */
#include <stdio.h>
#include <math.h>


#define PI 3.14159265

main ()
{
double param, result;
param = 45;
result = sin (param*PI/180);
printf ("Sine of %lf degrees is %lf\n", param, result );
return 0;
}
Output:
Sine of 45.000000 degrees is 0.707107
Top


Lab 6 Assignment - Exercise

Write a program that finds the missing information of a right triangle. Prompt the user for the angle A1 and the length of the side X. Then your program should calculate the missing angle and the missing sides. Pctorially (using characters of your choice) draw out a triangle with each angle and side shown. Use pre-packaged functions where possible.


Use at least a and a  function  structure and a  procedure  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