



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A laboratory exercise from a computer science course focusing on looping structures. It includes goals, tasks, and code examples for creating tables of kinetic energy and recurring payment ratio using nested for loops, as well as evaluating infinite series for sine and exponential functions using both for and while loops. The document also covers loop termination problems and equivalent angles.
Typology: Slides
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Larry Caretto Computer Science 106
2
Outline
3
Exercise Six Goals
Tasks for Exercise Six
5
Task One: Kinetic Energy Table
6
Nested for Loops
7
Kinetic Energy Table
8
One Table Row
9
Full Code for One Table Row
from previous chart
10
How to Get Table?
11
Code to Produce Table
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
n 0
n x n!
x
∞ = 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
20
n n n 0
n n 0
n
∞
=
∞
=
n
x n(n 1 )!
(n 1 )! x n!
(n 1 )! x
x
(n 1 )!
x
n!
x
T
n 1
n n 1
n
n 1
n (^) = −
−
21
0 1 0
∞
=
n
n
22
23
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
while ( !converged && n < maxN) { n++; term *= x / n; sum += term; converged = fabs( term ) <= maxError * fabs( sum ); }
25
Why did we exit the loop?
26
Sine Series
∑ ∑
∞ + = 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
= −
− − = − − +
−
− = (^) −
− −+ −
−
27
Using a for Loop
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<