CS111 PreLab 10
First, you should do the reading for this week's problem set.
Bring any questions you have to lab.
Practicing forms of iteration
We've seen how to create "rows" of pictures in PictureWorld.
The recursive definition for such a method is given below:
// This method returns a picture with p arranged in numberItems
// equally spaced columns (ie a row with numberItems elements).
public Picture row (Picture p, int numberItems) {
if (numberItems<=0) { // base case: a row with no elements is empty
return empty();
} else { // general case: a row is a picture with a row of one
// fewer number of elements to its right
return beside(p, row(p, numberItems-1), 1.0/numberItems);
}
}
Rewrite the method in three ways:
- iterative strategy
- using a while loop
- using a for loop
Is this method a good candidate for using a for loop? Why or why not?