Title: Functions, Part 3 of 3
1Functions, Part 3 of 3
- Topics
- Coding Practice
- In-Class Project The Box
- In-Class Project Drawing a Rectangle
- Reading
- None
2Coding Practice
- Lets take the algorithms that we developed in
Algorithms, Part 3 of 3, modularize them, and
code them.
3The Box
- Problem Write an interactive program to compute
and display the volume and surface area of a box.
The program must also display the box
dimensions. Error checking should be done to be
sure that all box dimensions are greater than
zero.
4The Box - Pseudocode
- Display Enter the height
- Read ltheightgt
- While (ltheightgt lt 0 )
- Display The height must be gt 0
- Display Enter the height
- Read ltheightgt
- End_while
5The Box - Pseudocode (cont)
- Display Enter the width
- Read ltwidthgt
- While (ltwidthgt lt 0 )
- Display The width must be gt 0
- Display Enter the width
- Read ltwidthgt
- End_while
6The Box - Pseudocode (cont)
- Display Enter the depth
- Read ltdepthgt
- While (ltdepthgt lt 0 )
- Display The depth must be gt 0
- Display Enter the depth
- Read ltdepthgt
- End_while
7The Box - Pseudocode (cont)
- ltvolumegt ltheightgt X ltwidthgt X ltdepthgt
- ltsurface1gt ltheightgt X ltwidthgt
- ltsurface2gt ltwidthgt X ltdepthgt
- ltsurface3gt ltheightgt X ltdepthgt
- ltsurface areagt 2 X (ltsurface1gt
ltsurface2gt -
ltsurface3gt)
8The Box - Pseudocode (cont)
Display Height , ltheightgt Display Width ,
ltwidthgt Display Depth , ltdepthgt Display
Volume , ltvolumegt Display Surface Area ,
ltsurface areagt
9Drawing a Rectangle
- Problem Write an interactive program that will
draw a solid rectangle of asterisks (). The
program must also display the dimensions of the
rectangle. Error checking must be done to be
sure that the dimensions are greater than zero.
10The Rectangle - Pseudocode
- Display Enter the height
- Read ltheightgt
- While (ltheightgt lt 0 )
- Display The height must be gt 0
- Display Enter the height
- Read ltheightgt
- End_while
11The Rectangle - Pseudocode (cont)
- Display Enter the width
- Read ltwidthgt
- While (ltwidthgt lt 0 )
- Display The width must be gt 0
- Display Enter the width
- Read ltwidthgt
- End_while
12The Rectangle - Pseudocode (cont)
- Display Height , ltheightgt
- Display Width , ltwidthgt
- Skip a line
13The Rectangle - Pseudocode (cont)
- ltheight countergt 1
- While ( ltheight countergt lt ltheightgt )
- ltwidth countergt 1
- While ( ltwidth countergt lt ltwidthgt )
- Display
- ltwidth countergt ltwidth countergt 1
- End_while
- Place cursor on next line
- ltheight countergt ltheight countergt 1
- End_while