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

Slides:



Advertisements
Similar presentations
In our lesson today we will learn how to find the area of a building.
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
PSEUDOCODE & FLOW CHART
Algorithms IV: Top-Down Design
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CMSC 104, Version 9/01 1 The Box Problem: Write an interactive program to compute and display the volume and surface area of a box. The program must also.
CMSC 104, Version 9/011 Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading None.
CMSC 104, Section 301, Fall Lecture 06, 9/18/02 Algorithms, Part 3 of 3 Topics Disk Quota Exceeded. Using Pine. More Algorithms Reading Read.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMSC 104, Version 8/061L17Top-DownDesign.ppt Top-Down Design Topics Top-Down Design Top-Down Design Examples The Function Concept Reading Sections 3.1.
CMSC 104, Version 9/01 1 Functions, Part 3 of 3 Topics: Coding Practice o In-Class Project: The Box o In-Class Project: Drawing a Rectangle Reading: None.
1 Algorithms Practice Topics In-Class Project: Tip Calculator In-Class Project: Drawing a Rectangle.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
The ‘while’ loop ‘round and ‘round we go.
Algorithms, Part 3 of 3 Topics In-Class Project: The Box
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CMSC 104, Version 8/061L16IncrementalProg.ppt Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
- Standard C Statements
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Lecture 7: Repeating a Known Number of Times
solve the following problem...
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
The while Looping Structure
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
2008/11/12: Lab 5/Lecture 17 CMSC 104, Section 0101 John Y. Park
Variables in C Topics Naming Variables Declaring Variables
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Incremental Programming
1) C program development 2) Selection structure
The while Looping Structure
Variables in C Topics Naming Variables Declaring Variables
The ‘while’ loop ‘round and ‘round we go.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Algorithms, Part 3 of 3 Topics In-Class Project: The Box
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Solutions to In-Class Problems
Algorithms Practice Topics In-Class Project: Tip Calculator
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 4 of 4 Topics: Coding Practice Reading: None
More Loops Topics Relational Operators Logical Operators for Loops.
Assignment Operators Topics Increment and Decrement Operators
Variables in C Topics Naming Variables Declaring Variables
Assignment Operators Topics Increment and Decrement Operators
UMBC CMSC 104 – Section 01, Fall 2016
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
The while Looping Structure
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Incremental Programming
The while Looping Structure
Assignment Operators Topics Increment and Decrement Operators
More Loops Topics Counter-Controlled (Definite) Repetition
Area and Volume How to … Calculate the area of a square or rectangle
Algorithms, Part 3 of 3 Topics In-Class Project: Tip Calculator
Functions, Part 3 of 4 Topics: Coding Practice Reading: None
Assignment Operators Topics Increment and Decrement Operators
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

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

Coding Practice Let’s take the algorithms that we developed in “Algorithms, Part 3 of 3”, modularize them, and code them. CMSC 104, Version 9/01

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

Hierarchy Chart main Height function Width function Depth function CMSC 104, Version 9/01

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

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

The Box – Pseudocode for depth function Display “Enter the depth: “ Read <depth> While (<depth> <= 0 ) Display “The depth must be > 0” End_while Return depth CMSC 104, Version 9/01

The Box - Pseudocode (con’t) Call height_function saving answer in <height> Call width_function saving answer in <width> Call depth_function saving answer in <depth> <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

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

Code the Design #include <stdio.h> int height_function( void ); int width_function( void ); int depth_function( void ); CMSC 104, Version 9/01

main( ) CMSC 104, Version 9/01 int main( void ) { int height, width, depth, volume; int surface1, surface2, surface3, surface_area; height = height_function( ); width = width_function( ); depth = depth_function( ); volume = height * width * depth; surface1 = height * width; surface2 = width * depth; surface3 = height * depth; surface_area = 2 * (surface1 + surface2 + surface3); 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", surface_area ); return 0; } CMSC 104, Version 9/01

height_function( ) int height_function( void ) { int height; printf( "Enter the height: " ); scanf( "%d", &height); while( height <= 0 ) printf( "The height must be > 0\n" ); } return height; CMSC 104, Version 9/01

width_function int width_function( void ) { int width; printf( "Enter the width: " ); scanf( "%d", &width ); while( width <= 0 ) printf( "The width must be > 0" ); } return width; CMSC 104, Version 9/01

depth_function( ) int depth_function( void ) { int depth; printf( "Enter the depth: " ); scanf( "%d", &depth ); while( depth <= 0 ) printf( "The depth must be > 0" ); } return depth; CMSC 104, Version 9/01

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

Hierarchy Chart main Height function Width function Draw solid line Hollow line CMSC 104, Version 9/01

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

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

The Rectangle – Pseudocode function Draw_solid_line Receive width_size Set I to 0 While ( I < width_size ) Display “*” add 1 to I Display “\n” CMSC 104, Version 9/01

The Rectangle – Pseudocode function Draw_hollow_line Receive <width_size> Display “*” Set I to 0 While ( I < <width_size> - 2 ) Display “ ” add 1 to I Display “*\n” CMSC 104, Version 9/01

The Rectangle - Pseudocode main function Call Height_function saving answer in <height> Call Width_function saving answer in <width> Skip a line CMSC 104, Version 9/01

The Rectangle - Pseudocode (con’t) Call Draw_solid_line sending <width> Set height_counter to 1 While ( <height counter> <= <height - 2> ) call Draw_hollow_line sending width <height counter> = <height counter> + 1 End_while Call Draw_solid_line sending width CMSC 104, Version 9/01

#include <stdio.h> int height_function( void ); int width_function( void ); void draw_solid_line( int width_size ); void draw_hollow_line( int width_size ); CMSC 104, Version 9/01

main( ) CMSC 104, Version 9/01 int main( void ) { int height; int width; int height_counter; height = height_function( ); width = width_function( ); printf( "\n" ); draw_solid_line( width ); height_counter = 1; while ( height_counter < ( height - 2 ) ) draw_hollow_line( width ); height_counter++; } return 0; CMSC 104, Version 9/01

height_function( ) – software reuse int height_function( void ) { int height; printf( "Enter the height: " ); scanf( "%d", &height); while( height <= 0 ) printf( "The height must be > 0\n" ); } return height; CMSC 104, Version 9/01

width_function( ) – software reuse int width_function( void ) { int width; printf( "Enter the width: " ); scanf( "%d", &width ); while( width <= 0 ) printf( "The width must be > 0" ); } return width; CMSC 104, Version 9/01

draw_solid_line( ) void draw_solid_line( int width_size ) { int i; while ( i < width_size ) printf( "*" ); i++; } printf( "\n" ); CMSC 104, Version 9/01

draw_hollow_line( ) void draw_hollow_line( int width_size ) { int i; printf( "*" ); i = 0; while( i < ( width_size - 2 ) ) printf( " " ); i++; } printf( "*\n" ); CMSC 104, Version 9/01