CS140 -- Lab 1
- Announcements Wed. 8/25 @ 4pm
- Your file extensions should be .cpp, not .c as in the original lab
instructions.
- For array.cpp you should be able to re-use most of your code from
parrot.cpp, not firstlast.cpp, as I said in the original lab
instructions.
- I modified my friends executable so that it correctly right-justifies the
number of answers correct and number of questions answered fields. It
is ok if you do not right-justify these fields in your first four
programs, since the first four programs are simply trying to lead you
up to writing the final program. My first four executables continue
to left justify the number correct and number answered fields, but
you may right justify those fields as well if you wish.
- Submission Instructions
- Tips
- If parrot.cpp prints the last line twice, your code might have
a problem that is discussed in Tips
Purpose
The purpose of this lab is to let you review and knock the rust off your
C++ skills. It also shows you a good program development technique, which
is to incrementally add functionality to a program and test it frequently.
Developing programs in this manner is one of the best ways to minimize
debugging time and hence the overall development time of your program.
How Well Do Your Friends Know You?
Facebook has an application that allows you to select questions about
yourself and then quiz your friends on how well they know you.
Writing the application is beyond the scope of this course. However,
we can pretend to write a small portion of it, namely the part that
ranks your friends on how well they know you.
Let us suppose that the application generates a file with a list of
the names of your friends, the number of questions they answered correctly, and
the number of questions they answered (perhaps you modify the
quiz occasionally and so different friends answer different numbers of
questions). You are going to write a program that reads the friends
from that file
into an array and then use selection
sort to sort them into descending order
with their percentage score being the key.
Once the input has been read and sorted
your program will print the names in decending order based on quiz score, thus
giving you a sorted list of who knows you best.
For example, if the input is:
mary 3 12
fred 8 12
susan 11 15
joe 4 10
barthowlomew 6 15
then your output should be:
susan 11 15 0.73
fred 8 12 0.67
joe 4 10 0.40
barthowlomew 6 15 0.40
mary 3 12 0.25
Note that each line of output has a friend's name, the number of questions
they answered correctly, the number of questions they answered, and the
percentage score.
Also note that joe and barthowlomew had the same percentage score
and that they were printed out in the
same order that they appeared in the original input.
The program has the following specifications and requirements:
- The name of the file to be read should be provided on the command line
- There will be a maximum of 100 friends but there may be fewer than
that many friends in any given file.
- You must use a class to store each friend's information. Each class should
contain fields for the friend's name, number of questions answered,
number of questions answered correctly, and percentage score.
- Each input line will consist of a name, which will be a single word,
and two integers, which represent
the number of questions answered and the number
of questions answered correctly.
- The format of your output should be as follows:
- Names should be left-justified in a field 15 characters wide.
- The number of questions answered correctly and the number of
questions answered should be right-justified in fields that are
3 characters wide.
- The percentage score should be rounded to two decimal
digits. Do not worry about the field size.
- There should be a single space between each field.
- You may assume that the input is correct so your program does not have
to do any error-checking.
If you have not seen selection sort before or need to brush up on it then
try this Wikipedia link.
What to Write and Submit
To both warm you up and give you an idea of how programs should be
incrementally developed, you will write and hand in five programs. When
writing each program, you should be able to start with a copy of the
previous program and simply modify it:
- parrot.cpp: This program will read the input and echo it to the output
without performing any manipulations. You do not even need to use a class
to store the information for this first program, but the output should
be formatted in the manner specified above (it will lack the percentage
score however).
- firstlast.cpp: This program will read the input and print the friend
who is first in alphabetical order and the friend who is last in
alphabetical order. For the above input, the output should be:
barthowlomew 6 15
susan 11 15
For this program you should create a class that allows you to store
friend information. You will then declare class variables for the first
and last friend in alphabetical order. These variables
will save the information
for the current first and last friends. You should discard friends
who are not currently first or last in alphabetical order.
- array.cpp: This program will read the input into an array and then pass
the array to a print function that will print the elements of the
array. You should be able to re-use most of your code from parrot.cpp.
You will need to write the print function. As in parrot.cpp
you should simply echo the input without sorting it.
- percent.cpp: Now add a percent field to your class from array.cpp
and compute each
friend's quiz percentage as you read that friend's line. Modify the
print function to also print the friend's percentage.
- friends.cpp: Modify percent.cpp to implement the selection sort and print
the friends in descending order. You should write a sort function to
sort the friends and pass it the array of friends as input. You should
be able to combine your code from firstlast.cpp and percent.cpp to
create friends.cpp.
Note that each program builds on the previous one and you should be able
to use either all or most of the code from the previous program. This
incremental development of a program is the way that you should write all
programs since it allows you to frequently test your code. In general, if
there is an error, you will know that the error is in the most recent set
of additions, making it easier to locate the error.
Where To Find Things
The ~bvz/cs140/labs/lab1 directory contains the following
useful files:
- Executable files for each of the programs you have been asked to
write. The executables have the same names as the program files. For
example, friends is the executable for friends.cpp. If
have any questions about what your output should look like or how your
program should behave, try executing my program.
If you want to
see debugging information that shows how selection sort works, then
invoke friends with the -d flag. For example:
friends -d friends_file
- empty_file.txt, onename.txt, and
friends_file.txt: These three files contain test data
that you can use to test your programs.
These files are only a sample of possible input
and do not exhaust all the cases we might test. Therefore you should also
come up with your own test files. However, the files do show one important
test principle, which is to use test files with no input, one item of input,
and many items of input. I call it the 0-1-many rule.