ECS 110 - Spring 1997

Homework 0

Due 11:59 PM, Friday, April 4, 1997

 

The purpose of assignment 0 is to introduce you to the mechanics of compiling C++ programs while introducing you to a few basic C++ features.

We will give you a file containing vector coordinates. You will redirect the file to standard input, read the vectors, sort them by length and write the sorted list to standard output. If you were to write this program in C, we assume that you would be able to write it in an hour or less. However, you will be required to use certain C++ features in your solution, and that is the challenge of this assignment.

Input File

The input file will contain one or more "runs". Each run will consist of a small positive integer followed by that many pairs of integer coordinates. If the small positive integer is zero then your program should terminate. The file we supply and the file that we use when grading your program will contain correct input and will be terminated by a zero. Do not worry about mathematical overflow in your calculations.

For example, here is a small sample containing two runs:

3
2 4
-4 4
3 3
1
123 12
0

Output File

For each run, your program should output the vectors sorted in ascending order by length. Use any sorting algorithm you would like. The runs should be separated by a blank line. Recall that the length of a vector is a floating point number and can be calculated

Here is the output corresponding to the small sample listed above:

3 3
2 4
-4 4

123 12

We will be grading part of your program mechanically, so it is very important that your output conform to these conventions and look similar to this sample.

C++

This program is trivial. What is not so trivial is that you will be writing it in a language that is unfamiliar to you. Read your C++ reference tonight. You have fewer than four days to do the assignment. Here are the C++ requirements:

  1. Class. You should define a class to represent a vector. In keeping with good C++ style, the data members of the class should be declared private and manipulated externally by public member functions only.
  2. Input and Output. Do not use any of the C routines in stdio.h. Instead use the standard C++ cin and cout input and output streams defined in iostream.h.
  3. Print Function. Write and use a vector class member function for printing each vector to standard output.
  4. Memory Allocation. Each run will potentially involve a different number of vectors. Dynamically allocate and de-allocate an array of your vector class using the C++ memory operators new and delete.
  5. g++. Your program must compile using g++ on the instructional computers.

Hints

If you were sorting an array of integers using Bubblesort it might look something like this:

const int array_length = 100;
int int_array[array_length];
 
for (int i = 0; i < array_length - 1; i++)
  for (int j = array_length - 1; i < j; j--)
    if (int_array[j-1] > int_array[j])
      swap(&int_array[j-1], &int_array[j]);

You may want to compile your program using the -Wall flag on the g++ command line. This turns on all compiler warnings, and heeding them can help you avoid some pitfalls.

C++ standard libraries replace some familiar C libraries. For instance, you will almost never #include <stdio.h> in a C++ program. You will instead #include <iostream.h> which handles input and output differently. The standard C libraries are still useful for things like math and other utility functions. Whether you are accessing a C or C++ library, make sure to include the header file and any necessary libraries (remember the -lm flag on the command line when including math.h).

What To Turn In

We will be using the electronic submission program called handin. Directions on its use can be found at the course web page. The handin directory for this program will be hw0. You must turn in a makefile called makefile which outputs an executable program called vectsort. We will be using make to compile your program, so it is very important that you turn in a functioning makefile. If your program uses any header files that you wrote, then submit them, too.