1.124J Foundations of Software Engineering

Problem Set 4

Due Date: Tuesday 10/17/00

Reference Readings:
From C++ Primer
(in addition to the reference reading from PS1, PS2 & PS3):

From algorithms in C++

General Instructions and Turn in Requirements

Please, submit printouts of all the source code files, and the output results from the execution of the completed program using the provided input data. Turn in the source code files electronically, too, as described below.

 You can print a (source code) file using the command:

% enscript   -2Gr   -h   -P<printer name>   <filename>

 and you can dump an X window directly to a printer using the command:

 % xdpr  -P<printer name>      (and clicking on the window you want to print)

Please comment your code when you think it would be helpful for the grading and don't make any other changes to the provided code, except for those that you are asked to make.

For this problem set there are the follwoing files which you can chekc out using CVS:
    touristIncome.data visitorsNumber.data for problem 1
    point.h  point.C  data4_2 for problem 2                           
You need to submit all files required, including a makefile, to compile your program.

To check out the provided files with CVS, you can use either of the following two commands:

 %  cvs co Problems/PS2
% cvs co OOP_PS2
Electronic Turnin
You must turnin electronically all the source code files, makefiles and data files that you have used. Please, electronically turnin all files that are necessary to compile your code and execute the program with the provided data. To electronically turnin a file use the following command:
%   turnin   -c 1.124   problem_set_number   filename
(e.g. to submit the ps4_1.C file of PS4:     %  turnin   -c  1.124   4  ps4_1.C )

Please do not turnin any executable or/and object files electronically. Problems set source code files must be turned in electronically before class starts (i.e. at 2:30 p.m.) on the due date of the problem set. Any files turned in later than that time will be considered late and will be penalized.


Problem 1:[40%]

   In this problem you need to develop a program that can handle user-provided data. You need to write the main() function and two template functions. The program should be able to read using the one template function monthly data for a number of years. It should be used twice, once to read the number of visitors, let's say to a tourist resort, and the other the income in millions of dollars.

    The input data are provided in the following files, in tabular form with each row representing the year and for each row having 12 columns, one for each months

e.g. the  touristIncome.data file looks as follows:

474.33 129.72 232.33 239.43 234.56 244.60 286.72 362.46 283.43 245.26 253.40  98.07
240.34 268.36 245.36 165.36 234.45 242.43 344.25 320.20 447.93 375.26 201.30  94.34

   The first element represents the tourist income for the first month of the corresponding year, which in this case assume that it is 1997. The second row has the 12 values for the income for the 1 months of year 1998, and so on.

  You are asked to implement the two template functions in the file ps4_1.h, and the main() function in the file ps4_1.C.
 

main(): The main() function should:

readData() template function: This template function should:

 writeInvertedData() template function: This template function should:

Sample execution of the program:

The execution of your program should have an output similar to the following.
The bolded and italic identify whatever is entered by the user of your program.

 Visitors Statistics

  First year: 1997
  File with data: visitorsNumber.data
  Data for 3 years have been read

  File to store data: visitors.out
  Data for 3 years have been stored to file: visitors.out
 

 Income Statistics

  First year: 1997
  File with data: touristIncome.data
  Data for 2 years have been read

  File to store data: touristIncome.out
  Data for 2 years have been stored to file: touristIncome.out
 

The contents of the input and output files, after the above execution, are presented below:
 

Input files:


visitorsNumber.data:
475544 1985572 2076432 2165239 2283546 2344460 2635040  2958672 3047626 2843543 2734526 2523400 2412307 878734
2343240 2546836 2436456 1943656 2453425 2754240 3256404 3454645 3698200 4138479 3743756 3398320 983474   923498
2845672 3456566 2341653 2734562 2534400 2346307  943734 2234544 2398636 2345346 1546256 2453435 2345240 3456504
 

touristIncome.data
474.33 129.72 232.33 239.43 234.56 244.60 286.72 362.46 283.43 245.26 253.40  98.07
240.34 268.36 245.36 165.36 234.45 242.43 344.25 320.20 447.93 375.26 201.30  94.34
 

Output files:

visitors.out:
Month     1997     1998     1999
  1      475544  2412307  3743756
  2     1985572   878734  3398320
  3     2076432  2343240   983474
  4     2165239  2546836   923498
  5     2283546  2436456  2845672
  6     2344460  1943656  3456566
  7     2635040  2453425  2341653
  8     2958672  2754240  2734562
  9     3047626  3256404  2534400
  10    2843543  3454645  2346307
  11    2734526  3698200   943734
  12    2523400  4138479  2234544
 

touristIncome.out:
Month     1997     1998
  1     474.330  240.340
  2     129.720  268.360
  3     232.330  245.360
  4     239.430  165.360
  5     234.560  234.450
  6     244.600  242.430
  7     286.720  344.250
  8     362.460  320.200
  9     283.430  447.930
  10    245.260  375.260
  11    253.400  201.300
  12     98.070   94.340


 

