|
|
|
|
|
|
Textbook |
|
Chapter 6 : Iteration |
|
6.2.1: for loops |
|
Chapter 5 : Functions |
|
5.5: Parameters |
|
5.8: Reference Parameters |
|
Package 1 – Patterns |
|
NameUseFunctions |
|
LoopCounting |
|
Package 2 – Recitation 3 |
|
Package 3 – Lab 3 |
|
|
|
|
|
Functions |
|
Parameter passed by value |
|
Parameter passed by reference |
|
Increment operator ++ |
|
Decrement operator -- |
|
|
|
|
|
|
|
More on function |
|
Pass parameters by reference |
|
Flow of control |
|
Repetition – for statement |
|
Format output |
|
|
|
|
|
|
|
Example |
|
Write a function MakeChange. The user needs to
supply one argument that specify the amount in the range of 1 to 99. The
function will use four additional reference arguments to return the number
of quarters, dimes, nickels and pennies needed to make the desired amount. |
|
Solution |
|
|
|
|
|
Any algorithm could be constructed using
combinations of four standardized flow of control structures: |
|
Sequential |
|
Selection (if statement) |
|
Repetition |
|
Invocation. |
|
Many problems require a repetition capacity, in
which the same calculation or sequence of instruction is repeated, over and
over, using different sets of data. |
|
|
|
|
|
|
|
More commonly, a section of code that is
repeated is referred as a loop (C++ term). |
|
Each repetition is also referred as an iteration
or one pass. |
|
In general, there are three different forms of
repetition structures |
|
while structure |
|
for structure |
|
do-while structure |
|
|
|
|
|
|
|
for (initialization; Condition; Loop Update) |
|
{ |
|
statements; //loop body |
|
} |
|
Within the parentheses of the for statement are
three items, separated by semicolons. |
|
Each of these items is optional and can be
described individually |
|
but the semicolons must be presented. |
|
|
|
|
|
|
|
Initialization |
|
set up the counting variable and any other
variables or constants |
|
Loop Continuation Condition |
|
Normally contains the maximum or minimum value
that the counting variable can have |
|
if this condition is true, loop is
repeated; if it is false, the loop
is terminated |
|
Loop Body |
|
The code for the task that will be repeated |
|
Loop Update Statement |
|
for counting loops, this is the code that
increments/decrements the counting variable |
|
Post-Processing code |
|
for any post-processing to be done after the
loop completion |
|
|
|
|
|
|
for (int n = 0; n < 5; n++) |
|
cout << n; |
|
// print: 0 1 2 3 4 |
|
for (int n = 5; n <= 15; n = n + 2) |
|
cout << n; |
|
// print: 5 7 9 11 13 15 |
|
int MINIMUM_VALUE = 3; |
|
for (int n = 8; n >= MINIMUM_VALUE ; n --
) |
|
cout << n; |
|
// print: 8 7 6 5 4 3 |
|
|
|
|
int n = 0; |
|
for ( ; n < 5; n++) |
|
cout << n; // print: 0
1 2 3 4 |
|
int n = 0; |
|
for ( ; n < 5; ) { |
|
cout << n; |
|
n = n + 1; |
|
} |
|
cout
<< “Hello !” << endl; |
|
// print: 0 1 2 3 4 Hello |
|
for ( ; ; )
{ // infinite loop |
|
statements; |
|
} |
|
|
|
|
|
|
|
|
More on function |
|
Pass parameters by reference |
|
Flow of control |
|
Repetition – for statement |
|
Format output |
|
|
|
|
|
|
Besides displaying correct results, a program
should present its results attractively. |
|
|
|
The format of numbers displayed by cout can be
controlled by field width manipulators included in each output stream. |
|
|
|
|
|
|
|
cout << “The sum of 6 and 15 is “ <<
setw(3) |
|
<< 21 << endl; |
|
The setw(3) field width manipulator included in
the stream of data passed to cout is used to set the displayed field width. |
|
The 3 in this manipulator sets the default field
width for the next number in the stream to 3 spaces. |
|
This field setting causes 21 to be displayed in
a field of 3 spaces, which includes one blank space and the number 21. |
|
|
|
|
21 |
|
Integers are right-justified within the
specified field. |
|
Field width manipulators are useful in printing
columns of numbers that are correctly aligned. |
|
|
|
|
#include <iostream.h> |
|
int main() { |
|
cout << 6 << endl |
|
<< 18 << endl |
|
<< 124 << endl |
|
<<"---" << endl |
|
<< (6 + 18 + 128) << endl; |
|
return 0; |
|
} |
|
6 |
|
18 |
|
124 |
|
--- |
|
148 |
|
|
|
|
|
|
|
#include <iostream.h> |
|
#include <iomanip.h> |
|
int main() { |
|
cout
<< setw(3) << 6
<< endl |
|
<< setw(3) << 18
<< endl |
|
<< setw(3) << 124
<< endl |
|
<<"---" << endl << (6 + 18 + 124)
<< endl; |
|
return 0; |
|
} |
|
6 |
|
18 |
|
124 |
|
--- |
|
148 |
|
|
|
|
|
|
The field width manipulator must be included in each
occurrence of a number inserted into the data stream sent to cout |
|
The manipulator only applies to the next
insertion of data immediately following it. |
|
|
|
|
|
|
Formatted floating-point numbers require the use
of two filed width manipulators. |
|
The first manipulator sets the total width of
the display, including the decimal point |
|
setw(10) |
|
The second manipulator determines how many
digits can be printed to the right of the decimal point |
|
setprecision(3) |
|
|
|
|
|
|
cout << "|" << setw(10) |
|
<< setiosflags(ios::fixed) |
|
<< setprecision(4) |
|
<< 123.89 |
|
<< "|" << endl; |
|
|
123.8900| |
|
|
|
|
|
|
|
|
|
cout << "|" << setw(10) |
|
<< setiosflags(ios::left) |
|
<< 142 <<
"|" << endl; |
|
|142
| |
|
cout << "|" << setw(10) |
|
<< setiosflags(ios::right) |
|
<< 142 <<
"|" << endl; |
|
|
142| |
|
|
|
|
|
More on function |
|
Pass parameters by reference |
|
Flow of control |
|
Repetition – for statement |
|
Format output |
|
|
|
|
|
|
|
|
Flow of control |
|
Repetition – for statement |
|
Nested loop |
|
Random Number generating |
|
|
|