CS140 -- Lab 1


The purpose of this lab is to give you practice with several C constructs, including:

  1. C-style strings
  2. structs
  3. printf
  4. malloc and pointers

You will write a program that reads students' names and exams scores from a file and stores each of students in an array. You wil then sort the students into ascending order based on their average exam score.

Helpful Links

Here are some useful links to review from CS102 and CS140:

  1. scanf and printf
  2. pointers
  3. malloc
  4. C-style strings

Where To Find Things

The ~bvz/cs140/www-home/fall-2008/labs/lab1 directory contains the following useful files:

  1. An executable file named exam_sort. If you have any questions about what your output should look like or how your program should behave, try executing this program. If you want to see debugging information that shows how selection sort works, then invoke exam_sort with the -d flag. For example:
    exam_sort -d grades_file

  2. A sample file, named grades_file, that you can use to test your exam_sort program. This file is only a sample and does not exhaust all the cases we might test. Therefore you should also come up with your own test files.

  3. A makefile that you can use to create your executables. If you copy this makefile to your directory you can compile your .c files into executables by typing make exam_sort.

Exam Sort

In this part of the lab you are going to create a file called exam_sort.c that reads lines containing a name and a set of exam scores from a file whose name is given on the command line. Your program will read all the students into an array and then use selection sort to sort them into ascending order with the average exam score as a key. Once the input has been read and sorted your program will print the names in ascending order based on exam score. For example, if the input is:

mary 85  95  100
fred 67  82   53
susan 93 87 100
joe 91  72   85
barthowlomew 84 58 81
then your output should be:
fred             67.33
barthowlomew     74.33
joe              82.67
mary             93.33
susan		 93.33
Note that mary and susan had the same average 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. There will be a maximum of 50 students but there may be fewer than that many students in any given file.
  2. You must use a struct to store each student's information. Each struct should contain a name and an average exam score.
  3. Each input line will consist of a name of no more than 15 characters and three integer exam scores.
  4. Each line of output should contain a left-justified name that occupies 15 spaces, then a space, and finally a right-justified floating point exam average that contains 3 leading digits, a decimal point, and two fractional digits for a total of 6 spaces.
  5. 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 Submit

You should submit a file named exam_sort.c. If you cannot complete the program as described then also include a README file that describes what your program is able to accomplish.