CS202 Computer Science II
Assignment 3
Due October 12th before 9:00 a.m. in class
Last modified 9/28/98
A
Makefile and sample
Test Cases for
program verification and turnin intructions
This assignment will get you familiar with command line arguments, sorting,
structs, and reinforce the idea of separating your code into many
files both for modularity and for maintainability.
Design a Bubble Sort Program (sorting in ascending order) that takes
upto two command line arguments and read, sorts, and prints student
records. A student record is defined by the following structure in
the file type.h
struct Student {
int ssn;
char name[STRINGLENGTH];
float gpa;
};
STRINGLENGTH along with other definitions is in the file
const.h.
You can expect the input to be in the form
ssn name gpa
and the file input gives you sample input for 10
students.
The program can be called in one of three (3) ways:
1. srter
2. srter m
3. srter m -[s/n/g]
An optional command line argument does NOT have to
be present on the command line. If an optional argument is not present
on the command line the program uses a default value.
- m, the first OPTIONAL argument identifies the size of the input. That is, it defines the number of student records to read, sort
(using bubble sort), and print. For example:
- srter 6 means to sort 6 records.
- The default value of m is 10 records.
- srter all by itself means to sort 10 records.
- The second OPTIONAL argument specifies the sort field
by which to sort the student records. The default is to to sort by the
gpa field.
- srter 7 -s would mean to sort by the ssn field.
- srter 3 -n would mean to sort by the name field.
- srter 25 -g would mean to sort by the gpa field
- srter 25 also means to sort by the gpa field
since no sort field is specified and we must therefore use the default.
As you can see, you should be able to run your program in a variety of ways
from the command line.
Here are some constraints on the assignment:
-
You should put your struct declaration(s) in the file
type.h and your program in
as3.cpp
- You may assume that if there is one command line argument it must be the
number of records to sort and you sort by gpa. If there are two
command line arguments then the first determines the number of records to
sort and the second defines the sort field.
- You may use the string library function strcmp() to compare
two strings. Follow the link to read the manual page and learn how to use
strcmp().
- NO global variables.
- You must use a one-dimensional array of the structs
defined in type.h
- You may assume that you will never have to deal with more than 75 records
at a time (CS202's enrollment cap is actually 60).
- You must define and use a swap() function that passes structures
by reference to swap items in your array of structs.
- Your main program as3.cpp looks
like:
#include "const.h"
#include "type.h"
.
.
.
int main(int argc, char * argv[])
{
...declarations...
...
ProcessCommandLine(......);
ReadInput(.......);
BubbleSort(.......);
PrintOutput(.......);
return 0;
}
I expect you to define and use at least the four functions listed
above (as well as swap).
- I also expect (the Makefile also expects this):
- the function ProcessCommandLine() to be in a
file named command.cpp.
- ReadInput() and PrintOutput() to be in a file
named io.cpp
- BubbleSort() to be in the file bubblesort.cpp
- all #define's to be in
const.h and your struct
definition will be in type.h.
Any deviations will cost you points.
Good Luck. Start on the assignment now.
Examples
Using the file input as an input file to the
program, here are some examples.
- Use defaults -- sort 10 records in ascending order using the gpa
field
% srter < input
1212 bab 1
3423 gag 2.45
4321 bibi 2.76
5555 ggg 2.87
3294 onds 2.93
8888 bbb 3.12
1111 aaa 3.45
1234 didi 3.76
923 jfid 3.82
2222 abb 4
- Sort 6 records with the default sort field.
% srter 6 < input
1212 bab 1
3423 gag 2.45
5555 ggg 2.87
8888 bbb 3.12
1111 aaa 3.45
2222 abb 4
- Sort 8 records using the name field
% srter 8 -n < input
1111 aaa 3.45
2222 abb 4
1212 bab 1
8888 bbb 3.12
4321 bibi 2.76
1234 didi 3.76
3423 gag 2.45
5555 ggg 2.87
- Sort 9 records using the ssn field
% srter 9 -s < input
923 jfid 3.82
1111 aaa 3.45
1212 bab 1
1234 didi 3.76
2222 abb 4
3423 gag 2.45
4321 bibi 2.76
5555 ggg 2.87
8888 bbb 3.12
Please follow these instructions for
turning in assignments.