Presentation is loading. Please wait.

Presentation is loading. Please wait.

Solutions to In-Class Problems

Similar presentations


Presentation on theme: "Solutions to In-Class Problems"— Presentation transcript:

1 Solutions to In-Class Problems
In-Class Project: The Box In-Class Project: Drawing a Rectangle CMSC 104, Version 9/01

2 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 9/01

3 The Box - Inputs and Outputs
height width depth Outputs volume surface area CMSC 104, Version 9/01

4 The Box - Rough Algorithm
Get the height from the user, performing error checking Get the width from the user, performing error checking Get the depth from the user, performing error checking Compute the volume of the box Compute the surface area of the box Display the height, width, depth, volume, and surface area of the box CMSC 104, Version 9/01

5 The Box - Final Pseudocode
Display “Enter the height: “ Read <height> While (<height> <= 0 ) Display “The height must be > 0” End_while CMSC 104, Version 9/01

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

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

8 The Box - Final 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 9/01

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

10 The Box - C Code #include <stdio.h> int readHeight( );
int readWidth( ); int readDepth( ); int computeVolume(int height, int width, int depth); int computeSurfaceArea(int height, int width, int depth); void printResults(int height, int width, int depth, int volume, int surfaceArea); CMSC 104, Version 9/01

11 The Box - C Code (con’t) int main( ) { int height; /* height of box */
int width; /* width of box */ int depth; /* depth of box */ int volume; /* volume of box */ int surfaceArea; /* surface area of box */ /* Get the height, width, and depth from the user */ height = readHeight( ); width = readWidth( ); depth = readDepth( ); CMSC 104, Version 9/01

12 The Box - C Code (con’t) /* Compute the volume of the box */
volume = computeVolume(height, width, depth); /* Compute the surface area of the box */ surfaceArea = computeSurfaceArea(height, width, depth); /* Display the height, width, depth, volume, and surface area */ printResults(height, width, depth, volume, surfaceArea); return 0; } CMSC 104, Version 9/01

