CSCI 112: Programming and Algorithms II
Spring 2006
Program 1: Chart
Code due: 8am Monday
February 6 (copied to your ECC turn-in
directory, see below)
Grading Weight: 1 (programs will have a weight between 1 - 5)
Overview:
The
goal of this program is to introduce you to C++ and programming in the
UNIX
environment.
This program reads many digits (0, 1, 2, ..., 9) from standard input
and draws a
bar chart to standard output using asterisks and spaces ('*' and ' ')
to illustrate how many times each digit was entered.
When the user enters a -1, the program will stop reading standard input
and print the chart.
For example, if the user entered, 3 2 7 3 1 3 2 8 9 -1, your program
should print:
*
* *
* * * * * *
0 1 2 3 4 5 6 7 8 9
Since the 1 appeared once in the input, there is one asterisk
above the 1, since there were three threes, there are three asterisks
above the 3, and so forth.
Program Requirements:
Read any number of digits from standard input. You may
assume that the only input are digits (0-9) and a single "-1" In
other words, I will NOT test for errors (however, in all other
assignments I will test for errors).
After all the digits are read (that is, when the user enters a
-1),
draw the corresponding bar chart.
For example consider the following input/output (I use "$" to represent
the UNIX command prompt):
$ chart
1 2 2 3 3 3 7 8 9 -1
*
* *
* * * * * *
0 1 2 3 4 5 6 7 8 9
$
The user typed "chart" (the name of the program's executable) at
the UNIX command prompt and then pressed return.
Then the user typed in the text:
<1><space><2><space><2><space><3><space><3><space><3><space><7><space><8><space><9><space><-1><return>
Note: text between a "<"
and a ">" is text that the user types. Sometimes it is a
single key, sometimes it is a string.
Then the program printed the asterisks and spaces. Note:
there is a column of spaces before the bar for 0. This
makes the assignment easier to implement. There must not be any spaces
after the last column and there must not be any black lines before or
after the chart.
The final "$" is the UNIX command prompt, it is not printed by the
program.
Your program must work exactly like this. It must not print
out
anything else.
Note: when you type a command at the UNIX prompt, the shell looks
for a
file with that name in all the directories in your path (a path is just a list of
directories). If the
current
directory (called ".") is in your path, then you can just type
"chart". If it is not in your path, you have to type
"./chart" You can add "." to your path by editing the file in
your home directory called .bash_profile and adding the following
line. If you don't have a .bash_profile file, create a new
one.
Add the following line to your .profile.
export PATH=.:$PATH
Testing
Your Program:
I will provide sample tests for your program in the tests/p1 directory.
Each test consists on a single input file (with a name like t01) and a
single corresponding output file (with a name like t01.out). If
you copy these files to your directory, you can test your program as
follows:
$ chart < t01 > t01.myout
$ diff t01.myout t01.out
$
The program diff looks for differences in files. If there are
differences it prints them. If there are no differences, it
doesn't print anything.
Thus if you do the above for each test in the p1 test directory, and
diff does not print anything, you pass all of my sample tests.
However, I will use tests that I
don't post to grade your program, so it is a good idea to develop some
of your own tests.
If you are programming using windows, every line in your program will
contain an extra hidden character (the DOS standard is different than
the UNIX standard). I will only post UNIX files. However,
you can convert your DOS files to UNIX files using the commend dos2unix.
Hints:
While this program is fairly
simple, there is a lot you have to learn before you can finish it: C++,
UNIX/Linux, an editor, and how to test and turn in the assignment.
The earlier you start this assignment and the more proficient you
become in these tools, the easier the next assignment will be.
Since you know that you will only
be given digits 0-9, you can use an array (of size 10) to store the
counts. Initialize all the elements of this array to 0.
Each time you read a digit add one to the corresponding element
of the array:
// assume that digit is an integer, add 1 to the digit element of the array
counts[digit]++;
Read the input using the << operator in a while loop:
int digit;
while (cin >> digit)
{
if (digit == -1)
break;
// do something with the digit here
}
You will actually be drawing a rectangular grid with a width
equal
to the number of bars (10) and a height equal to the height
of the tallest bar. At each spot in the grid you must draw a
space or
an asterisk.
Since you need to know the height of the tallest bar before
you start drawing the bars, in my solution there is a function that
finds the largest integer in an array of counts. It looks like
this:
// return the largest value in the given array of 10 integers
int find_max(int values[])
{
int max = 0;
// code to actually find the max goes here
return max;
}
In C/C++ you can pass an array
without specifying the size of the array. In a couple of weeks I
will cover in lecture why this works.
Advice:
If you don't finish this assignment it is very unlikely you will pass
the class. I strongly urge you to take this assignment seriously
and start early. If you finish early you don't have to worry
about it.
It is very important to you learn how to use my tests to make sure your
program is correct. You need to learn how to copy my tests and
how to use the diff utility. If you don't learn it for this
assignment, the next assignment will be harder.
If you don't finish in time, turn in what you have. If you turn
nothing in I will give you a zero. If you turn in something I
will give you partial credit. Partial credit is always better
than 0.
General Requirements:
I will deduct 10
percent
if your program does not compile using the command "g++ -o chart
chart.cpp" on a Linux machine. Thus you must make sure to use
the filenames
listed below
(do not
capitalize your filenames), and if you are working on windows,
you
should compile and test your program on one of the department's Linux
workstations before turning it in (you can log on to these machines
from home using
the putty
program).
I will grade your program using another program, so if your program
does
not work exactly as specified below you will lose points. For
example, if you put an extra space at the end of the line, or a blank
line at the end of the output, you will lose points. Test your
program using the sample tests in the test directory (see above).
The first lines of chart.cpp must contain:
// last name, first name
// ecst_username@ecst.csuchico.edu
// 112 Program 1: Chart
How to
turn in code:
You must turn in the following file:
chart.cpp
copy the above file into the directory:
/user/projects/csci112/p1/USERNAME
where USERNAME
is your ecst username.
Notes: This directory
won't exist until the second week of
classes. You will not be able to access this directory after the
deadline
has passed.
See lab2 for instructions
on how to turn in the assignment if you did it on your home machine.
Late
assignments:
If you do not finish by the deadline (see top for the
deadline),
you may turn your assignment in up to 24 hours late. You will
lose
15% for turning your assignment in 1-24 hours late.
If you turn your assignment in late, copy your assignment into the
following
directory:
/user/projects/csci112/p1_late/USERNAME
Assignments will not be accepted if more than 24 hours late.