Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015.

Similar presentations


Presentation on theme: "CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015."— Presentation transcript:

1 CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

2 Relational Operators Operator Meaning = =Equal to ! =Not Equal to <Less Then < =Less Then or Equal to >Greater Then > =Greater Then or Equal to Note difference between = and ==

3 Relational Operators Example : if ( choice = 2) { answer = perimeter_cal (base, height); printf(" The perimeter is %f ", answer ); } Note the condition above is incorrect… compare: if ( choice = = 2) { answer = perimeter_cal (base, height); printf(" The perimeter is %f ", answer ); } The variable choice is assigned the value of 2... 2 is not 0 therefore this condition is ALWAYS evaluated as TRUE (never FALSE)

4 Relational Operator Demo (1) Let’s develop a short program that prompts the user to enter two numbers, and once the numbers are entered the program determines (tests) which of the two numbers is larger. After the determination is made (as to which is larger), a message is printed to the screen indicating the result of the test. Start with an algorithm!!

5 1.Prompt user to provide a number (number_1) 2.Record the user's input for number_1 3.Prompt user to provide another number (number_2) 4.Record user's input for number_2 5.Test to see if the the numbers are unequal a.If test is true i.Test to see if number_1 is greater than number_2 1)If test is true a) Inform the user that number_1 is greater than number_2 b)Go to step 6. 2) If test is false (else) a)Inform the user that number_2 is greater than number _1 b)Go to step 6. b.If test is false (else) i.Inform the user that number_1 and number_2 are equal ii.Go to step 6. 6.Terminate Now Lets Code It!! A Possible Algorithm, Not "The" Algorithm

6 Relational Operator Demo (2) #include // My file 3.c… also demonstrates "nested" int main (void) // if statements just to do it { float num1, num2; printf("\n Enter a number: "); scanf("%f", &num1); printf("\n Enter a second number: "); scanf("%f", &num2); if (num1 != num2) { if (num1 > num2) printf("\n The first number s greater than the second.\n\n\n"); else printf("\n The second number is greater than the first.\n\n\n"); } else printf("\n The numbers you entered are equal.\n\n\n"); return 0; }

7 Relational Operator Demo (3) #include // My file 3a.c… condition evaluation issues int main (void) { float num1, num2; printf("\n Enter a number: "); scanf("%f", &num1); printf("\n Enter a second number: "); scanf("%f", &num2); if ( 0 ) // After running add a ! before ( 0 ) and 0 { // and rerun if (num1 > num2) printf("\n The first number s greater than the second.\n\n\n"); else printf("\n The second number is greater than the first.\n\n\n"); } else printf("\n The numbers you entered are equal.\n\n\n"); return 0; }

8 Relational Operator Demo (4) #include // My file 3b.c… demonstration of // condition evaluation resultant values int main (void) { float answer; answer = 1 < 4 && 4 < 7 ; printf("\n\n Answer: %f ", answer ) ; answer = ! (1 < 3) || ( 2 < 4 ) ; printf("\n\n Answer: %f ", answer ) ; answer = ! ( (1 < 3) || ( 2 < 4 ) ) ; printf("\n\n Answer: %f ", answer ) ; answer = ! (372) ; printf("\n\n Answer: %f \n\n\n", answer ) ; return 0; }

9 Relational Operator Demo (5) #include // My file 3c.c… demonstration of // condition evaluation resultant values int main (void) // using data type int { // The result of all relational and Boolean int answer; // logical operations is always an int answer = 1 < 4 && 4 < 7 ; printf("\n\n Answer: %d ", answer ) ; answer = ! (1 < 3) || ( 2 < 4 ) ; printf("\n\n Answer: %d ", answer ) ; answer = ! ( (1 < 3) || ( 2 < 4 ) ) ; printf("\n\n Answer: %d ", answer ) ; answer = ! (372) ; printf("\n\n Answer: %d \n\n\n", answer ) ; return 0; }

10 Logical Operators OperatorMeaning !NOT (Highest Priority) &&AND ||OR (Lowest Priority) Condition A ! (NOT) Condition A Condition B &&(AND) ||(OR) true false truetrue truetrue false true truefalse falsetrue falsetrue falsetrue falsefalse falsefalse

11 Compound Logical Expressions Write an if statement that doubles a number if the number is greater than 0 yet does not exceed 25, triples the number if it’s greater than 25 but less than 50, and assigns the value 0 if the previous conditions aren’t met. Hint: use the if … else if … else construction