Problem 2:[60%]

     In this problem you need to write a program that will read a number of points store them using an array of pointers to objects of the class Point, and print the points out, as they are. It should then call a function to sort them using the X-coordinate and print them out sorted. Then, it should invoke the same function to sort them using the Y-coordinate and print them out sorted.

  Two files, in which the class Point is defined, are provided for you. The one is a header file named point.h, and the other is named point.C. These files are presented below. You should NOT make any changes to these files. However, when submitting electronically your code you need to submit these files as well, as that will be helpful during grading in order to compile and run your code.

// Problem Set#4: [point.h]

#ifndef POINT_4_H
#define POINT_4_H
 

class Point
{
private:
  double x,y;

public:
  Point(double x, double y);

  double getX(void);
  double getY(void);

  friend ostream& operator << (ostream &i, Point &p);
};

#endif

// Problem Set#4:   [point.C]
#include  <iostream.h>
#include "point.h"
 

Point::Point(double x, double y)
  {
    this -> x = x ;
    this -> y = y ;
  }
 

double Point::getX(void)
{
  return x;
}

double Point::getY(void)
{
  return y;
}

ostream& operator << (ostream &o, Point &p)
{
  o << " (x,y) = (" << p.x << " , " << p.y << ")"  ;
  return o;
}


  The code that you are asked to provide should be written in two files, the ps4_2.h and the ps4_2.C.
In the former you need to provide only the declarations and in the latter the actual definitions of your code.

main()

  The main() function, which should be provided in the file ps4_2.C, should do the following with this certain order:


readPoints()

  This function should read in the data. You can use the provided file dat4_2 and redirection to save some time while debugging your program. You need to read in the number of points and dynamically allocate the pointers to Point, and then use one by one the x and y values and dynamically allocate memory for a Point object and assign accordingly its address to the corresponding element of the array of pointers.

  File dat4_2 has the following structure:

5
  65.34  618.34
-365.1    54.92
 324.2    54.07
  72.5   527.34
-193.2     9.37
printPoints()

  This function should print out the points in a format similarly as to the following:

  Points
 Point 1:  (x,y) = (65.34 , 618.34)
 Point 2:  (x,y) = (-365.1 , 54.92)
 Point 3:  (x,y) = (324.2 , 54.07)
 Point 4:  (x,y) = (72.5 , 527.34)
 Point 5:  (x,y) = (-193.2 , 9.37)
quickSortPoints()

  This function should sort the points, either in X or Y direction. The latter can be specified using a pointer to a Point class member function to point to the getX and getY functions, respectively. You will need to call a partition function, which you also need to provide. Please, name that function partitionPoints().
 

partitionPoints()

  This function should perform the partition that is necessary for quicksort.

releaseMemory()

  Finally, this function should release any dynamically allocated memory.
 

Sample execution:

The output of your program for the following input data set should look as follows:

Number of Points: 5

 x = 65.34
 y = 618.34

 x = -365.1
 y = 54.92

 x = 324.2
 y = 54.07

 x = 72.5
 y = 527.34

 x = -193.2
 y =  9.37
 

                Points
 Point 1:  (x,y) = (65.34 , 618.34)
 Point 2:  (x,y) = (-365.1 , 54.92)
 Point 3:  (x,y) = (324.2 , 54.07)
 Point 4:  (x,y) = (72.5 , 527.34)
 Point 5:  (x,y) = (-193.2 , 9.37)
 

    Points sorted in X-direction
 Point 1:  (x,y) = (-365.1 , 54.92)
 Point 2:  (x,y) = (-193.2 , 9.37)
 Point 3:  (x,y) = (65.34 , 618.34)
 Point 4:  (x,y) = (72.5 , 527.34)
 Point 5:  (x,y) = (324.2 , 54.07)
 

    Points sorted in Y-direction
 Point 1:  (x,y) = (-193.2 , 9.37)
 Point 2:  (x,y) = (324.2 , 54.07)
 Point 3:  (x,y) = (-365.1 , 54.92)
 Point 4:  (x,y) = (72.5 , 527.34)
 Point 5:  (x,y) = (65.34 , 618.34)

Releasing all the dynamically allocated memory and exiting....
 
 


Note:

Please submit both printouts of the source code you have written (preferably using % enscript -2Gr -Pprinter filename)  and (or screen dumps of) the execution output (using %xdpr -Pprinter), with your name and username clearly written on the first page of the stapled submitted problem set. The submitted code must be identical to that electronically turned in (as described above).



© 1.124J Foundations of Software Engineering
Prof. Kevin Amaratunga,1-274, kevina@mit.edu
TA: Petros Komodromos, 1-245, petros@mit.edu
TA: Eric Perkins, 1-245, edp@mit.edu