Goals for this lab:
forloop where appropriate.
Copy example loop_lab_stub/loop_lab.cs to a new project of yours, and fill in function bodies for each part below:
/// Print n copies of s, end to end. /// For example PrintReps("Ok", 9) prints: OkOkOkOkOkOkOkOkOk static void PrintReps(string s, int n)
Hint: How would you do something like the example
PrintReps("Ok", 9) or with a higher count by hand?
Probably count under your breath as you write:
1 2 3 4 5 6 7 8 9 OkOkOkOkOkOkOkOkOk
This is a counting loop.
/// Return a string containing n copies of s, end to end. /// For example StringOfReps("Ok", 9) returns: "OkOkOkOkOkOkOkOkOk" static string StringOfReps(string s, int n)
Note the distinction from the previous part: Here the function prints nothing. Its work is returned as a single string. You have to build up the final string.
Factorial, in a format much like SumToN in example
/// Return n! (n factorial: 1*2*3 *...* n if n >=1; /// 0! is 1.). For example Factorial(4) returns 1*2*3*4 = 24. static int Factorial(int n)
It is useful to think of the sequence of steps to calculate a concrete example of a factorial, say 6!:
Start with 1 2 *1 = 2 3*2 = 6 4 * 6 = 24 5*24 = 120 6*120 = 720
ALSO find the largest value of
n for which the function works.
(You might want to add a bit of code further testing Factorial,
to make this easier.) Caution: although a negative result from the
product of two positive numbers is clearly wrong, only half of the
allowed values are negative, so the first wrong answer could equally well
Modify the function to return a
Then what is the largest value of
n for which the function works?
Remember the values from this part and the previous part to tell the TA’s checking out your work.
Complete the method
// Print a filled rectange, where the filling is // the specified number of columns and rows of the character inChar, // surrounded by a border made of the character edgeChar. // For example printRectangle(3, 2, 'i', 'e') prints // eeeee // eiiie // eiiie // eeeee static void PrintRectangle(int columns, int rows, char inChar, char edgeChar)
Here are further examples:
PrintRectangle(5, 1, ' ', 'B'); PrintRectangle(0, 2, '-', '+');
BBBBBBB B B BBBBBBB ++ ++ ++ ++
Suggestion: You are always encouraged to build up to a complicated solution incrementally. You might start by just creating the inner rectangle, without the border.
Complete the method below.
/// Print the borders of the cells of a table. /// The borders divide the table into rows and columns. /// The blank space within a cell is width characters wide /// and continues down for height lines. /// The horizontal borders are dashes '-' and the vertical borders /// are vertical bars, '|', except that all intersections are '+'. /// For example PrintTableBorders(3, 2, 4, 1) prints /// +----+----+----+ /// | | | | /// +----+----+----+ /// | | | | /// +----+----+----+ static void PrintTableBorders(int columns, int rows, int width, int height)
Here is further example:
PrintTableBorders(2, 1, 6, 3);
would print (with actual vertical bars)
+------+------+ | | | | | | | | | +------+------+
You can do this with lots of nested loops,
or much more simply you can use
StringOfReps, possibly six times
in several assignment statements,
and print a single string. Think of larger and larger building blocks.
The source of this book is plain text where some of the tables are laid out in a format similar to the output of this function. The Emacs editor has a mode that maintains a fancier related setup on the screen, on the fly, as content is added inside the cells!