Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions, Part 3 of 4 Topics: Coding Practice Reading: None

Similar presentations


Presentation on theme: "Functions, Part 3 of 4 Topics: Coding Practice Reading: None"— Presentation transcript:

1 Functions, Part 3 of 4 Topics: Coding Practice Reading: None
In-Class Project: The Box In-Class Project: Drawing a Rectangle Reading: None CMSC 104, Version 8/06

2 Coding Practice Let’s take the algorithms that we developed in “Algorithms, Part 3 of 3”, modularize them, and code them. First, let’s review the algorithms. Remember they are as a single monolithic program, not taking advantage of a key concept, functions! CMSC 104, Version 8/06

3 The 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. CMSC 104, Version 8/06

4 The Box - Pseudocode Display “Enter the height: “ Read <height>
While (<height> <= 0 ) Display “The height must be > 0” End_while CMSC 104, Version 8/06

5 The Box - Pseudocode (con’t)
Display “Enter the width: “ Read <width> While (<width> <= 0 ) Display “The width must be > 0” End_while CMSC 104, Version 8/06

6 The Box - Pseudocode (con’t)
Display “Enter the depth: “ Read <depth> While (<depth> <= 0 ) Display “The depth must be > 0” End_while CMSC 104, Version 8/06

7 The Box - Pseudocode (con’t)
<volume> = <height> X <width> X <depth> <surface1> = <height> X <width> <surface2> = <width> X <depth> <surface3> = <height> X <depth> <surface area> = 2 X (<surface1> + <surface2> + <surface3>) CMSC 104, Version 8/06

8 The Box - Pseudocode (con’t)
Display “Height = “, <height> Display “Width = “, <width> Display “Depth = “, <depth> Display “Volume = “, <volume> Display “Surface Area = “, <surface area> CMSC 104, Version 8/06

9 Drawing 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. CMSC 104, Version 8/06

10 The Rectangle - Pseudocode
Display “Enter the height: “ Read <height> While (<height> <= 0 ) Display “The height must be > 0” End_while CMSC 104, Version 8/06

11 The Rectangle - Pseudocode (con’t)
Display “Enter the width: “ Read <width> While (<width> <= 0 ) Display “The width must be > 0” End_while CMSC 104, Version 8/06

12 The Rectangle - Pseudocode (con’t)
Display “Height = “, <height> Display “Width = “, <width> Skip a line CMSC 104, Version 8/06

13 The Rectangle - Pseudocode (con’t)
<height counter> = 1 While ( <height counter> <= <height> ) <width counter> = 1 While ( <width counter> <= <width> ) Display “*” <width counter> = <width counter> + 1 End_while Place cursor on next line <height counter> = <height counter> + 1 CMSC 104, Version 8/06


Download ppt "Functions, Part 3 of 4 Topics: Coding Practice Reading: None"

Similar presentations


Ads by Google