13 The Box - C Code (con’t) /************************************************************ ** readHeight - gets the height of a box, making sure that it is > zero ** Inputs: None ** Outputs: height of the box ************************************************************/ int readHeight( ) { int height; /* box height */ do { printf("Enter the height: "); scanf("%d", &height); if (height <= 0) { printf("The height must be > 0\n"); } } while(height <= 0); return(height); CMSC 104, Version 9/01

14 The Box - C Code (con’t) /************************************************************ ** readWidth - gets the width of a box, making sure that it is > 0 ** Inputs: None ** Outputs: width of the box ************************************************************/ int readWidth( ) { int width; /* box width */ do { printf("Enter the width: "); scanf("%d", &width); if (width <= 0) { printf("The width must be > 0\n"); } } while(width <= 0); return(width); CMSC 104, Version 9/01

15 The Box - C Code (con’t) /************************************************************ ** readDepth - gets the depth of a box, making sure that it is > 0 ** Inputs: None ** Outputs: depth of the box ************************************************************/ int readDepth( ) { int depth; /* box depth */ do { printf("Enter the depth: "); scanf("%d", &depth); if (depth <= 0) { printf("The depth must be > 0\n"); } } while(depth <= 0); return(depth); CMSC 104, Version 9/01

16 The Box - C Code (con’t) /************************************************************ ** computeVolume - computes the volume of a box ** Inputs: height - box height ** width - box width ** depth - box depth ** Outputs: volume of the box ************************************************************/ int computeVolume(int height, int width, int depth) { int volume; /* box volume */ volume = height * width * depth; return(volume); } CMSC 104, Version 9/01

17 The Box - C Code (con’t) /******************************************************** ** computeSurfaceArea - computes the surface area of a box ** Inputs: height - box height ** width - box width ** depth - box depth ** Outputs: surface area of the box ********************************************************/ CMSC 104, Version 9/01

18 The Box - C Code (con’t) int computeSurfaceArea(int height, int width, int depth) { int surfaceArea; /* box surface area */ int surface1; /* area of a single surface */ int surface2; /* area of a single surface */ int surface3; /* area of a single surface */ surface1 = height * width; surface2 = width * depth; surface3 = height * depth; surfaceArea = 2 * (surface1 + surface2 + surface3); return(surfaceArea); } CMSC 104, Version 9/01

19 The Box - C Code (con’t) /************************************************************** ** printResults - displays the height, width, depth, volume, ** and surface area of a box ** Inputs - height - box height ** width - box width ** depth - box depth ** volume - box volume ** surface area - box surface area ** Outputs - None **************************************************************/ void printResults(int height, int width, int depth, int volume, int surfaceArea) { printf("Height = %d\n", height); printf("Width = %d\n", width); printf("Depth = %d\n", depth); printf("Volume = %d\n", volume); printf("Surface Area = %d\n", surfaceArea); } CMSC 104, Version 9/01

20 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 9/01

21 The Rectangle - Inputs and Outputs
height width Outputs the drawing of the rectangle CMSC 104, Version 9/01

22 The Rectangle - Rough Algorithm
Get the height from the user, performing error checking Get the width from the user, performing error checking Display the height and width Draw the rectangle CMSC 104, Version 9/01

23 The Rectangle - Final Pseudocode
Display “Enter the height: “ Read <height> While (<height> <= 0 ) Display “The height must be > 0” End_while CMSC 104, Version 9/01

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

25 The Rectangle - Final Pseudocode (con’t)
Display “Height = “, <height> Display “Width = “, <width> Skip a line CMSC 104, Version 9/01

26 The Rectangle - Final 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 9/01

27 The Rectangle - C Code #include <stdio.h> int readHeight( );
int readWidth( ); void displayDimensions(int height, int width); void drawRectangle(int height, int width); int main( ) { int height; /* rectangle height */ int width; /* rectangle width */ CMSC 104, Version 9/01

28 The Rectangle - C Code (con’t)
/* Get height and width from user */ height = readHeight( ); width = readWidth( ); /* Display the height and width */ displayDimensions(height, width); /* Draw the rectangle */ drawRectangle(height, width); return 0; } CMSC 104, Version 9/01

29 The Rectangle - C Code (con’t)
/************************************************************ ** readHeight - gets the height of a rectangle, making sure it is > 0 ** Inputs: None ** Outputs: height of the rectangle ************************************************************/ int readHeight( ) { int height; /* rectangle height */ do { printf("Enter the height: "); scanf("%d", &height); if (height <= 0) { printf("The height must be > 0\n"); } } while(height <= 0); return(height); CMSC 104, Version 9/01

30 The Rectangle - C Code (con’t)
/************************************************************ ** readWidth - gets the width of a rectangle, making sure it is > 0 ** Inputs: None ** Outputs: width of the rectangle ************************************************************/ int readWidth( ) { int width; /* rectangle width */ do { printf("Enter the width: "); scanf("%d", &width); if (width <= 0) { printf("The width must be > 0\n"); } } while(width <= 0); return(width); CMSC 104, Version 9/01

31 The Rectangle - C Code (con’t)
/******************************************************* ** displayDimensions - displays the height and width ** Inputs: height - rectangle height ** width - rectangle width ** Outputs: None *******************************************************/ void displayDimensions(int height, int width) { printf("\nHeight = %d\n", height); printf("Width = %d\n", width); } CMSC 104, Version 9/01

32 The Rectangle - C Code (con’t)
/******************************************************* ** drawRectangle - draws a solid rectangle of asterisks ** Inputs: height - rectangle height ** width - rectangle width ** Outputs: None *******************************************************/ void drawRectangle(int height, int width) { int i; /* loop counter */ int j; /* loop counter */ printf("\n"); for (i = 1; i <= height; i++) { for (j = 1; j <= width; j++) { printf("*"); } CMSC 104, Version 9/01


Download ppt "Solutions to In-Class Problems"

Similar presentations


Ads by Google