C Programming for Engineers Iteration ICEN 360 Spring 2017 Prof. - - PowerPoint PPT Presentation

c programming for engineers iteration
SMART_READER_LITE
LIVE PREVIEW

C Programming for Engineers Iteration ICEN 360 Spring 2017 Prof. - - PowerPoint PPT Presentation

C Programming for Engineers Iteration ICEN 360 Spring 2017 Prof. Dola Saha 1 Application: Summing even numbers 2 Application: Compound Interest Calculation Consider the following problem statement: A person invests $1000.00 in a


slide-1
SLIDE 1

1

C Programming for Engineers Iteration

ICEN 360– Spring 2017

  • Prof. Dola Saha
slide-2
SLIDE 2

2

Application: Summing even numbers

slide-3
SLIDE 3

3

Application: Compound Interest Calculation

Ø

Consider the following problem statement:

§ A person invests $1000.00 in a savings account yielding 5% interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts:

a = p(1 + r)n

where

p is the original amount invested (i.e., the principal) r is the annual interest rate n is the number of years a is the amount on deposit at the end of the nth year.

slide-4
SLIDE 4

4

C Code for Compound Interest Calculation

slide-5
SLIDE 5

5

Output

slide-6
SLIDE 6

6

Classwork Assignment

Ø Write a program that finds the smallest of several

  • integers. Assume that the first value read specifies the

number of values remaining. Your program should read

  • nly one value each time scanf is executed.

Ø A typical input sequence might be § 5 400 500 300 200 100 § where 5 indicates that the subsequent five values are to be used for finding minimum.

slide-7
SLIDE 7

7

Classwork Assignment

Ø Write a program that prints the following patterns

separately, one below the other. Use for loops to generate the patterns. [Hint: The last two patterns require that each line begin with an appropriate number

  • f blanks.]

(A) (B) (C) (D) * ********** ********** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* ********** * * **********

slide-8
SLIDE 8

8

do ... while Iteration Statement

Ø Similar to the while statement.

Ø

do statement while (condition); Ø The loop-continuation

condition after the loop body is performed.

Ø The loop body will be

executed at least once.

Ø

while (condition) Ø The loop-continuation

condition is tested at the beginning of the loop

slide-9
SLIDE 9

9

Example do ... while Iteration Statement

slide-10
SLIDE 10

10

Flowchart do ... while Iteration Statement

slide-11
SLIDE 11

11

break and continue Statements

Ø Break § Used inside while, for, do…while, switch Statements § When executed, program exits the statements Ø Continue § Used in while, for, do…while Statements § When executed, the loop-continuation test is evaluated immediately after the continue statement is executed. § In the for statement, the increment expression is executed, then the loop-continuation test is evaluated.

slide-12
SLIDE 12

12

break Statement

slide-13
SLIDE 13

13

continue Statement

slide-14
SLIDE 14

14

Revisiting switch Statement

Ø

If break is not used anywhere in a switch statement, then each time a match occurs in the statement, the statements for all the remaining cases will be executed—called fallthrough.

Ø

If no match occurs, the default case is executed, and an error message is printed.

slide-15
SLIDE 15

15

Code Snippet (1)

slide-16
SLIDE 16

16

Code Snippet (2)

slide-17
SLIDE 17

17

Code Snippet (3)

slide-18
SLIDE 18

18

Code Snippet (4) & Output

slide-19
SLIDE 19

19

Logical Operators

Ø Used to form more complex conditions by combining simple

conditions.

Ø The logical operators are && (logical AND), || (logical OR)

and ! (logical NOT also called logical negation)

Ø Logical AND – used to ensure that two conditions are both

true before we choose a certain path of execution

Ø Logical OR – used to ensure that at least one condition is

true before we choose a certain path of execution

Ø Logical NOT – used to “reverse” the meaning of a condition.

slide-20
SLIDE 20

20

Truth Table

Ø Table of Logic

slide-21
SLIDE 21

21

Operator Precedence

slide-22
SLIDE 22

22

Structured Program Summary (1)

slide-23
SLIDE 23

23

Structured Program Summary (2)

slide-24
SLIDE 24

24

Rules for forming structured programs

Ø Begin with the simplest flowchart Ø Stacking Rule – Any rectangle (action) can be replaced by

two rectangles (actions) in sequence

Ø Nesting Rule – Any rectangle (action) can be replaced by

any control statement

Ø Stacking & Nesting Rule rules may be applied in any

  • rder.
slide-25
SLIDE 25

25

Simplest Flowchart

slide-26
SLIDE 26

26

Stacking Rule

slide-27
SLIDE 27

27

Nesting Rule

slide-28
SLIDE 28

28

Structured Program Building Blocks

slide-29
SLIDE 29

29

Structured Programming

Ø Structured programming promotes simplicity. Ø Bohm and Jacopini showed that only three forms of

control are needed:

§ Sequence § Selection § Iteration

slide-30
SLIDE 30

30

Structured Programming Options

Ø Sequence is straightforward. Ø Selection is implemented in one of three ways: § if statement (single selection) § if…else statement (double selection) § switch statement (multiple selection) Ø Iteration is implemented in one of three ways: § while statement § do…while statement § for statement