Solutions to In-Class Problems

Slides:



Advertisements
Similar presentations
CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Advertisements

For loops For loops are controlled by a counter variable. for( c =init_value;c
Guidelines for working with Microsoft Visual Studio.Net.
ICS103 Programming in C Lecture 9: Functions I
Algorithms IV: Top-Down Design
Chapter 1 Pseudocode & Flowcharts
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.
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
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.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
How to design and code functions Chapter 4 (ctd).
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
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 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
WEEK 8 Class Activities Lecturer’s slides.
Computer Programming for Engineers
1 ICS103 Programming in C Lecture 9: Functions I.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
CMSC 104, Version 8/061L16IncrementalProg.ppt Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading.
Repetition statements
ECE Application Programming
Introduction to Computer Algorithmics and Programming Ceng 113
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Lecture 7: Repeating a Known Number of Times
Week 4 – Repetition Structures / Loops
solve the following problem...
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
The while Looping Structure
2008/11/12: Lab 5/Lecture 17 CMSC 104, Section 0101 John Y. Park
Lec 7.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Incremental Programming
Chapter 8 The Loops By: Mr. Baha Hanene.
1) C program development 2) Selection structure
The while Looping Structure
The ‘while’ loop ‘round and ‘round we go.
UMBC CMSC 104 – Section 01, Fall 2016
Arrays, Part 1 of 2 Topics Definition of a Data Structure
UMBC CMSC 104 – Section 01, Fall 2016
Algorithms, Part 3 of 3 Topics In-Class Project: The Box
Algorithms Practice Topics In-Class Project: Tip Calculator
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions, Part 4 of 4 Topics: Coding Practice Reading: None
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
Functions, Part 3 of 3 Topics: Coding Practice Reading: None
UMBC CMSC 104 – Section 01, Fall 2016
The while Looping Structure
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Incremental Programming
CSCE 206 Lab Structured Programming in C
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
Algorithms, Part 3 of 3 Topics In-Class Project: Tip Calculator
Functions, Part 3 of 4 Topics: Coding Practice Reading: None
Presentation transcript:

Solutions to In-Class Problems In-Class Project: The Box In-Class Project: Drawing a Rectangle 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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