| CMSC 106 | Project #3 | Spring 2001 |
In this project you will write a program using loops and nested loops. Although as a matter of style and clarity it is usually extremely important to determine the most appropriate type of loop for each iterative task in a program, in order to get practice with all of C's loop statements it is suggested that you intentionally use some of each type in your project.
You are on the committee involved in the bidding to bring the Olympic games to the Baltimore/Washington Area. You are hosting a fund raiser and want to display flags of several countries to encourage people to give money toward your cause. Your program is to create images of flags for these for decorations. (Your program at this point is only going to do 3 different flags, but those three flags are available in a variety of sizes and in either horizontal or vertical orientation.) Not having access to a fancy color graphics terminal, you've decided to start by writing a program which produces facsimiles of flags using letters to indicate which colors they contain.
As you read this section, you may also want to refer to the ``Sample output'' section below.
The input to your program is to consist of a sequence of input lines (redirected from an ASCII text file using standard UNIX input redirection). Each of the lines will have three integers the first representing a country code, the second a flag height (the shorter direction of a rectangular flags), and the third an orientation (horizontal or vertical). Your program will read each input line and then print a flag of the chosen country which is of the size that was entered and in the orientation indicated. Your program is to stop reading input lines and quit once an input line is read which has a country code of 0. That last line will be complete (contain 3 values), but the country code of 0 is what should terminate the program.
Valid country codes, and the countries they represent, are 1 for Mauritius, 2 for Jersey, and 3 for Eritrea (without the gold crest). The flags for these nations appear on the class web site in color. But their descriptions (with details of how you will need to display them in their horizontal orientation) follow:
| Mauritius | 4 parallel stripes of color |
| (in Africa) | All equal in size |
| Parallel to the long direction of the flag | |
| From top to bottom(when horizontal) or right to left (when vertical) : Red, Blue, Yellow and Green | |
| Jersey | A mostly white flag with a red X going from corner to opposite corner |
| (in Europe) | The X should be one character wide regardless of the size of the flag |
| One character wide is defined as for the shorter direction of the flag | |
| which means the stripe will be 3 wide in the longer direction. | |
| Eritrea | A rectangle which is broken into three triangles One triangle is red in |
| (in Africa) | color, is an isosoles triangle and has its base taking the whole left side |
| of the horizontal flag reaching to the middle of the other side. The second is a | |
| green triangle which covers the top right corner (almost reaching the | |
| top left corner to the middle of the right). The third is a blue triangle which covers | |
| the bottom right corner (almost reaching the bottom left corner to the middle of the right). | |
| Note: The red triangle has a thickness of 3 on the top and bottom lines, | |
| is the whole width of the flag in the middle line, and increases by an equal amount | |
| for each line between. Note: completely skip the gold crest which also appears on their flag. |
All the colors on the flags are to be represented using their uppercase first letter (e.g. 'B' for blue, 'W' for white, etc.) with the one exception that the Red color of the flag should be done in a ``-'' (dash or minus sign) which will make it more obvious. When horizontal orientation is selected (by a value of 1 in the input file), the flag will be wider on the screen than it is high. When vertical orientation is selected (by a value of 2), it should be as if that flag is turned 90 degrees clockwise (so that what was on the top is now on the right), and the flag should appear taller on the screen than it is wide. (It will look significantly disproportionately tall because we write letters taller than they are wide.) The longer dimension of the flag is always 3 times the shorter dimension, and the shorter dimension is always what is given by the user.
No flag can be printed which has the shorter dimension larger than 20 (it would just take too much paper). No flag can be printed which has a shorter dimension of less than 3 (it wouldn't be able to show the characteristics of that flag). In addition certain size restrictions apply to the individual flags. In order to be a valid size, the Maruitius flag must be given a size which is a multiple of 4. In order to be a valid size, the Jersey and the Eritrea flags must be given a size which is odd.
If any input line contains an incorrect country code your program must print an explanatory error message and read a new input line without printing a flag for that line. This explanatory error message must contain the exact word ``ERROR'' followed on the same line by the invalid input value which is then followed by the two word sequence ``Country Code''
If the country code is valid, but the input line contains an invalid flag size, your program must print an explanatory error message without printing a flag for that line. This error message must contain the exact word ``ERROR'' followed on the same line by the invalid input value which is then followed by the word ``Size''.
If the country code and size are valid, but an invalid orientation is given, the program should forgive this typographical error and assume the default which is a horizontal flag.
Before each set of output (flag or error message), a line must be printed to tell the person who is reading the output which input line the following output corresponds to. The line must first have an integer (starting with 1 for the first input line) followed immediately by a space which is then followed by a series of exactly 10 stars (asterisks).
cc -std1 -trapuv on the OIT
UNIX Class Cluster. You will lose credit if your program generates any
warning messages when it is compiled. Even if you already know what they
are, you may not use any C language features other than those introduced
in Chapters 1 through 6 of your textbook, plus those presented in lecture
while these chapters were covered. Note that as a result character variables,
arrays anduser-defined functions may not be used. In addition, neither the
goto nor the continue statement may be used, and the
break statement may not be used in any loop. Lastly, your program
must contain only one single return statement at its end, and may not
use the exit() function at all. Using C features not in these
chapters, or using the goto statement, multiple
returns, or break or continue in loops will
result in losing credit.
Your program must have a comment near the top which contains your name, login ID, student ID, your section number, your TA's name, and an original description of the action and operation of the program. Do not put your alias in this comment! Your program should be written using good programming style and formatting, as discussed in class and throughout your textbook. For this project, style is considered to consist of:
Your project must be electronically submitted by the deadline stated above to avoid losing credit per 24 hours as indicated on the syllabus. No project more than 48 hours late will be accepted without prior permission or a valid medical excuse, as described on your syllabus. Lost passwords or other system problems do not constitute valid justifications for late projects. You should start working on your project now!
You may want to skip this section at first, read the rest of the project, and come back to study it carefully when you are about to begin writing your program.
One of the important skills which must be learned in this course is how to track down the causes of and fix both syntax and semantic errors in your code. As your program will inevitably have many errors while you are developing it, you will need to follow procedures such as those described below before coming to office hours for assistance. Should you still not be able to solve the problem and have to come to office hours, you will be required to clearly describe your error and what you have already done to try and find it, and bring current printouts of your source code, and any compiler errors or execution results.
As mentioned in the earlier project assignments it is extremely important to type in only a part of your program at a time and compile and test it to verify that it is correct before going on. Different choices can be made in selecting which parts of a program to implement first; the following is just one possibility. It's fine if you prefer to follow different steps, but whichever order you choose to develop your program it is essential to enter small parts at a time and test each one before going on!
Be sure to compile and thoroughly test the code implementing this step (in fact, you could implement this step itself in several substeps, testing each as you go). These substeps could include assuming the user will only ask for the first type of flag and only in horizontal orientation. Take each step allowing more flexibility in the input - make sure you have the correct values in your input file for the development level you are assuming at that time.
A useful strategy to use for narrowing down the cause of such
types of errors is to comment out one section of the program at a
time and recompile it after each part is commented out, until the
syntax error goes away. At that point, the cause of the error
must be in the most recently commented-out code. If you comment
out parts of your code your program certainly won't work
correctly, but this technique is just to help you quickly find the
syntax error, and once you correct it you can just remove the
/* and */ symbols which you added to comment out
your code.
A few things to keep in mind about this technique are as follows.
You can't comment out only a part of a larger statement, for
instance, you can't comment out the first half of an if
statement but leave the else part- you would have to
comment out the whole if/else. You can't comment
out half of a compound statement such as the opening brace
({) and some of the statements inside but leaving the
closing brace (}) uncommented as part of the code- you
would have to comment out the whole compound statement. You also
can't comment out code containing comments- you would have to
copy your program to a different file, delete the comments, and
then use this strategy. Once you find the syntax error in this
temporary copy of your program you can correct it in your real
version.
A file used for UNIX input redirection (using the < symbol after
the name of a program being run) is just a text file which you can create
with any UNIX text editor (e.g., emacs). You type the exact lines to be
used for the program's input the same way you would have typed them from the
keyboard while the program was running if you were not using input
redirection. To run the program with different input you can modify the
file or create a new one with different contents.
The newline character will not be visible at the end of each line of the input file you create. It is necessary that there be a newline character at the end of the last input line, so be sure you press the return (or enter) key at the end of the last line of any file you create containing input values you are going to test your program with. Otherwise your program may not work properly in some circumstances.
printf
statements. Since you are writing a program to draw something,
the debug printf output will prevent your graph from being
displayed correctly, but it can help you figure out why your
program is wrong and the debug printfs can be easily
removed after that.
printf
statement inside the body of each loop and run your program again,
the printf which gets repeated identifies which loop is the
infinite one. If no line gets repeated, the infinite loop was
either missed when you added the debug printfs or this loop
does not have a body. An extra (unwanted) semicolon in the wrong
place can cause the loop to have a null (empty) statement for its
body.
printf statements and you didn't see their results
printed, so the problem would seem to be above that point in the
program. However, it could be that those statements were
executed, but their results were just not displayed on the screen
because the infinite loop or fatal error occurred soon afterwards
in the program.
It may sound like it's a difficult debugging problem to even
figure out where in your program an error occurs, if printf
results may or may not appear on the screen, but there is an easy
way to see all of a program's output at any time. If a newline
character (\n) is printed, then any pending output which
has not actually been displayed yet will be immediately printed.
So if your program seems to have an infinite loop (or a fatal
error), either simply print some newlines, or even better, as
mentioned above, add debug printf statements to every loop
to see which one has the problem- but make sure each such debug
printf ends in a newline character! Extra newlines or
debug printf results will probably cause your histogram to
appear incorrect, but at least they will help you figure out where
your program's error is, and the extra print statements can be
easily removed later.
If there is any chance you may ever have to come to office hours, you should make your code readable from the very beginning. You should be able to easily understand it, and so should your instructor or a teaching assistant in office hours. If your code is a mess, the teaching assistants will direct you to first clean it up before returning so they can understand it and be able to help you. Naturally you want your code to be clear and easily readable by the graders after you have submitted it as well.
Any evidence of unauthorized use of computer accounts or cooperation on projects will be submitted to the Student Honor Council, which could result in an XF for the course, suspension, or expulsion from the University. Projects are to be written INDIVIDUALLY. For academic honesty purposes, projects are to be considered comparable to a take-home exam. Any cooperation or exchange of ideas which would be prohibited on an exam is also prohibited on a project assignment, and WILL BE REPORTED to the Honor Council.
VIOLATIONS OF ACADEMIC HONESTY INCLUDE:
|
IT IS THE RESPONSIBILITY, UNDER THE UNIVERSITY HONOR POLICY, OF ANY STUDENT WHO LEARNS OF AN INCIDENT OF ACADEMIC DISHONESTY TO REPORT IT TO THEIR INSTRUCTOR.
Turn in your program using the ``submit'' program as before, except using ``3'' for the project number. You are to submit only the .c file containing your source code, not the executable version of your program! If your program is in a file named flags.c, submit would be run as shown. Don't forget to read the class announcements in your instructor's account every time you log in.
% submit 3 flags.c
Before you submit your project, you must exactly follow the specific submission checklist in the ``Testing projects before submitting'' handout separately posted by your instructor!
If the name of the executable version of your program is flags.x, here is a sample execution for the input data shown. Each of the three sample sessions show the input file and then what the program does when run with that input file. The first input set shown is what we will be grading as the primary input - the other two displayed inputs are just to show other output for more advanced features of your program.
Be sure to test your program against a variety of data, so you are sure it works in all circumstances!
|
|