cpts121-2-3

# cpts121-2-3 - CptS 121 Fall 09 Lecture 2-3 HK Chapter 3:...

This preview shows pages 1–7. Sign up to view the full content.

1 CptS 121 L2-3 – 9/4/09 Prof. Chris Hundhausen CptS 121 Fall ‘09 Lecture 2-3 HK Chapter 3: Functions Lecture Outline I. Another Problem-Solving Example II. Top-Down Design III. Functions IV. C Math Functions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 CptS 121 L2-3 – 9/4/09 Prof. Chris Hundhausen Problem Solving Example Problem Statement: Write a program that computes the areas of three rooms in a house, given the dimensions of the rooms. Assume that all rooms are square. Inputs: Width1 Width2 Width3 Outputs Area of room1 Area of room2 Area of room3 Relevant formula: area = width * width
3 CptS 121 L2-3 – 9/4/09 Prof. Chris Hundhausen Step 2: Design Algorithm Initial algorithm Get the widths of the three rooms Compute the areas of the three rooms Display the results Refined algorithm Get the widths of the three rooms Compute the areas of the three rooms - area = width * width Display the results

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 CptS 121 L2-3 – 9/4/09 Prof. Chris Hundhausen Step 3: Implement Solution /* * Computes the areas of three rooms */ #include <stdio.h> /* printf, scanf defs */ int main(void) { double width1, width2, width3, area1, area2, area3; /* Get the widths */ printf("Enter width of room1, in feet: "); scanf("%lf", &width1); printf("Enter width of room2, in feet: "); scanf("%lf", &width2); printf("Enter width of room3, in feet: "); scanf("%lf", &width3); /* Compute areas */ area1 = width1 * width1; area2 = width2 * width2; area3 = width3 * width3; /* Display results */ printf(“Room 1 is %.2lf square feet.”,area1); printf(“Room 2 is %.2lf square feet.”,area2); printf(“Room 3 is %.2lf square feet.”,area3); return 0; }
5 CptS 121 L2-3 – 9/4/09 Prof. Chris Hundhausen Notes on Example It’s redundant! We’re using the exact same sequence of commands (printf, scanf) to obtain the three widths and lengths We’re using the exam same formula to compute the three areas We’re using the same command (printf) to display the three areas Is there a better (less redundant, easier to read, more concise) way to approach this problem?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 CptS 121 L2-3 – 9/4/09 Prof. Chris Hundhausen Top-Down Design Allows us to manage the complexity of a
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/25/2011 for the course CPTS 121 taught by Professor Hundhaus during the Spring '09 term at Washington State University .

### Page1 / 21

cpts121-2-3 - CptS 121 Fall 09 Lecture 2-3 HK Chapter 3:...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online