Download presentation
Presentation is loading. Please wait.
Published byMonica Wright Modified over 9 years ago
1
CS 108 Computing Fundamentals Notes for Tuesday, February 10, 2015
2
Let's Finish the Slide From Last Week Let's go to the slides from our last meeting
3
Let's Go Through an Entire Example (1) Let's write a program that calculates the area and the perimeter and of rectangle once the user provides a length and width input… let’s start by developing an algorithm Maximize the use of functions main( ) merely passes values and control to programmer-created functions (PCFs) We are going to violate the "Urban Inside-Out One Step at a Time Method" this time to view functions from a slightly different perspective
4
Let's Go Through an Entire Example (2) /********************************** Algorithm 1. Greet user 2. Ask for length 3. Read/record the length 4. Ask for width 5. Read/record the width 6. Calculate area (area = length * width ) 7. Calculate perimeter (perimeter = 2 * length + 2*width) 8. Display answer 9. Terminate *****************/
5
Let's Go Through an Entire Example (3) /********************************** Algorithm 1. Greet user ………………………………………….…..…. PCF #1 2. Ask for length …………….……………………….….….. PCF #2 3. Read/record the length ……………………………….….. PCF #2 4. Ask for width.……………….………………………..….. PCF #3 5. Read/record the width ………………….…………….…... PCF #3 6. Calculate area (area = length * width ).………………….… PCF #4 7. Calculate perimeter (perimeter= 2 * length + 2* width.)…. PCF #5 8. Display answer ………………………………………….…. PCF #6 9. Terminate ………………………………………………..…. main( ) *****************/ This link below shows the use of the template and the addition of the algorithm to the template. http://web.cs.sunyit.edu/~urbanc/cs_108_feb_10_1.txt
6
Let's Go Through an Entire Example (4) /********************************** Algorithm 1. Greet user ………………………………………….….…. PCF #1 2. Ask for length …………….……………………….…….. PCF #2 3. Read/record the length ………………………………….. PCF #2 4. Ask for width.……………….……………………….….. PCF #3 5. Read/record the width ………………….…………….….. PCF #3 6. Calculate area (area = length * width ).………………….… PCF #4 7. Calculate perimeter (perimeter= 2 * length + 2* width)…. PCF #5 8. Display answer ……………………………………………. PCF #6 9. Terminate …………………………………………………. main( ) *****************/ In-class exercise: Given the algorithm above with the additional guidance about PCFs, develop, on paper, the prototypes for each of the 6 PCFs (use identifiers of your choice).
7
Let's Go Through an Entire Example (5) /********************************** Algorithm 1. Greet user ……………………………………………..…PCF #1 2. Ask for length ……………….…………………………... PCF #2 3. Read/record the length ………………………………….. PCF #2 4. Ask for width.……………….…………………………... PCF #3 5. Read/record the width ………………….………………... PCF #3 6. Calculate are (area = length * width ).………………….… PCF #4 7. Calculate perimeter (perimeter= 2 * length + 2* width)…..PCF #5 8. Display answer ……………………………………………. PCF #6 9. Terminate ………………………………………………….. main( ) *****************/ void intro_msg (void) ; // PCF #1 Prototype float get_length (void) ; // PCF #2 Prototype float get_width (void) ; // PCF #3 Prototype float calc_area (float, float) ; // PCF #4 Prototype float calc_perimeter (float, float) ; // PCF # 5 Prototype void display_answer (float, float) ; // PCF # 6 Prototype
8
Let's Go Through an Entire Example (6) In-class exercise: Given the prototypes below, develop, on paper, the calls for each of the 6 PCF prototypes … answers on next slide. void intro_msg (void) ; // PCF #1 Prototype float get_length (void) ; // PCF #2 Prototype float get_width (void) ; // PCF #3 Prototype float calc_area (float, float) ; // PCF #4 Prototype float calc_perimeter (float, float) ; // PCF # 5 Prototype void display_answer (float, float) ; // PCF # 6 Prototype
9
Let's Go Through an Entire Example (7) In-class exercise: Given the prototypes below, develop, on paper, the calls for each of the 6 PCF prototypes … calls below in green. void intro_msg (void) ; // PCF #1 Prototype float get_length (void) ; // PCF #2 Prototype float get_width (void) ; // PCF #3 Prototype float calc_area (float, float) ; // PCF #4 Prototype float calc_perimeter (float, float) ; // PCF # 5 Prototype void display_answer (float, float) ; // PCF # 6 Prototype intro_msg( ) ; length = get_length( ) ; width = get_width ( ) ; area = calc_area( length, width) ; perimeter = calc_perimeter( length, width ) ; display_answer ( area, perimeter ) ;
10
Let's Go Through an Entire Example (8) In-class exercise: Given the prototype and call below, develop, on paper, the declaration necessary. void intro_msg (void); // PCF #1 Prototype intro_msg( ) ; // PCF #1 Call
11
Let's Go Through an Entire Example (9) In-class exercise: Given the prototype and call below, develop, on paper, the declaration necessary. void intro_msg (void); // PCF #1 Prototype intro_msg( ) ; // PCF #1 Call // PCF #1 Declaration, no formal parameters and no return value void intro_msg (void) { printf("\n\nThis program does very little.\n\n\n"); return ; } http://web.cs.sunyit.edu/~urbanc/cs_108_feb_10_2.txt
12
Let's Go Through an Entire Example (10) In-class exercise: Given the prototype and call below, develop, on paper, the declaration necessary. float get_length (void); // PCF #2 Prototype length = get_length( ) ; // PCF #2 Call
13
Let's Go Through an Entire Example (11) In-class exercise: Given the prototype and call below, develop, on paper, the declaration necessary. float get_length (void); // PCF #2 Prototype length = get_length( ) ; // PCF #2 Call // PCF #2 Declaration, no formal parameters, a single float value // returned to the calling environment float get_length (void) { float llength =0.0; // Local variable llength declared printf("\nEnter the length of the rectangle: "); scanf("%f", &llength ); // http://web.cs.sunyit.edu/~urbanc/cs_108_feb_10_3.txthttp://web.cs.sunyit.edu/~urbanc/cs_108_feb_10_3.txt return ( llength ); }
14
Let's Go Through an Entire Example (11b) In-class exercise: developing the get_width( ) declaration is very similar to developing the get_length( ) declaration http://web.cs.sunyit.edu/~urbanc/cs_108_feb_10_4.txt
15
Let's Go Through an Entire Example (12) In-class exercise: Given the prototype and call below, develop, on paper, the declaration necessary. float calc_area (float, float); // PCF #4 Prototype area = calc_area( length, width) ; // PCF #4 Call
16
Let's Go Through an Entire Example (13) In-class exercise: Given the prototype and call below, develop, on paper, the declaration necessary. float calc_area (float, float); // PCF #4 Prototype area = calc_area( length, width) ; // PCF #4 Call // PCF # 4 Declaration, two formal parameters // and a single float value returned float calc_area ( float ca_length, float ca_width ) { float ca_area = 0.0; // Local variable declared ca_area = ca_length * ca_width; return ( ca_area ); } http://web.cs.sunyit.edu/~urbanc/cs_108_feb_10_5.txt
17
Let's Go Through an Entire Example (14) PCF #5 is very similar to PCF #4… PCF #6 displays the answer http://web.cs.sunyit.edu/~urbanc/cs_108_feb_10_6.txt Complete example at this link : http://web.cs.sunyit.edu/~urbanc/cs_108_feb_10_7.txt
18
Let's Go Over Fall 2014 Exam #1 (1-16)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.