cs 108 computing fundamentals notes for tuesday, september 22, 2015

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

Upload: myron-webb

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

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

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

Page 2: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 3: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Relational OperatorsExample :

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)

Page 4: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 5: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

A Possible Algorithm, Not "The" Algorithm

Page 6: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Relational Operator Demo (2)#include <stdio.h> // 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;}

Page 7: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Relational Operator Demo (3)

#include <stdio.h> // My file 3a.c… condition evaluation issuesint 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;}

Page 8: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Relational Operator Demo (4)#include <stdio.h> // 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;}

Page 9: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Relational Operator Demo (5)#include <stdio.h> // 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;}

Page 10: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Logical Operators

Operator Meaning

! NOT (Highest Priority)&& AND|| OR (Lowest Priority)

Condition A ! (NOT) Condition A Condition B &&(AND) ||(OR) true false true true true true false true true false false true

false true false true false false false false

Page 11: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 12: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Compound Logical Expressions

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

}

Page 13: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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?

Page 14: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

1. Prompt user to enter a test score2. Record user's input number for test score3. Test the test score to see if it is less than 65

i. If test is truea. Display a message indicating the grade earned is Fb. Go to step 8

4. Test the test score to see if it is greater than or equal to 65 but less than 70i. If test is true

a. Display a message indicating the grade earned is Db. Go to step 8

5. Test the test score to see if it is greater than or equal to 70 but less than 80i. If test is true

a. Display a message indicating the grade earned is Cb. Go to step 8

a. Test the test score to see if it is greater than or equal to 80 but less than 90a. If test is true

1. Display a message indicating the grade earned is B2. Go to step 8

1. Display a message indicating the grade earned is A2. Terminate

Now Lets Code It!!

A Possible Algorithm, Not "The" Algorithm

Page 15: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Compound Logical Expression Demo (2)#include <stdio.h> //My file 4.cint 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;}

Page 16: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 17: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Let’s Use a Programmer-Created Function

#include <stdio.h> // 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 */

Page 18: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

/* 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 );}

Page 19: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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' );}

Page 20: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 21: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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;

}

Page 22: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

switch … case Statement.

expression

Action Block 2

Label 1

Action Block 1

Label 2

Action Block n

Label n

Action Default

default

Page 23: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Let’s See a switch Statement Example

#include <stdio.h> /* 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 ); }

Page 24: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 25: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 26: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 27: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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 #1I'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)

Page 28: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 29: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 30: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Let's Go Through an Entire Example (3)/**********************************Algorithm

1. Greet user ………………………………………….…..…. PCF #12. Ask for length …………….……………………….….….. PCF #23. Read/record the length ……………………………….….. PCF #24. Ask for width .……………….………………………..….. PCF #3 5. Read/record the width ………………….…………….…... PCF #36. Calculate area (area = length * width ).………………….… PCF #47. Calculate perimeter (perimeter= 2 * length + 2* width.)…. PCF #58. Display answer ………………………………………….…. PCF #69. 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

Page 31: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Let's Go Through an Entire Example (4)

/**********************************Algorithm

1. Greet user ………………………………………….….…. PCF #12. Ask for length …………….……………………….…….. PCF #23. Read/record the length ………………………………….. PCF #24. Ask for width .……………….……………………….….. PCF #3 5. Read/record the width ………………….…………….….. PCF #36. Calculate area (area = length * width ).………………….… PCF #47. Calculate perimeter (perimeter= 2 * length + 2* width)…. PCF #58. Display answer ……………………………………………. PCF #69. 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).

Page 32: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

Let's Go Through an Entire Example (5)/**********************************Algorithm

1. Greet user ……………………………………………. .…PCF #12. Ask for length ……………….…………………………... PCF #23. Read/record the length ………………………………….. PCF #24. Ask for width .……………….…………………………... PCF #3 5. Read/record the width ………………….………………... PCF #36. Calculate are (area = length * width ).………………….… PCF #47. Calculate perimeter (perimeter= 2 * length + 2* width)…..PCF #58. Display answer ……………………………………………. PCF #69. Terminate ………………………………………………….. main( )*****************/

void intro_msg (void) ; // PCF #1 Prototypefloat get_length (void) ; // PCF #2 Prototypefloat get_width (void) ; // PCF #3 Prototypefloat calc_area (float , float) ; // PCF #4 Prototypefloat calc_perimeter (float , float) ; // PCF # 5 Prototypevoid display_answer (float , float) ; // PCF # 6 Prototype

Page 33: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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 Prototypefloat get_length (void) ; // PCF #2 Prototypefloat get_width (void) ; // PCF #3 Prototypefloat calc_area (float , float) ; // PCF #4 Prototypefloat calc_perimeter (float, float) ; // PCF # 5 Prototypevoid display_answer (float , float) ; // PCF # 6 Prototype

Page 34: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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 Prototypefloat get_length (void) ; // PCF #2 Prototypefloat get_width (void) ; // PCF #3 Prototypefloat calc_area (float , float) ; // PCF #4 Prototypefloat calc_perimeter (float, float) ; // PCF # 5 Prototypevoid 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 ) ;

Page 35: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 36: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 37: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 38: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

return ( llength ); }

Page 39: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 40: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 41: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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

Page 42: CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015

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