Solved

# Sorts and Saves

Posted on 1997-12-14

I'm having a problem sorting values that are read from an external text file which initialize the array. The program can read the values with no problem it's just the swap part of the algorithm doesn't seem to work. I think it has to do with the reassignment of the array variables when the swap function is called. The other problem is with the destructor function that saves the sorted data to disk. When viewing the output file, all I see are scientific notation numbers. The following is the way the input and ouput files should look respectively:

INPUT FILE: 6 7

5 5

4 3

OUTPUT FILE: 4 3

5 5

6 7

***************************CODE********************************

#include <iostream.h>

#include <math.h>

#include <iomanip.h>

#include <fstream.h> //for file streams

#include <stdlib.h> //for exit function

typedef int bool;

const int false = 0;

const int true = (!false);

class Rectangle {

public:

Rectangle(double inLength = 1.0, double inWidth = 1.0);

double perimeter() const;

double area() const;

void SetWidth(double w);

void SetLength(double l);

double GetWidth() const;

double GetLength() const;

double diagonal() const;

bool isSquare() const;

private:

double length;

double width;

};

Rectangle :: Rectangle(double inLength, double inWidth) : length(inLength), width(inWidth){}//Constructor initializer

class recList {

public:

recList();

void sort();

void display();

~recList();

private:

int n;

Rectangle recArray[20];

void swap(Rectangle &rec1, Rectangle &rec2);

};

void main()

{

recList recListObj; //declaration of recListObj is of class recList

recListObj.sort();

recListObj.display();

}

//Functions for class recList/////////////////////////////////////////////////////////////////////////////////////

recList :: recList() //Reads values from input file and assigns them to Rectangle(), SetWidth(), and SetLength()

{

double inLength, inWidth;

ifstream infile("proj5in.txt");

if (!infile)

{

cerr << "ERROR READING FILE!";

exit(1);

}

else

n = 0;

while ((n < 20) && (infile >> inLength >> inWidth)) //Bounds checking of array

{

recArray[n] = Rectangle(inLength, inWidth);

recArray[n].SetWidth(inWidth);

recArray[n].SetLength(inLength);

n ++;

}

}

void recList :: sort() //Sorts values in recArray in ascending order

{

int j, top, largest;

for (top = 0; top < n-1; top++)

{

largest = top;

Rectangle rec1 = recArray[largest].area(), rec2 = recArray[top].area();

for (j = top + 1; j < n; j++)

if (recArray[j].area() < recArray[largest].area())

largest = j;

swap (rec1, rec2);

cout << recArray[top].area() << " " << recArray[largest].area() << endl;

}

}

void recList :: swap(Rectangle &rec1, Rectangle &rec2)

{

Rectangle temp = rec1;

rec1 = rec2;

rec2 = temp;

}

void recList :: display() //Uses sorted array values to control rectangle/square output

{

//Functions that format decimal point two places

cout.setf(ios::fixed);

cout.precision(2);

for (int j = 0; j < n; j++)

{

cout << "Array Element: " << j << endl;

cout << " Length:" << setw(12) << recArray[j].GetLength();

cout << setw(20) << "Width:" << setw(12) << recArray[j].GetWidth() << endl;

cout << " Area:" << setw(12) << recArray[j].area();

cout << setw(20) << "Perimeter:" << setw(12) << recArray[j].perimeter() << endl;

cout << "Diagonal:" << setw(12) << recArray[j].diagonal() << setw(20) << "Type:"

<< setw(12); recArray[j].isSquare();

if (recArray[j].isSquare() == 1)//Uses value returned from isSquare() to indicate rectangle or square

cout << "Square";

else

cout << "Rectangle";

cout << endl << endl;

}

}

recList :: ~recList() //Writes sorted array values to .txt file

{

double inLength, inWidth;

ofstream outfile ("proj5out.txt");

if (!outfile)

{

cerr << "ERROR OPENING FILE!";

exit(1);

}

else

n = 0;

while ((n < 20) && (outfile << inLength << inWidth))

{

recArray[n] = Rectangle(inLength, inWidth);

recArray[n].SetWidth(inWidth);

recArray[n].SetLength(inLength);

n ++;

}

}

//Functions for class rectangle///////////////////////////////////////////////////////////////////////////////////

double Rectangle :: perimeter() const

{

return (2 * width + 2 * length);//Returns perimeter of rectangle or square

}

double Rectangle :: area() const

{

return (length * width);//Returns area of rectangle or square

}

void Rectangle :: SetWidth(double w)

{

width = w;//Assigns w to width

}

void Rectangle :: SetLength(double l)

{

length = l;//Assigns l to length

}

double Rectangle :: GetWidth() const

{

return width;//Returns value of width

}

double Rectangle :: GetLength() const

{

return length;//Returns value of length

}

double Rectangle :: diagonal() const

{

return (sqrt(pow(length, 2) + pow(width, 2)));//Returns diagonal of rectangle or square

}

bool Rectangle :: isSquare() const

{

if (length == width)//Tests length and width variables for rectangle or square attributes

return true;

else

return false;

}

Any help is appreciated.