CS 102 Lab1

Due Friday Sept 23rd

Overview:

This is a simple review lab to help you shake the rust off of your programming skills. It is designed to help you review the material from cs101, particularly one dimensional arrays. It is intended to be short and non-threatening to get you used to the eagle server again. Because of this, the requirements are strictly specified.

This lab will also acquaint you with with eagle, our cs SUN Solaris based computer server. All assignments done in this class must be turned in on eagle and run on eagle.


What you must do:

Once you have received your cs 102 account for the semester, you must first set the password. To do that use a telnet program and connect to eagle. You should only telnet this first time. Once your password is set, log out.

Now use ssh to log in. ssh is the “secure shell”. It is much more secure than telnet and will give some nice features that we will use later in the class.

Ssh is available built in on Macs and Linux/Unix computers. For windows users there is a free program called putty that you can download and install to give you ssh capability. (see http://www.chiark.greenend.org.uk/~sgtatham/putty/ for the putty home page.)

Or you can use the CD for windows that I gave out in class.

now log into eagle.

ssh <usename>@eagle.bridgew.edu (where you must replace <username> with your actual username)

Now copy a configuration file. Type (or paste in) the following command

cp /export/spare/faculty/jsantore/.tcshrc-cs102 .tcshrc

If you are typing the line, note that there is a space between -.tcshrc-cs102 and .tcshrc

now type

tcsh

to enter the t shell. I've set up several environment variables and features that you will probably find useful. One feature that most people will notice immediately is that you can now use the arrow key to navigate.

Now create a new folder/directory for your lab one work

mkdir lab1

and then move into that directory/folder

cd lab1

At this point you are ready to begin working on the programming part of the lab.

Your Program

In this lab you must write a program that allows the user to enter the names of several candidates in a local election and the number of votes recieved by each candidate. Your program must then output each candidate's name, along with his/her votes and the percentage of the total votes that are recieved by each candidate. Your program should also print the winner of the election after the data.

Some specifics:

You must have a constant for the maximum number of candidates. You should use the constant throughout your code so that I can change the constant and recompile your program and see the change reflected in your program's behavior.

You might implement the constant as follows

const int MAX_CANDIDATES=5;

Your program should prompt the user to input candidates until the user is done. The program must also allow a user to stop adding candidates and vote totals if the user wishes. The program should not allow more than MAX_CANDIDATES worth of input though.

You must have at least four functions in your program:
  1. Your main function, which cannot be more than 6 lines long at most
  2. A function to get input from the user
  3. a function to calculate the vote percentages
  4. a function to print output to the user.
You may have additional functions to perform other tasks or further break down the tasks above if you like.

You may not have any global variables in your program. Global variables are generally a bad idea, and they are not needed for this lab, you can accomplish everything by using parameters to your functions. You will need to pass some parameters by reference to accomplish this.

As always, make sure that you comment your code reasonably well. Tell the reader what you are doing, not what you are writing. Some of your grade will be assigned to your comments.

Sample output:

Please enter a candidate (enter done to stop)
Thomas
Enter the number of votes Thomas recieved
456
Please enter a candidate (enter done to stop)
Imelda
Enter the number of votes Imelda recieved
5467
Please enter a candidate (enter done to stop)
Leo
Enter the number of votes Leo recieved
3454
Please enter a candidate (enter done to stop)
Maria
Enter the number of votes Maria recieved
986
Please enter a candidate (enter done to stop)
done

Candidate      Votes          % of Total Votes
Thomas         456            4.40027
Imelda         5467           52.755
Leo            3454           33.3301
Maria          986            9.51462


Writing your program

To write your program you will need to use an editor. In the past many students have written their programs using notepad as their editor and then used ftp to move the file to eagle. This was done because the tools available on eagle at the time were rather limited. I suggest you write your code on eagle itself after connecting through ssh using the CD that I gave you in class.

If you have high speed internet - especially from school:

Users who have fast connections should probably use emacs. It is the most full-featured of the editors. To run emacs, type

emacs <filename.cpp> &

where <filename.cpp> is the cpp file you want to edit.You will have an emacs window popup on your screen and you will be able to use menus normally. This editor also "pretty prints" your text using colored text for c++ keywords and constants. It will also indent your text for you properly giving you a better feel for your program.

The '&' character means "run this in the background". In plain english than means "give me my prompt back after you run this program"

If you have slower speed connections - mainly off campus

Use xjed, which will also displays keywords in color and indents text. It doesn't have quite as much functionality and isn't as pretty, but it is faster.

xjed <filename.cpp> &

When you are ready to try compiling what you have written

Pull your emacs or xjed window out of the way and run this command

g++ -g -o Lab1Prog <yourFilename.cpp>

I encourage you to compile often so that you don't end up with so many errors that you feel overwhelmed when you finally do compile.

Submitting:

When your program is ready, you need to write a readme.txt file that provides me the following information:

If you are in your lab1 directory when you read this, move to the parent folder/directory
cd ..
(where the '..' means <parent directory> note that there is a space between cd and ..)
now zip up the entire lab1 directory and send it to me.
zip -r lab1.zip lab1
This means zip up the entire lab1 directory (the -r flag says do everything in the directory not just the directory itself) and put it in a file called lab1.zip

Now prepare to email your lab to me.

type the following command
pine
Pine is a terminal based email program. From eagle, you will only be able to send mail to bridgew.edu email addresses. You will submit your lab by email to me this semester. However you need to configure pine the first time you use it.

Configuring pine

Choose 'c' to compose a mail and send me a mail with the zip file of your lab solution attached.