![]() |
CS210 - Algorithms and Data Structures I Department of Computer Science National University of Ireland, Maynooth |
T Naughton, NUIM
Back to CS210 home
Laboratory Sheet 2
Iteration and stepwise refinement. This lab exam will allow you to practice your while loop programming skills and will (re-)introduce the concept of stepwise refinement.
You are expected to understand the programs you write (especially such simple ones that you will deal with for this lab session). Using test data is NOT an effective way of determining whether your program works or not. Each of you is more intelligent than a computer and should be able to reason about what your programs do before running them. You will therefore not be permitted to use a compiler. Use your lab books for all writing.
The format for writing in your lab books is the same as always: Every page should contain the date and lab number. Every program or piece of roughwork should clearly state which problem it refers to. This will insure you get the marks you deserve during my spot-checks.
In order to help you reason about the programs you write, for the first few minutes of the lab you will be taken through the design and pseudocode of the following functions.
Problem 2.A Write a function that takes an integer n as a parameter and writes out n rows of asterisks to the screen as shown below. Sample output of the function is shown below, when n has the value 4.
*
**
***
****
The following function would be suitable.
void TwoA(int n) {
/* This function prints out a triangle of stars, in n rows,
** as follows: *
** **
** *** (and so on...)
*/
int row, count;
if (n < 0) {
println("n must be >= 0");
} else {
row = 0;
while (row < n) {
count = 0;
while (count < (n - row - 1)) {
print(' ');
count++;
}
count = 0;
while (count < (row + 1) {
print('*');
count++;
}
println;
row++;
}
}
}
Problem 2.B Write a function that takes an integer n as a parameter and writes out n rows of asterisks to the screen as shown below. This sample output of the function occurs when n has the value 4.
****
* *
* *
****
Now attempt the following problems on your own.
Problem 2.1 Write a function that takes an integer n as a parameter and writes out n rows of text to the screen. [Hint: Each row (where the row counter i goes from 0 to (n - 1)) contains i blanks followed by (i + 1) 'a' characters.] Sample output of the function is shown below, when n has the value 4.
a
aa
aaa
aaaa
Your function should have the following structure.
void TwoOne(int n) {
/* This function prints out n rows, each containing
** (n-1) blanks followed by (n) 'a' characters.
*/
int... // Your variables here
if (n < 0) {
println("n must be >= 0");
} else {
// Your code here
}
}
Problem 2.2 Write a function that takes an integer n as a parameter and writes out n rows of asterisks to the screen as shown below. This sample output of the function occurs when n has the value 4.
****
***
**
*
Problem 2.3 Write a function that takes an integer n as a parameter and writes out 2n rows of asterisks to the screen as shown below. This sample output of the function occurs when n has the value 4.
****
***
**
*
*
**
***
****
Problem 2.4 Write a function that takes an integer n as a parameter and writes out n rows of asterisks to the screen as shown below. This sample output of the function occurs when n has the value 4.
* ****
* ***
* **
* *
Problem 2.5 Write a function that takes an integer n as a parameter and writes out n rows of asterisks to the screen as shown below. This sample output of the function occurs when n has the value 4.
****
****
****
****
Problem 2.6 Write a function that takes an integer n as a parameter and writes out n rows of asterisks to the screen as shown below. This sample output of the function occurs when n has the value 4.
****
* *
* *
****
Problem 2.7 Write a function that takes an integer n as a parameter and writes out n2 rows of characters to the screen as shown below. This sample output of the function occurs when n has the value 2.
****
*XX*
*XX*
****
This sample output of the function occurs when n has the value 3.
*********
*********
*********
***XXX***
***XXX***
***XXX***
*********
*********
*********
Problem 2.8 Write a function that takes an integer n as a parameter and writes out n2 rows of characters to the screen as shown below. This sample output of the function occurs when n has the value 2.
X**X
*XX*
*XX*
X**X
This sample output of the program occurs when n has the value 3.
X*******X
*X*****X*
**X***X**
***X*X***
****X****
***X*X***
**X***X**
*X*****X*
X*******X