1 / 16

CS 108 Computing Fundamentals Notes for Tuesday, February 11, 2014

CS 108 Computing Fundamentals Notes for Tuesday, February 11, 2014. 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

donal
Download Presentation

CS 108 Computing Fundamentals Notes for Tuesday, February 11, 2014

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 108 Computing Fundamentals Notes for Tuesday, February 11, 2014

  2. 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

  3. 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 *****************/

  4. 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_11_1.txt

  5. 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).

  6. 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

  7. 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

  8. 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 ) ;

  9. 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

  10. 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_11_2.txt

  11. 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

  12. 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_11_3.txt return ( llength ); }

  13. 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_11_4.txt

  14. 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

  15. 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_11_5.txt

  16. Let's Go Through an Entire Example (14) • PCF #5 is very similar to PCF #4… PCF #6 displays the answer • Complete example at this link : http://web.cs.sunyit.edu/~urbanc/cs_108_sep_17_7.txt

More Related