Repeated Calculations - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Repeated Calculations

Description:

... having to deliberately set a value for the while condition ... system('PAUSE'); return 0; Lect0503.c. Gibbons | GNG1101-5: ... alternative method uses the ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 25
Provided by: davidg84
Category:

less

Transcript and Presenter's Notes

Title: Repeated Calculations


1
Repeated Calculations
Lect05
  • To obtain the areas of several circles, we would
    have to run our program several times.
  • There are many times when we will want to repeat
    calculations and C has looping structures built
    in
  • while
  • do while
  • for

2
while - Statement
  • while (condition) operations
  • The operations in the while block are performed
    as long as (while) the condition is true.
  • When the condition becomes false, the while loop
    completes and control moves to the instruction
    following the while loop.

3
while - An Example
  • float area, pi3.14159, radius1.0
  • while(radiusgt0.0)
  • printf("Input radius ")
  • scanf("f", radius)
  • areapiradiusradius
  • printf("Area is f\n", area)
  • printf("End of program\n")

Lect0501.c
4
Notes
  • The entry value of the while condition must be
    specified.
  • radius1.0 is the entry condition.
  • There must be a termination condition for the
    while loop.
  • providing a value of radiuslt0.0 will stop the
    loop.

5
Repetition - while
TRUE
FALSE
6
Remember the if
TRUE
FALSE
7
Repetition - do while
TRUE
FALSE
8
do while - Statement
  • To prevent having to deliberately set a value for
    the while condition on entry to the loop, the do
    while structure has been provided.
  • In the do while, the condition is tested at the
    end of the block rather than at the beginning.

9
do while - Example
  • float area, pi3.14159, radius
  • do
  • printf("Input radius ")
  • scanf("f", radius)
  • areapiradiusradius
  • printf("Area is f\n", area)
  • while(radiusgt0.0)
  • printf("End of program\n")

Lect0502.c
10
while OR do while?
  • while is used when the program naturally sets the
    while condition before entry to the loop.
  • do while is used when the while condition is
    undefined outside of the loop.

11
Counting
  • Some applications require a loop which performs
    in a sequential manner or counting.
  • Consider the sum of the first ten integers
  • 123...10

12
Sum of First Ten Integers
  • int main()
  • int i1, sum0
  • while(ilt11)
  • sumsumi
  • i //equivalent to ii1
  • printf("Sum is d\n", sum)
  • system("PAUSE")
  • return 0

Lect0503.c
13
Notes i
  • Now we see that the next language after C i.e.
    C1 is called C.

14
Notes sumsumi
  • sumsumi
  • In mathematics this would mean that
    i0 (sum-sumi)
  • All should now be clear about "" being is
    assigned the value of
  • The variable sum is assigned the value of (the
    old value of the variable sum plus the value of
    the variable i)

15
for - Statement
  • An alternative method uses the for loop
  • for (start setting looping condition step)
    operations
  • Note the separation of the three terms in the for
    header by

16
Repetition - for
Initial setting
TRUE
Looping condition
Step
Body
FALSE
17
Sum of First Ten Integers - 2
  • include ltstdio.hgt
  • int main()
  • int i, sum0
  • for(i1 ilt11 i) sumsumi
  • printf("Sum is d\n", sum)
  • system("PAUSE")
  • return 0

Lect0504.c
18
Sum of Numbers
i1
Initial setting
TRUE
sumsumi
i
ilt11
Looping condition
Step
Body
FALSE
19
Compare while and for
  • int i1, sum0
  • while(ilt11)
  • sumsumi
  • i
  • for(i1 ilt11 i) sumsumi

20
Nested Loops
  • int i, n, sum
  • do
  • printf("Sum of how many numbers? ")
  • scanf("d", n)
  • sum0
  • for(i1 iltn i) sumsumi
  • printf("Sum is d\n", sum)
  • while(ngt0)

Lect0505.c
21
Nested Loops - 2
  • int i, n, sum
  • for(n1 nlt11 n)
  • sum0
  • for(i1 iltn i) sumsumi
  • printf("Sum of first d numbers is d\n", n,
    sum)

Lect0506.c
22
Console Output
  • Sum of first 1 numbers is 1
  • Sum of first 2 numbers is 3
  • Sum of first 3 numbers is 6
  • Sum of first 4 numbers is 10
  • Sum of first 5 numbers is 15
  • Sum of first 6 numbers is 21
  • Sum of first 7 numbers is 28
  • Sum of first 8 numbers is 36
  • Sum of first 9 numbers is 45
  • Sum of first 10 numbers is 55

23
3D Surface
  • float x, y, z
  • for(x-20.0 xlt25.0 xx2.0)
  • for(y-12.0 ylt13.0 yy1.0)
  • zxx(y10)(y10)
  • printf("f,f,f\n", x, y, z)

Lect0507.c
24
Plot
Write a Comment
User Comments (0)
About PowerShow.com