Problem Solving - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Problem Solving

Description:

Exercise: Leap Year ... Leap year: 366 days (with Feb 29) ... 1900 is NOT a leap year. The number of days in 1900 is 365. Test with the following input ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 38
Provided by: fuhb
Category:
Tags: problem | solving

less

Transcript and Presenter's Notes

Title: Problem Solving


1
  • Programming Review

Lecture 2
  • Problem Solving
  • and
  • Program Design

2
Remember Your First Program?
Preprocessor statements
// a simple program include ltiostreamgt using
namespace std int main() cout ltlt "Hello
world!" ltlt endl return 0
Comments
Function named main() indicates start of program
Print statement
Ends execution of main() which ends program
Function
3
C Software Development
  • Major activities
  • Editing (to write the program)
  • Compiling (creates .obj file)
  • Linking with compiled files (creates .exe file)
  • Object files
  • Library modules
  • Loading and executing
  • Testing the program

4
Problem Solving
  • Define the problem.
  • Develop a solution.
  • Outline solution first.
  • Then write down the solution steps in detail.
  • Test the solution and revise if necessary.
  • Document and maintain the solution.

5
Example 1
  • Define Problem
  • Find the best way to travel from HKUST to Central
    quickly and cheaply.

6
Example 1
  • Develop Solution
  • Evaluate all possible routes and modes of
    transportation
  • Select the route that meets our goal (fastest and
    cheapest)

7
Example 1
  • Testing
  • Test the route and make sure conditions have not
    changed (e.g., increased bus fares, road
    construction).
  • Documentation
  • Give description of solution and explain it.
  • Maintenance
  • Test and revise the route as needed.

8
Programming as Problem Solving
  • Define the problem.
  • What is the input output?
  • What constraints must be satisfied?
  • What information is essential?
  • Develop a solution
  • Construct an algorithm (steps that must be done)
  • Implement a program.
  • Compile, test, and debug the program.
  • Document and maintain the program.

9
What Makes a Good Program?
  • Correctness
  • Meets the problem requirements
  • Produces correct results
  • Easy to read and understand
  • Easy to modify
  • Easy to debug
  • Efficient
  • Fast
  • Requires less memory

10
  • Programming Review
  • Branching

11
Three Program Structures
  • Sequence - executable statements which the
    computer processes in the given order
  • Choice/Branching - sequence(s) selected depending
    on some condition
  • if ltcondition existsgt
  • ltdo Pgt
  • Iteration - repetitively executed sequences
  • while ltcondition existsgt
  • ltdo Pgt

12
Sequence
  • It is natural to write a program as a sequence of
    program structures such as sequences, choices,
    and iterations
  • Program
  • Structure 1
  • Program
  • Structure 2
  • Program
  • Structure 3

13
Choice Constructs
  • Provide
  • Ability to control whether a statement list is
    executed
  • Two constructs
  • if statement
  • if
  • if-else
  • if-else-if
  • switch statement

14
The Basic if Statement
  • Syntax
  • if(Expression)
  • Action
  • Example absolute value
  • if(value lt 0)
  • value -value

Expression
true
false
Action
15
Absolute Value
  • // program to read number print its absolute
    value
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int value
  • cout ltlt "Enter integer "
  • cin gtgt value
  • if (value lt 0)
  • value -value
  • cout ltlt "The absolute value is " ltlt value ltlt
    endl
  • return 0

16
Choice (if)
  • Put multiple action statements within braces
  • if ltit's raininggt
  • lttake umbrellagt
  • ltwear raincoatgt

17
Sorting Two Numbers
  • int value1
  • int value2
  • int temp
  • cout ltlt "Enter two integers "
  • cin gtgt value1 gtgt value2
  • if (value1 gt value2)
  • temp value1
  • value1 value2
  • value2 temp
  • cout ltlt "The input in sorted order "
  • ltlt value1 ltlt " " ltlt value2 ltlt endl

18
Relational Operators
  • Relational operators are used to compare two
    values
  • Math C Plain English
  • equals example if(ab)
  • (ab) means put the value of b into a
  • lt lt less than
  • ? lt less than or equal to
  • gt gt greater than
  • ? gt greater than or equal to
  • ? ! not equal to

19
Relational Expressions
  • Examples
  • numberOfStudents lt 200
  • 20 j 10 i

20
Operator Precedence
  • Which comes first?
  • /
  • -
  • lt lt gt gt
  • !

Answer
21
The if-else Statement
  • Syntax of if-else
  • if ltit's sunnygt
  • ltwalk catgt
  • else
  • lttake cat with umbrellagt
  • Example
  • if (v 0)
  • cout ltlt "v is 0"
  • else
  • cout ltlt "v is not 0"

