Docsity

Exercise 6: Looping in Computer Science - Creating Tables and Evaluating Infinite Series, Slides of Computer Science

Toggle sidebar
bg1
Exercise 6 -- Looping March 9, 2006
1
Laboratory VI
Laboratory VI –
–Program Control
Program Control
Using Loops
Using Loops
Larry Caretto
Computer Science 106
Computing in Engineering
and Science
March 9, 2006
2
Outline
• Exercise six goals
• Outline tasks for exercise six
• Introduce idea of nested loops and table
generation
• Provide details for some tasks
3
Exercise Six Goals
• As a result of this exercise you should
be able to accomplish the following:
– write looping structures using both the
while and for commands
– write programs with nested loops
– prepare a table of values for a function of
two variables
– write a program to compute the sum of an
infinite series
4
Tasks for Exercise Six
• One –copy and paste code to produce
table of kinetic energy as a function of
mass and velocity
•Two –modify task one code to create
similar table for a different formula
• Three –write code with a while loop to
sum an infinite series for sin(x)
• Four –modify task three code to use a
for loop in place of a while loop
5
Task One: Kinetic Energy Table
•KE = mV
2/2
• Copy and execute code from exercise
• Code prints table of KE as a function of
mass, m, and velocity, V
– 1 kg ≤m ≤25 kg, with ∆m = 1 kg
–6 m/s≤V ≤15 m/s, with ∆V = 1 m/s
• Modify this code for task two
6
Nested for Loops
• Can have an inner for loop nested
inside an outer for loop
• Example, print table of kinetic energy
such as the one below
Velocity values m/s below
6 7 8 9 10
Mass
1 18.0 24.5 32.0 40.5 50.0
2 36.0 49.0 64.0 81.0 100.0
pf3
pf4
pf5

Partial preview of the text

Download Exercise 6: Looping in Computer Science - Creating Tables and Evaluating Infinite Series and more Slides Computer Science in PDF only on Docsity!

Laboratory VILaboratory VI^ – –^ Program ControlProgram Control

Using LoopsUsing Loops

Larry Caretto Computer Science 106

Computing in Engineering

and Science

March 9, 2006

2

Outline

  • Exercise six goals
  • Outline tasks for exercise six
  • Introduce idea of nested loops and table

generation

  • Provide details for some tasks

3

Exercise Six Goals

  • As a result of this exercise you should

be able to accomplish the following:

  • write looping structures using both the while and for commands
  • write programs with nested loops
  • prepare a table of values for a function of two variables
  • write a program to compute the sum of an infinite series 4

Tasks for Exercise Six

  • One – copy and paste code to produce

table of kinetic energy as a function of

mass and velocity

  • Two – modify task one code to create

similar table for a different formula

  • Three – write code with a while loop to

sum an infinite series for sin(x)

  • Four – modify task three code to use a

for loop in place of a while loop

5

Task One: Kinetic Energy Table

  • KE = mV^2 /
  • Copy and execute code from exercise
  • Code prints table of KE as a function of

mass, m, and velocity, V

  • 1 kg ≤ m ≤ 25 kg, with ∆m = 1 kg
  • 6 m/s ≤ V ≤ 15 m/s, with ∆V = 1 m/s
  • Modify this code for task two

6

Nested for Loops

  • Can have an inner for loop nested

inside an outer for loop

  • Example, print table of kinetic energy

such as the one below

Velocity values m/s below

Mass

7

Kinetic Energy Table

  • Use nested for loops
  • Inner loop calculates and prints one row

of the table

  • Outer loop does inner loop for all rows
  • Use type int variables for loop indices
  • Convert to double before division by 2
  • Need initial loop to print headers for

each column

8

One Table Row

  • Each row prints a mass then prints the

KE for each velocity from 6 to 15

  • What is loop index for printing a row?

cout << setw(3) << m;

for( int v = 6; // initialize

v <= 15; // continue

v++ ) // increment

9

Full Code for One Table Row

cout << setw(3) << m;

for( int v = 1; v <= 10; v++ )

double KE = double( m * v

* v ) / 2;

cout << setw(7) << KE;

from previous chart

10

