CSCI 112: Programming and Algorithms II
Fall 2005
Program 1: Chart
Code due: 5pm Wednesday September 7
(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 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 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.

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 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 .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 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:

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.  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 all your files (.h and .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.