CSCI6370: Topics in Computer Science Advanced Topics in Algorithms and Applications Fall Semester, 2002 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CSCI6370: Topics in Computer Science Advanced Topics in Algorithms and Applications Fall Semester, 2002

Description:

All can be solved by executing a series of actions in a specific order ... your maxnum.c file to the dropbox called Maximum Number and complete Program Quiz 2. ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 17
Provided by: XIAODO
Category:

less

Transcript and Presenter's Notes

Title: CSCI6370: Topics in Computer Science Advanced Topics in Algorithms and Applications Fall Semester, 2002


1
Lecture 2 Logical Problems with Choices
2
Problem Solving
  • Before writing a program
  • Have a thorough understanding of the problem
  • Carefully plan an approach for solving it
  • While writing a program
  • Know what building blocks are available
  • Using good programming principles

3
Algorithms
  • Computing problems
  • All 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 executed
  • The order in which these actions are to be
    executed
  • Program control
  • Specify order in which statements are to be
    executed

4
Control Structures
  • Sequential execution
  • Normally, statements are executed one after the
    other in the order written
  • Transfer of control
  • When the next statement executed is not the next
    one in sequence
  • Overuse of goto statements led to many problems
  • All C programs written in term of 3 control
    structures
  • Sequence structures
  • Programs executed sequentially by default
  • Selection structures
  • Three types if, if else, and switch
  • Repetition structures
  • Three types while, do while and for

5
Pseudocode
  • Artificial, informal language that helps develop
    algorithms
  • Similar to everyday English
  • Not actually executed on computers
  • Help think out a program before writing it
  • Easy to convert into a corresponding C program
  • Consists only executable statement
  • Definitions are not executable statements

6
Flowchart
  • Graphical representation of an algorithm
  • Drawn using certain special-purpose symbols
    connected by arrows called flowlines
  • Special-purpose symbols
  • Rectangle (action) symbol
  • any type of action.
  • Oval symbol
  • the beginning or end of a program
  • Small 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 Cs sequence stucture
7
if Selection Statement
  • Selection structure
  • Used to choose among alternative courses of
    action
  • Example - Pseudocode
  • if students grade is no less than 60
  • print Passed
  • If condition true
  • Print statement executed and program goes on to
    next statement.
  • If condition false
  • Print statement is ignored and the program goes
    onto the next statement

8
if Selection Statement
  • Pseudocode statement in C
  • if ( grade gt 60)
  • printf( Passed\n )
  • C code corresponds closely to the pseudocode
  • Flow chart for the if selection statement
  • Diamond symbol (decision symbol)
  • Indicates decision is to be made
  • Contains an expression that can be true or false
  • Test the condition, follow appropriate path

9
if else Selection Statement
  • if else
  • Specifies an action to be performed both when the
    condition is true and when it is false
  • if 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
10
if else Selection Statement
  • Flowchart

begin
Read in two numbers num1 and num2
true
false
Print num1 num2
Print num2 num1
end
11
if else Selection Statement
  • C code

12
Nested if else Statements
  • Test for multiple cases by placing ifelse
    selection statements inside ifelse selection
    statement
  • Once condition is met, rest of statements skipped
  • Example - Pseudocode

If students grade is greater than or equal to
90 Print A else If students grade is
greater than or equal to 80 Print B
else If students grade is greater than
or equal to 70 Print C
else If students grade is greater
than or equal to 60 Print
D else Print F
13
Nested if else Statements
  • Flowchart

Print A
Print B
Print C
false
Print D
Print F
14
Nested if else Statements
/ Convert a student's score to grade
/ include ltstdio.hgt / 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 gt 90 ) printf( "The grade
is 'A'.\n" ) else if ( score gt 80
) printf(
"The grade is 'B'.\n" )
else if ( score gt 70
) printf( "The grade is 'C'.\n"
) else if ( score
gt 60 ) printf( "The grade
is 'D'.\n" ) else
printf( "The grade is 'F'.\n"
) return 0 / indicate that
program ended successfully / / end
function main /
  • C code

15
Compound Statement
  • Set of statements within a pair of braces
  • Example
  • if ( grade gt 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.

16
In-Class Programming Exercise
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.
Write a Comment
User Comments (0)
About PowerShow.com