Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough...
-
Upload
william-manning -
Category
Documents
-
view
213 -
download
1
Transcript of Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough...
![Page 1: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/1.jpg)
Lecture 2: Logical Problems with Choices
![Page 2: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/2.jpg)
Problem Solving
Before writing a programHave a thorough understanding of the problemCarefully plan an approach for solving it
While writing a programKnow what “building blocks” are availableUsing good programming principles
![Page 3: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/3.jpg)
Algorithms
Computing problemsAll can be solved by executing a series of actions in a specific order
Algorithms: An algorithm is a clearly specified set of simple instructions to be followed to solve a problem
Actions to be executedThe order in which these actions are to be executed
Program controlSpecify order in which statements are to be executed
![Page 4: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/4.jpg)
Control Structures
Sequential executionNormally, statements are executed one after the other in the order written
Transfer of controlWhen the next statement executed is not the next one in sequenceOveruse of goto statements led to many problems
All C programs written in term of 3 control structuresSequence structures
Programs executed sequentially by defaultSelection structures
Three types: if, if … else, and switchRepetition structures
Three types: while, do … while and for
![Page 5: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/5.jpg)
Pseudocode
Artificial, informal language that helps develop algorithmsSimilar to everyday EnglishNot actually executed on computersHelp “think out” a program before writing it
Easy to convert into a corresponding C programConsists only executable statement
Definitions are not executable statements
![Page 6: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/6.jpg)
Flowchart
Graphical representation of an algorithmDrawn using certain special-purpose symbols connected by arrows called flowlinesSpecial-purpose symbols
Rectangle (action) symbol:any type of action.Oval symbol:the beginning or end of a programSmall circle (connector) symbol:the entry or exit of a portion of an algorithm.Diamond symbol:indicate that a decision is to be made.
Single-entry/single-exit
Flowcharting C’s sequence stucture
![Page 7: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/7.jpg)
if Selection Statement
Selection structureUsed to choose among alternative courses of actionExample - Pseudocode:if student’s grade is no less than 60
print “Passed”If condition true
Print statement executed and program goes on to next statement.
If condition falsePrint statement is ignored and the program goes onto the next statement
condition
![Page 8: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/8.jpg)
if Selection Statement
Pseudocode statement in Cif ( grade >= 60) printf( “Passed\n” );
C code corresponds closely to the pseudocodeFlow chart for the if selection statement
Diamond symbol (decision symbol)Indicates decision is to be madeContains an expression that can be true or false
Test the condition, follow appropriate path
condition
![Page 9: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/9.jpg)
if … else Selection Statement
if … elseSpecifies an action to be performed both when the condition is true and when it is falseif: Only performs an action if the condition is true.
Problem:
Pseudocode:
Read in 2 numbers and print in non-decreasing order.
Read in two numbers, num1 and num2.If num1 is no larger than num2 Print “num1 num2”else Print “num2 num1”
![Page 10: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/10.jpg)
if … else Selection Statement
Flowchart
begin
Read in two numbers: num1 and num2
num1 <= num2 Print “num1 num2”truefalse
end
Print “num2 num1”
Flowcharting the double-selection if...else statement
![Page 11: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/11.jpg)
if … else Selection Statement
C code
![Page 12: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/12.jpg)
Nested if … else Statements
Test for multiple cases by placing if…else selection statements inside if…else selection statementOnce condition is met, rest of statements skippedExample - Pseudocode
If student’s grade is greater than or equal to 90 Print “A”else If student’s grade is greater than or equal to 80 Print “B” else If student’s grade is greater than or equal to 70 Print “C” else If student’s grade is greater than or equal to 60 Print “D” else Print “F”
![Page 13: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/13.jpg)
Nested if … else Statements
Flowchart
Print “A” truescore>=90
false
score>=80Print “B” true false
score>=70Print “C” true false
score>=60Print “D” truePrint “F”
false
![Page 14: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/14.jpg)
Nested if … else StatementsC code
/* Convert a student's score to grade */
#include <stdio.h>
/* function main begins program execution */int main( void ){ int score; / * score of the student */
printf( "Enter the score\n" ); /* prompt */ scanf( "%d", &score ); /* read an integer */
if ( score >= 90 ) printf( "The grade is 'A'.\n" ); else if ( score >= 80 )
printf( "The grade is 'B'.\n" ); else
if ( score >= 70 ) printf( "The grade is 'C'.\n" ); else if ( score >= 60 ) printf( "The grade is 'D'.\n" ); else printf( "The grade is 'F'.\n" );
return 0; /* indicate that program ended successfully */
} /* end function main */
![Page 15: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/15.jpg)
Compound Statement
Set of statements within a pair of bracesExample:
if ( grade >= 60 ) printf( "Passed.\n" );else { printf( "Failed.\n" ); printf( "You must take this course again.\
n" ); }
Without the braces, the statement printf( "You must take this course again.\
n" );would be executed automatically.
A compound statement can be placed anywhere in a program that a single statement can be placed.
![Page 16: Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649efd5503460f94c114cb/html5/thumbnails/16.jpg)
Write a program that completes the following:Read in three integers and determine the largest.
You should only use if/else statements for the logic in your code.
Submit your maxnum.c file to the dropbox called Maximum Number and complete Program Quiz 2.
In-Class Programming Exercise