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

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 4: Making Decisions.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
CS 108 Computing Fundamentals Notes for Thursday, February 12, 2015.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
CS 108 Computing Fundamentals Notes for Thursday September 10, 2015.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Flow of Control Part 1: Selection
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
CS 108 Computing Fundamentals Notes for Tuesday, February 10, 2015.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
CPS120: Introduction to Computer Science Decision Making in Programs.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CS 108 Computing Fundamentals Notes for Thursday, September 17, 2015.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
CS 108 Computing Fundamentals Notes for Thursday, February 18, 2016.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CS 108 Computing Fundamentals Notes for Tuesday, March 22, 2016.
A First Book of C++ Chapter 4 Selection.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
CNG 140 C Programming (Lecture set 3)
Decisions Chapter 4.
Chapter 4 (Conditional Statements)
EGR 2261 Unit 4 Control Structures I: Selection
CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017
Variables A piece of memory set aside to store data
CS 108 Computing Fundamentals Notes for Tuesday, September 19, 2017
Control Statement Examples
Lecture 2: Logical Problems with Choices
Chapter 7 Conditional Statements
Switch Case Structures
Control Structures.
Presentation transcript:

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

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 is not 0 therefore this condition is ALWAYS evaluated as TRUE (never FALSE)

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

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

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

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

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

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

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

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

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

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?

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

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

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

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

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

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

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

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

switch … case Statement.

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

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

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

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: bbrown/onlinet.htm bbrown/onlinet.htm

Exam #1 (2) If you need to take the exam in the Learning Center –Send me an 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)

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

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 use of the template and the addition of the algorithm to the template.

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

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

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

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 :