Expression
false
true
Action1
Action2
22
Finding the Bigger One
  • int value1
  • int value2
  • int larger
  • cout ltlt "Enter two integers "
  • cin gtgt value1 gtgt value2
  • if (value1 gt value2)
  • larger value1
  • else
  • larger value2
  • cout ltlt "Larger of inputs is " ltlt larger ltlt endl

23
Selection
  • Often we want to perform a particular action
    depending on the value of an expression
  • Two ways to do this
  • if-else-if statement
  • if-else statements glued together
  • switch statement
  • An advanced construct

24
if-else-if Statements
  • Top10
  • if ltMon, Wed, or Fri AMgt
  • ltgoto COMP103gt
  • else if ltTues, Thurs AMgt
  • ltgoto MATH/PHYSgt
  • else if lt1PM or 7PMgt
  • lteatgt
  • else
  • ltsleepgt

25
if-else-if Statement Grade
  • Example
  • if(score gt 90)
  • cout ltlt "Grade A" ltlt endl
  • else if(score gt 80)
  • cout ltlt "Grade B" ltlt endl
  • else if(score gt 70)
  • cout ltlt "Grade C" ltlt endl
  • else if(score gt 60)
  • cout ltlt "Grade D" ltlt endl
  • else
  • cout ltlt "Grade F" ltlt endl

26
switch Statement Grade
  • switch(int(score)/10)
  • case 10
  • case 9 cout ltlt "Grade A" ltlt endl
  • break
  • case 8 cout ltlt "Grade B" ltlt endl
  • break
  • case 7 cout ltlt "Grade C" ltlt endl
  • break
  • case 6 cout ltlt "Grade D" ltlt endl
  • break
  • default cout ltlt "Grade F" ltlt endl

27
Switch Calculator
  • int left
  • int right
  • char oper
  • cout ltlt "Enter simple expression "
  • cin gtgt left gtgt oper gtgt right
  • cout ltlt left ltlt " " ltlt oper ltlt " " ltlt right
  • ltlt " "
  • switch (oper)
  • case '' cout ltlt left right ltlt endl break
  • case '-' cout ltlt left - right ltlt endl break
  • case '' cout ltlt left right ltlt endl break
  • case '/' cout ltlt left / right ltlt endl break
  • default cout ltlt "Illegal operation" ltlt endl

28
A Boolean Type
  • C contains a type named bool which can have one
    of two values
  • true (corresponds to non-zero value)
  • false (corresponds to zero value)
  • Boolean operators can be used to form more
    complex conditional expressions
  • The and operator is
  • The or operator is
  • The not operator is !
  • Warning
  • and are also operators

29
A Boolean Type
  • Example logical expressions
  • bool P truebool Q falsebool R
    truebool S P Qbool T !Q Rbool U
    !(R !Q)

30
More Operator Precedence
  • Precedence of operators (from highest to lowest)
  • Parentheses ( )
  • Unary operators !
  • Multiplicative operators /
  • Additive operators -
  • Relational ordering lt lt gt gt
  • Relational equality !
  • Logical and
  • Logical or
  • Assignment

31
More Operator Precedence
  • Examples
  • 5 ! 6 7 lt 3
  • (5 !6) (7 lt 3)
  • 5 15 4 13 12 lt 19 !false 5 lt 24

32
Nested if Statements
  • Nested means that one complete statement is
    inside another
  • Example
  • if ltit is Mondaygt
  • if ltit is time for classgt
  • ltgo to COMP104gt
  • ltcall your friendsgt
  • Top10

33
Dangling Else Problem
  • Problem Nested if statements can seem ambiguous
    in their meaning.
  • What is the value of c after the following is
    executed?
  • int a-1, b1, c1
  • if(agt0)
  • if(bgt0)
  • c 2
  • else
  • c 3

34
Dangling Else Problem
  • C groups a dangling else with the most recent
    if.
  • The following indentation shows how C would
    group this example (answer c1).
  • int a-1, b1, c1
  • if(agt0)
  • if(bgt0)
  • c 2
  • else // dangling else grouped to nearest if
  • c 3

35
Dangling Else Problem
  • Use extra brackets to clarify the intended
    meaning, even if not necessary.
  • int a-1, b1, c1
  • if(agt0)
  • if(bgt0)
  • c 2
  • else // parenthesis avoid dangling else
  • c 3

36
Exercise Leap Year
  • Develop an algorithm that takes the year as
    input, and report the number of days for that
    year.
  • Leap year 366 days (with Feb 29)
  • A year is NOT leap year if it is not divisible by
    4. If the year is divisible by 100, it's not a
    leap year UNLESS it is also divisible by 400.
  • Write a C program according to your algorithm

37
  • Sample Program I/O
  • Leap Year Calculation
  • Enter the year 1900
  • 1900 is NOT a leap year.
  • The number of days in 1900 is 365.
  • Test with the following input
  • 1985, 1988, 1990, 2004, 2000
  • If you dont get every answer correct, check your
    algorithm and/or program
Write a Comment
User Comments (0)
About PowerShow.com