CSCI 112: Programming and Algorithms II
Program 1: Chart
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 a set of integers from standard input and draws a
bar chart to standard output using aterisks and spaces ('*' and ' ').
The user specifies a set of up to 100 integers greater than 0.
When the user enters a 0, the program will stop reading standard input
and print the chart.
For example, if the user entered, 1 2 3 4 3 2 1 10 0, your program must
print:
*
*
*
*
*
*
* *
*** *
***** *
********
Notes: there are no spaces before the column of the first
bar, and there are no spaces after the column of the last bar.
Program Requirements:
Read up to 100 positive integers (greater than 0) from standard input. You
must
store these integers in a single array.
After all these numbers are read (that is, after the user enters a
0),
draw the corresponding bar chart.
For example consider the following input/output (I use "$" to represent
the UNIX command prompt):
$ chart
1 4 2 3 0
*
* *
* * *
* * * *
$
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><4><space><2><space><3><space><0><return>
Note: text between a "<"
and a ">" is text that the user types. Sometimes it is a
single key, sometimes it is a string. "<" and ">" are often used to deliniate characters a user types.
Then the program printed the asterisks and spaces.
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 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 .profile and adding the following
line. If you don't have a .profile file, create a new one.
Add the following line to your .profile.
export PATH=.:$PATH
Testing
Your Program:
I have posted some sample tests in the tests/p1 directory.
The testing instructions contain instructions for testing your program using my sample tests.
I will test your program with additional tests not posted in the test
directory. It is a very good idea to design and implement your
own set of tests.
Hints:
Since you will not be
given more than 100 integers, create an array of 100 integers. It
is good programming practice to place the "100" in a single place so that
it is easy to change:
const int MAX = 100;
int values[MAX];
...
for (int i = 0; i < MAX; i++)
{
...
}
the above is better than
int values[100];
...
for (int i = 0; i < 100; i++)
{
...
}
You will actually be drawing a rectangular grid with a width
equal
to the number of bars (values entered) 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 integers. It looks like
this:
// return the largest value in the given array of integers
int find_max(int values[], int size)
{
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.
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 (such as jaguar.ecst.csuchico.edu). 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 jaguar before turning it in (you can log on to jaguar
from home using
the putty
program (see software installation instructions)).
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 all your files (.h and .cpp) must
contain:
// last name, first name
// ecst_username
// 112 Program 1: Chart
How to
turn in code:
You must turn in the following file:
chart.cpp
See the turn in instructions for details on how to turn in this assignment.
The turn in instructions contain details on turning in a late assignment.