CS140 -- Lab 1

Brad Vander Zanden


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:

  1. The name of the file to be read should be provided on the command line
  2. There will be a maximum of 100 friends but there may be fewer than that many friends in any given file.
  3. 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.
  4. 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.
  5. The format of your output should be as follows:

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

  1. parrot.c: 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).
  2. firstlast.c: 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.
  3. array.c: 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 firstlast.c. You will need to write the print function. As in parrot.c you should simply echo the input without sorting it.
  4. percent.c: Now add a percent field to your class from array.c 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.
  5. friends.c: Modify percent.c 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.
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:

  1. 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.c. 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
  2. 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.