| CSCI A201/A597Lab Notes 4 Spring 2000 |
Lab 4
quiz
in QuizSite contains your next assignment.
Work with the items that have been posted as "Lab4". You can submit up to ten times and you can run the programs before you submit the answers, so identifying the right answers will not be a problem. Understanding why they are the right ones is the important part, so next time you need to turn in a file that contains the right answers and a justification why those are the right answers, for each question.
If you still have trouble with the Crossed problem think for
a minute about the following.
You are supposed to print a square of characters, spaces and asterisks. This square of characters is printed in slices (because you have no other way of printing it). So you will be printing lines of characters, one by one.
On each line you will be printing the characters one by one until, at the end of the line, you need to print an end of line.
Unless you count all the characters you won't be able to know for sure how many you printed. And since you're printing a certain numbers of characters per line, and a certain numbers of lines overall, in the end each of the characters that you print (be it space or an asterisk) will be end up on a line and a column, so the number of the line, and the number of the column will make it clear what asterisk we are talking about.
In the end we will be printing columns characters per line, on a total of lines lines. In our case the user requests a certain size, and the number (s)he specifies will determine both the width and the height of the resulting square of characters (will be equal to both).
So the square of characters will actually be mapped on the following grid of pair of numbers (in each cell the numbers identify the line and the column on which the cell is):
So if you know how to create a square of asterisks like this all you need to do to create the pattern that we want to produce will be to identify its components.
What's the property of the asterisks on the left border of the square? Their column is indexed as 0 (zero). Whatever variable we use to keep track of the column on which we are printing, checking its value against zero will give us the answer to whether the character is on the first column or not at the time we are printing it.
Same thing about the other vertical border, whose index however
is 3 (or, in fact, size - 1).
The horizontal border has also index 0 (zero) but the index (or counter) that keeps track of lines is different from the ones that keeps track of columns. (One of them is in the first position and the other one is in the second).
We need to find similar relationships for the diagonals and then we would be set.
The program simply counts all the cells in the square and for each cell, if the indices for the line and the column on which the cell is satisfy one of the six conditions (for the four borders and the two diagonals) then an asterisk needs to be printed. Otherwise a space is printed.
I also include the text of the eleven questions here. This is just
for your reference. Please go into QuizSite to submit your answers
to these questions.