12 Compound Logical Expressions if ( (entry > 0) && (entry <= 25) ) { entry = entry * 2; } else if ( (entry > 25) && (entry < 50) ) { entry = entry * 3; { else { entry = 0; }

13 Compound Logical Expression Demo (1) Let’s develop a program that transforms a numeric grades entered by the user into a letter grade and then displays that letter grade to the screen. What do we do first?

14 1.Prompt user to enter a test score 2.Record user's input number for test score 3.Test the test score to see if it is less than 65 i.If test is true a.Display a message indicating the grade earned is F b.Go to step 8 4.Test the test score to see if it is greater than or equal to 65 but less than 70 i.If test is true a.Display a message indicating the grade earned is D b.Go to step 8 5.Test the test score to see if it is greater than or equal to 70 but less than 80 i.If test is true a.Display a message indicating the grade earned is C b.Go to step 8 6.Test the test score to see if it is greater than or equal to 80 but less than 90 i.If test is true a.Display a message indicating the grade earned is B b.Go to step 8 7.Display a message indicating the grade earned is A 8.Terminate Now Lets Code It!! A Possible Algorithm, Not "The" Algorithm

15 Compound Logical Expression Demo (2) #include //My file 4.c int main (void) { float score; printf("\n Enter your test score :"); scanf("%f",&score); if (score < 65) { printf("\n You earned an F.\n\n"); // Braces } else if ( (score >= 65) && (score < 70) ) // No Braces… why printf("\n You earned a D.\n\n"); else if ( (score >= 70) && (score < 80) ) printf("\n You earned a C.\n\n"); else if ( (score >= 80) && (score < 90) ) printf("\n You earned a B.\n\n"); else printf("\n You earned an A.\n\n"); return 0; }

16 Compound Logical Expression Demo (3) Move the code below, found in the previous program/slide, to a PCF named calc_grade( ) … the letter grade itself shall be printed in the main function, not in calc_grade( ) if (score < 65) else if ( (score >= 65) && (score < 70) ) else if ( (score >= 70) && (score < 80) ) else if ( (score >= 80) && (score < 90) ) else

17 Let’s Use a Programmer-Created Function #include // My file 5.c char calc_grade (float); // Function prototype int main(void) { float score; char grade; printf("\n Enter your test score: ") ; scanf("%f",&score); grade = calc_grade (score) ; //Function call printf("\n\nYou received a letter grade of %c. \n\n", grade); return ( 0 ); } /* Continued on next slide… read as one contiguous file */

18 /* Continued from previous slide… read as one contiguous file */ char calc_grade (float in_score) // Function declaration { char out_grade = ' '; // Local variable initialized to if (in_score <65) // a "space" (see ASCII table) out_grade = 'F' ; else if ((in_score >= 65) && (in_score < 70)) out_grade = 'D' ; else if ((in_score >= 70) && (in_score < 80)) out_grade = 'C' ; else if ((in_score >= 80) && (in_score < 90)) out_grade = 'B' ; else out_grade = 'A' ; return ( out_grade ); }

19 Let’s Use a Programmer-Created Function /* The action of the previous slide accomplished differently */ if (in_score <65) // Notice multiple return statements return ( 'F' ); else if ((in_score >= 65) && (in_score < 70)) return ( 'D' ); else if ((in_score >= 70) && (in_score < 80)) return ( 'C' ); else if ((in_score >= 80) && (in_score < 90)) return ( 'B' ); else //Only one return statement can execute return ( 'A' ); }

20 switch … case Statement. Simple if has only one action part, and if … else statement has only two action parts... else if can be messy. Often in life three or more cases of actions can be used, based on the condition. Use either nested else if statements or use a switch … case statement

21 switch … case Statement. switch (decision criteria or condition) { case Label_1 : Action Block 1; // decision criteria must be break; // data type int/char only case Label2 : // Labels must be integer Action Block 2; // constants only… if you break; // remember the ASCII … // table then you'll know that default : // catch-all // characters are included as Action Block else; // integer constants break; }

22 switch … case Statement.

23 Let’s See a switch Statement Example #include /* Let’s calculate a worker’s pay… 1.c*/ int main( void ) { int status = 0; double hours = 0.0; double pay = 0.0; printf("\n Enter the hours worked : "); scanf("%lf",&hours); printf("\n Enter the status (7 for full-timers and 9 for part-timers): "); scanf("%d",&status); switch (status) { case 7: pay = hours * 15.0; // full-time rate is $15 per hour break; case 9: pay = hours * 10.0; // part-time rate is $10 per hour break; default: printf("\n\n\n >>>Wrong Employment Status<<<\n"); break; } printf("\n The payment amount is: %4.2lf\n\n\n", pay); return ( 0 ); }

24 Remember: Steps to Build a Program What does it need to do (functionality): algorithm Can it be broken down into simpler pieces Code the simpler pieces – Code and test one piece at a time Integrate, test and fix Finished Start with a complete algorithm before writing one line of code Comment as you go "Urban Inside-Out Method"…one step at a time

25 Pico Shortcuts ctrl-w followed by ctrl-t produces a prompt that asks you on which line you like the cursor to be positioned ctrl-c produces the number of the line on which the cursor is currently positioned ctrl-y moves the cursor up one page ctrl-v moves the cursor down one page

26 Exam #1 (1) Tuesday, September 29 Chapters 2, 3, video tutorials, Web page tutorials listed in Syllabus, and UNIX tutorial only for closed-book part… up through and including programmer-created functions (PCFs) and GHP #6 for open-book part Use "Quick Check Exercises" and "Review Questions" Use Online Interactive Tests at the following link to prepare: http://ftp.tuwien.ac.at/languages/c/programming- bbrown/onlinet.htm http://ftp.tuwien.ac.at/languages/c/programming- bbrown/onlinet.htm

27 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 Homework: do last semester’s Exam #1 –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)

28 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

29 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 *****************/

30 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_sep_22_1.txt

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

32 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

33 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

34 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 ) ;

35 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

36 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_22_2.txt

37 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

38 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_22_3.txthttp://web.cs.sunyit.edu/~urbanc/cs_108_sep_22_3.txt return ( llength ); }

39 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_22_4.txt

40 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

41 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_22_5.txt

42 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_22_6.txt Complete example at this link : http://web.cs.sunyit.edu/~urbanc/cs_108_sep_22_7.txt


Download ppt "CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015."

Similar presentations


Ads by Google