Processing math: 100%

Asg 1: List of Points

Objectives

To implement a point_t object, read a file, and add points to a list (std::vector).

Assignment

  1. Using the data_t C++ code example discussed at length in class, write C++ code to define a point_t class (object) that will hold x-, y-, and z-coordinates of a 3D point.
  2. Create two files: point.h that holds the object interface and point.cpp that holds the object implemantation.

Details

  1. Your point_t object should provide the following:

Requirements

  1. Your code should be such that the following main() routine works unaltered:
    int main(int argc, char *argv[])
    {
            point_t                         point;
            std::vector<point_t>            points;
            std::vector<point_t>::iterator  pit;
    
      // read input file consisting of names and id codes adding entities to list
      while(!std::cin.eof()) {
        std::cin >> point;
        if(std::cin.good()) 
          points.push_back(point);
      }
    
      // now play it back
      std::cout << "*** printing list" << std::endl;
    
      for(int i=0; i<(int)points.size(); i++) {
        std::cout << points[i] << std::endl;
      }
    
      // try it again...
      std::cout << "*** printing list again" << std::endl;
    
      for(int i=0; i<(int)points.size(); i++) {
        std::cout << points[i] << std::endl;
      }
    
      std::cout << "*** printing point coords" << std::endl;
    
      // get at x- and y-coords of p_i
      for(int i=0; i<(int)points.size(); i++) {
        std::cout << "p_i[0] = " << points[i][0] << " ";
        std::cout << "p_i[1] = " << points[i][1] << std::endl;
        std::cout << "p_i[2] = " << points[i][2] << std::endl;
      }
    
      std::cout << "*** checking distance function" << std::endl;
    
      // check distance between two points
      std::cout << "|p_0 - p_1| = " << points[0].distance(points[1]) << std::endl;
    
      // clear list via STL clear() function (memory leak if point_t used new!!)
      std::cout << "*** deleting list" << std::endl;
      points.clear();
    
      // nope ... see if we can delete an empty list
      std::cout << "*** deleting list again" << std::endl;
      points.clear();
    
      // prove we survived
      std::cout << "*** done" << std::endl;
    }
    	

Input

  1. The pointdata.txt file used to test the above program is:
    15 14 13
    600 320 140
    104 29 23
    19 78 83
    450 321 33
    	

Output

  1. Expected output with the above input file is:
    *** printing list
    15.00, 14.00, 13.00
    600.00, 320.00, 140.00
    104.00, 29.00, 23.00
    19.00, 78.00, 83.00
    450.00, 321.00, 33.00
    *** printing list again
    15.00, 14.00, 13.00
    600.00, 320.00, 140.00
    104.00, 29.00, 23.00
    19.00, 78.00, 83.00
    450.00, 321.00, 33.00
    *** printing point coords
    p_i[0] = 15.00 p_i[1] = 14.00 p_i[2] = 13.00
    p_i[0] = 600.00 p_i[1] = 320.00 p_i[2] = 140.00
    p_i[0] = 104.00 p_i[1] = 29.00 p_i[2] = 23.00
    p_i[0] = 19.00 p_i[1] = 78.00 p_i[2] = 83.00
    p_i[0] = 450.00 p_i[1] = 321.00 p_i[2] = 33.00
    *** checking distance function
    |p_0 - p_1| = 672.30
    *** deleting list
    *** deleting list again
    *** done
    	

Supplemental

  1. Here is a Makefile that you can use to compile the project:
    .SUFFIXES: .c .o .cpp .cc .cxx .C
    
    UNAME = $(shell uname)
    PLATFORM = $(shell uname -p)
    
    CC = g++
    COPTS = -g -Wall
    
    INCDIR =
    LIBDIR =
    
    ifeq ("$(shell uname)", "Linux")
    LIBS = -lm
    else
    ifeq ("$(shell uname)", "Darwin")
    LIBS = -framework Foundation -lstdc++
    endif
    endif
    
    .c.o:
    	$(CC) -c $(INCDIR) $(COPTS) -o $@ $<
    
    .cpp.o:
    	$(CC) -c $(INCDIR) $(COPTS) -o $@ $<
    
    all : main
    
    OBJECTS = \
    point.o
    
    main : $(OBJECTS) main.o
    	$(CC) -o $@ $(INCDIR) $(COPTS) $(OBJECTS) $@.o $(LIBDIR) $(LIBS)
    
    point.o: point.h point.cpp
    
    clean :
    	rm -f *.o
    	rm -rf main main.app
    	

Turn in

Turn in all of your code, in one tar.gz archive of your asg##/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Brief solution description (e.g., program design, description of algorithm, etc., however appropriate).
    4. Lessons learned, identified interesting features of your program
    5. Any special usage instructions
  2. Makefile
  3. source code (.h headers and .cpp source)
  4. object code (do a make clean before tar)

How to hand in

See handin notes

Grading scheme