How to Get Table?

  • Print column header row
  • Loop over all values of mass
    • Move output to a new line
    • For each value of mass, use code just

developed to print one row

  • End loop over mass

11

Code to Produce Table

// Put column header code here

for( int m = 1; m <= 10; m++ )

cout << endl;

//Code for one row

cout << endl;

Task One Code for ( int V = 6; V <= 15; V++ ) { // print column header with spacing } for ( int m = 1; m <= 25; m++ ) //row loop { cout << "\nm = " << setw(2) << m; for ( int V = 6; V <= 15; V++ ) //cols { double KE = 0.5 * m * V * V; cout << setw(7) << KE; } } 12

19

Infinite Series for e x

∞

n 0

n x n!

x

e n!^ =^ n ( n −^1 )( n −^2 )L(^3 )(^2 )(^1 )

= ∑ = + + + +L = + + + + L

∞ = 2 6

2 3 2 3 0

x x x x x x n

e x n

x n

n

n! = n ( n − 1 )! or ( n − 1 )!= n!

1 1 1 0! ( 1 1 )!^1! 2 ( − 1 )!=! ⇒ 1 !=( 2 − 1 )!=^2 != and = − = = n n n

( n − 1 )!=( n - 1 )( n - 2 )L( 3 )( 2 )( 1 )

20

Ratio of Terms in ex^ Series

n!

x

T T

n!

x

e

n n n 0

n n 0

n

x = = ⇒ =

∞

=

∞

=

n

x n(n 1 )!

(n 1 )! x n!

(n 1 )! x

x

(n 1 )!

x

n!

x

T

T

n 1

n n 1

n

n 1

n (^) = −

−

21

Coding the Series

  • Code for this approach

newTerm = oldTerm * x / n;

seriesSum = seriesSum + newTerm

oldTerm = newTerm;

0 1 0

where 1 −

∞

=

= ∑ = n = n

n

n

x T

n

x

e T T and T

22

Alternative Coding for Series

  • Code from last page newTerm = oldTerm * x / n; seriesSum = seriesSum + newTerm oldTerm = newTerm;
  • Simpler Code uses one term variable term = term * x / n; sum = sum + term;
  • Still Simpler Code term *= x / n; sum += term;
  • Must initialize sum and term properly

23

Starting the Loop

const int maxN = 100; const double maxError = 1e-12; bool converged = false; double term = 1; // change this! double sum = term; int n = 0; while ( !converged && n < maxN) { // see next slide

24

Loop Body

while ( !converged && n < maxN) { n++; term *= x / n; sum += term; converged = fabs( term ) <= maxError * fabs( sum ); }

25

Why did we exit the loop?

if ( converged )

cout << “For x = “ << x <<

“, exp(x) = “ << sum;

else

cout << “No Convergence”;

26

Sine Series

= − + − L

∑ ∑

∞

∞ + = 5!

x 3!

x x ( 2 n 1 )!

sin(x) T (^1 )x^35 n 0

n 2 n 1 n 0 n

2 n( 2 n 1 )

x ( 2 n 1 )!

( 2 n 1 )! x

x ( 1 )

( 1 )

[ 2 (n 1 ) 1 ]!

( 1 ) x

( 2 n 1 )!

( 1 )x

T

T 2 2 n 1

2 n 1 n 1

n n 1 2 (n 1 ) 1

n 2 n 1

n 1

n

= −

− − = − − +

−

− = (^) −

− −+ −

−

  • Similar, but more complicated, than ex
  • First term in series is x, not 1

27

Using a for Loop

  • Continuation condition can be complex
  • Remember while condition for series while ( !converged && n < maxN)
  • Can have similar condition in for loop
  • Can also have multiple initializations or

conditions separated by a comma

for ( n = 0, converged = false;

!converged && n < maxN; n++ )

  • Watch first n value and increment 28

Style: Indent Structures

while ( inFile.good() ) { inFile << hours << rate; if ( hours > 40 ) pay = rate * ( 40 + 1.5 * ( hours – 40 ) ); else pay = rate * hours;

outFile << pay << endl; }

29

Bad Style

while(inFile.good()){inFile<40)pay=rate(40+1.5( hours–40));else pay=rate*hours; outFile<