Key syntax elements for lab 3
The C++ feature we will focus on for lab 3 covers pass-by-reference, recursion,
and basic loops. (Refer back to the course notes or to the
lab 2 syntax examples for refreshers
on the syntax used earlier.)
Pass-by-reference
Remember that regular parameter passing simply passes a copy of a value,
whereas pass-by-reference allows the called function to access/change the
original variable passed to it.
This is done by adding the & symbol before the parameter name in the
formal parameter list (in the prototype and declaration of the function).
For example, in the program below the negate function switches
the passed parameter between positive and negative.
#include <iostream>
using namespace std;
void negate(float &orig);
int main()
{
int x = 3;
negate(x);
// x has now been changed to -3
cout << x << endl;
}
void negate(float &orig)
{
// replace the original value with the opposite
orig = - orig;
}
|
Recursion
The appropriate use of recursion is to have an if statement that
checks for a base case (or stopping condition), and otherwise
issues a recursive call with a slightly different parameter list.
For example, the sum function below adds all the integers from M to N,
and returns the result:
int sum(int M, int N)
{
// base case: if M > N return 0
if (M > N) {
return 0;
}
// general case, M must be <= N,
// so compute the sum from (M+1) to N,
// add M to that,
// and return the result
int result = M + sum(M+1, N);
return result;
}
|
So if our main routine contained something like
x = sum(5,9);
then the sequence of events would be
1. sum(5,9) calls sum(6,9) and waits for an answer
2. sum(6,9) calls sum(7,9) and waits for an answer
3. sum(7,9) calls sum(8,9) and waits for an answer
4. sum(8,9) calls sum(9,9) and waits for an answer
5. sum(9,9) calls sum(10,9) and waits for an answer
6. now in sum(10,9), M > N so it returns 0
7. now sum(9,9) can return 9+0, i.e. 9
8. now sum(8,9) can return 8+9, i.e. 17
9. now sum(7,9) can return 7+17, i.e. 24
10. now sum(6,9) can return 6+24, i.e. 30
11. now sum(5,9) can return 5+30, i.e. 35
Loops
The C++ syntax we will focus on for lab 5 covers while loops,
do-while loops, and for loops.
- while loops
these are top tested loops, and must include appropriate
instructions inside the loop to update the condition being checked
// print the values from 10 through 20, one per line
int x = 10;
while (x <= 20) // enter the body of the loop if x is less than or equal to 20, otherwise
{ // skip the body of the loop and go on with the rest of the program
printf("%d\n", x); // do the loop actions (in this case just a single print statement)
x++; // update x for the next potential pass through the loop
} // upon reaching the bottom of the loop, go back up to the test condition
// rest of the program would be down here
|
- do while loops
these are bottom tested loops, so always run the body of the loop at least once
(since you don't encounter the test condition until after the first pass through the loop)
int x = 10;
do
{
printf("%d\n", x);
x++;
} while (x <= 20); // if x is <= 20 go back and do the loop body again,
// otherwise leave the loop now
|
- for loops
for loops are top tested, but allow you to initiaze a loop counter
as part of the loop header, as well as the counter update statement
for loop | equivalent while loop |
for (int x = 1; x < 10; x++)
{
printf("%d\n", x);
}
|
int x = 1;
while (x < 10)
{
printf("%d\n", x);
x++;
}
|
Note the condition is checked at the top of the for loop, and the update
is applied at the bottom.
- nested loops
If we put one loop inside another, each time we go through the "outer" loop
once we might go through the "inner" loop many times. For example, in the code
below the outer loop controls which row we're working on, the inner loop controls
which column we're printing in the current row:
code | output |
for (int row = 1; row < 8; row++)
{
printf("Row %d: ", row);
for (int column = 1; column < 5; column++) {
printf("%d", column);
}
printf("%d\n", x);
}
|
Row 1: 1234
Row 2: 1234
Row 3: 1234
Row 4: 1234
Row 5: 1234
Row 6: 1234
Row 7: 1234
|