CS 108 Computing Fundamentals Notes for Tuesday, September 19, 2017
Review Quiz
Review GHP #8
Exam #1 (1) Tuesday, September 26 Chapters 2, 3, and UNIX tutorial only for closed-book part… up through and including programmer-created functions (PCFs) and GHP #8 for open-book part Use "Quick Check Exercises" and "Review Questions" in the textbook plus MPL exercises
Exam #1 (2) If you need to take the exam in the Learning Center Send me an e-mail reminding me Visit the Learning Center ASAP to make sure they are ready for you On Thursday I will collect official paperwork for accommodations if you have not already provided it Homework: do last semester’s Exam #1 I will e-mail is to you Do not make the mistake of thinking that your Exam #1 will be a copy of last semester's Exam #1 I'm showing you last semester's exam to give you a flavor of what could be on the exam and to give you an idea of what an exam looks like and what to expect (generally)
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 (like GHP #8) 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
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 **********************************/
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 beginning of the source-code file with the algorithm at the top each algorithm step inside the main ( ) http://web.cs.sunyit.edu/~urbanc/cs_108_sep_19_1.txt
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).
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
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
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 ) ;
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
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_sep_19_2.txt
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
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_sep_19_3.txt return ( llength ); }
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_sep_19_4.txt
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
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_sep_19_5.txt
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_sep_19_6.txt Complete example at this link : http://web.cs.sunyit.edu/~urbanc/cs_108_sep_19_7.txt