Introduction to C Programming Assignment 1: Conditional

advertisement
Introduction to C Programming
Assignment 1: Conditional Statements and Loops
Objective
The objective of this assignment is to exercise the use of conditional statements
and loops. The usage of the modulo (%) operator is also central to this assignment.
The Program
You will write a program that asks the user for 3 numbers. The first one will
represent the dimensions of a square that will be printed; the second number will
be used as the maximum amount of chars per line; and the third number will
represent the amount of lines printed by the program. These amounts do not
include the chars/lines used by the square's frame.
For example, if the user enter 4 16 16 that means that a square of 4x4 chars should
be printed. A 4x4 square should be printed until the amount of chars (not including
the frame) per line is 16 and the amount of lines (not including the frame) printed
is 16.
The squares printed are not always equal, although there is a pattern to the
printing – we will take a look at this pattern later on. The horizontal part of the
frame also has a pattern. Again, we will see about that later.
Please, notice that the number of chars per line and the number of lines to print
are not related to the size of the square (you may have to stop printing before you
finish 'drawing' a whole square).
You have to make sure that the numbers inserted by the user are all positive
numbers. If the user entered a non-positive number (including zero), you should
ask again for input.
How to write your program
We will suggest a step-by-step approach to solving the assignment. It is very
recommended you follow these instructions, but you are allowed to solve it your
own way, if preferred.
In this assignment, in order to reduce spelling mistakes to a minimum, you won't
write the printf calls by yourself. In fact, you are not allowed to write any call to
printf. Instead, you may use only the calls defined in beginning of the .c file.
For example, if you need to print a newline ('\n') during this assignment, you
should use the printNewLine definition instead of writing printf("\n") by
yourself.
Step 0: input control
Ask the user for the input. Use the printStart definition to do so. If the input is
not legal, ask the user again. Repeat these actions until the input is correct.
Don't forget to say good bye to the user before the end of your program by using
the printTchau definition.
Step 1: basic square
Start by printing a single square of the right dimensions. For example, if the first
number entered was 7, then the square should be 7x7. Like this:
+++++++
++++++–
+++++––
++++–––
+++––––
++–––––
+––––––
In order to print the '+', you should use the printPlus definition. In order to print
the '-', you should use the printMinus definition. Notice that, in this example, you
start by printing 7 '+' and no '-'. In each line you increase the amount of '-' chars
and decrease the amount of '+'.
Step 2: horizontal square repetition
Now that you know how to print a single square, print a series of squares (line-byline) up to the amount of chars permitted by line. For example, if the number of
chars per line asked by the user is 18, then you should print:
++++++++++++++++++
++++++–++++++–++++
+++++––+++++––++++
++++–––++++–––++++
+++––––+++––––+++–
++–––––++–––––++––
+––––––+––––––+–––
The color is just for emphasis, of course. Notice that when we finish printing the
chars of the first square (in a line), we start printing the second square and so on.
We continue doing that until we reach the amount of chars that can be printed per
line.
Step 3: vertical frame
The next step is to add the vertical part of the frame (a separation between the
squares). Using the previous example, now your output should look like this:
#+++++++#+++++++#++++#
#++++++–#++++++–#++++#
#+++++––#+++++––#++++#
#++++–––#++++–––#++++#
#+++––––#+++––––#+++–#
#++–––––#++–––––#++––#
#+––––––#+––––––#+–––#
To print the '#', you should use the printNormalFrame definition.
Step 4: vertical square repetition
Now, we will take into consideration the amount of lines that the user asked us to
print. Suppose that the number of lines should be 10. This means that we will have
to start again to print new squares in line 8, since each square has to take up to 7
lines. Now, the output should look line that:
#+++++++#+++++++#++++#
#++++++–#++++++–#++++#
#+++++––#+++++––#++++#
#++++–––#++++–––#++++#
#+++––––#+++––––#+++–#
#++–––––#++–––––#++––#
#+––––––#+––––––#+–––#
#+++++++#+++++++#++++#
#++++++–#++++++–#++++#
#+++++––#+++++––#++++#
Step 5: horizontal frame
Next, we will add the horizontal part of the frames. In order to do that, we have to
calculate exactly the amount of chars to print for the horizontal part of the frame.
The output of your program should look like this now:
######################
#+++++++#+++++++#++++#
#++++++–#++++++–#++++#
#+++++––#+++++––#++++#
#++++–––#++++–––#++++#
#+++––––#+++––––#+++–#
#++–––––#++–––––#++––#
#+––––––#+––––––#+–––#
######################
#+++++++#+++++++#++++#
#++++++–#++++++–#++++#
#+++++––#+++++––#++++#
######################
Step 6: horizontal frame fixed
If you run our program, you will notice that the horizontal part of the frames is
composed not only by '#' but also by '@'. The ith '#' to be printed is exchanged by
an '@' anytime i is a prime number – start counting from 0 and remember that the
first prime number is 2.
In the example we are considering, the horizontal part of the frame should be:
i=2 i=5
i=11
i=17
##@@#@#@###@#@###@#@##
i=7
i=13
i=3
i=19
Remember that a prime number is a number that is divisible (remainder=0) just by
1 and itself.
To print a '@', you should use the printSpecialFrame definition. Now, the output
should look like this:
##@@#@#@###@#@###@#@##
#+++++++#+++++++#++++#
#++++++–#++++++–#++++#
#+++++––#+++++––#++++#
#++++–––#++++–––#++++#
#+++––––#+++––––#+++–#
#++–––––#++–––––#++––#
#+––––––#+––––––#+–––#
##@@#@#@###@#@###@#@##
#+++++++#+++++++#++++#
#++++++–#++++++–#++++#
#+++++––#+++++––#++++#
##@@#@#@###@#@###@#@##
Step 7: squares almost fixed
If we observe the output of the program, we will notice that not all 'squares' are
equal. Instead, in part of them, the lines start with '+' and, in some, the lines start
with '-'.
There is a pattern to it. Let's take a look at the final output of our program, while
giving an horizontal index to our squares:
H=0
Lines start with '+'
H=1
H=2
##@@#@#@###@#@###@#@##
#+++++++#+++++++#++++#
#++++++–#–++++++#++++#
#+++++––#––+++++#++++#
#++++–––#–––++++#++++#
#+++––––#––––+++#+++–#
#++–––––#–––––++#++––#
#+––––––#––––––+#+–––#
##@@#@#@###@#@###@#@##
#–––––––#–––––––#––––#
#+––––––#––––––+#+–––#
#++–––––#–––––++#++––#
##@@#@#@###@#@###@#@##
Lines start with '-'
Try to figure out the pattern of the starting character of the squares. Change your
program, so every square where the lines should start with '-', start with '-'. Your
output should look like this now:
##@@#@#@###@#@###@#@##
#+++++++#–––––––#++++#
#++++++–#––––––+#++++#
#+++++––#–––––++#++++#
#++++–––#––––+++#++++#
#+++––––#–––++++#+++–#
#++–––––#––+++++#++––#
#+––––––#–++++++#+–––#
##@@#@#@###@#@###@#@##
#+++++++#–––––––#++++#
#++++++–#––––––+#++++#
#+++++––#–––––++#++++#
##@@#@#@###@#@###@#@##
Step 8: squares fixed
This is the last step. Again, let's take a look at the final output and give also vertical
indexes for the squares.
H=0
V=0
Amount of initial chars decrease per line
V=1
H=1
H=2
##@@#@#@###@#@###@#@##
#+++++++#+++++++#++++#
#++++++–#–++++++#++++#
#+++++––#––+++++#++++#
#++++–––#–––++++#++++#
#+++––––#––––+++#+++–#
#++–––––#–––––++#++––#
#+––––––#––––––+#+–––#
##@@#@#@###@#@###@#@##
#–––––––#–––––––#––––#
#+––––––#––––––+#+–––#
#++–––––#–––––++#++––#
##@@#@#@###@#@###@#@##
Amount of initial chars increase per line
Use the horizontal and vertical indexes to figure out the pattern and adapt your
program to it.
Important points
1. Try to think of 'difficult' inputs to try on. For example, what happens to our
program when the dimension of the square is bigger than the amount of
chars to be printed per line?
2. Your program should take more or less the same amount of time as ours. If
your program is faster, great! But, if it is considerably slower, you should
rewrite it in a more efficient manner.
3. Just to have a perspective: our solution to this assignment took less than
100 lines of code (not including blank or comment lines). We are not
expecting you to write 500 lines of code in your first assignment. If you get
to a huge amount of code, you are missing something.
4. Don't forget that you are not allowed to submit your assignment before
you've used our in/out files (as in lab 2).
Good Luck!
Download