I need to change this code into object oriented code. here's how to objects are supposed to be setup. First you will need to make a Timer object. This object should function like a traditional stopwatch with methods for starting, stopping, resetting, and reporting back times. The design of the object itself is up to you (it should minimally contain methods for the aforementioned ideas), but it must consist of a solitary object that provides interfaces appropriate for being compositionally included as part of a sorting object to facilitate the time keeping portion of this exercise. Make sure you have a properly separated specification and implementation file for your Timer/Stopwatch object. The second object you will make should be a data housing object that has methods that enable client code to read in the formatted contents of data files, house the data in memory, and execute bubble sort, selection sort, and insertion sort algorithms in a timed fashion with the assistance of your Timer object. You will need to include interfaces/methods for providing file names, reporting erroneously formatted data files, reporting files sizes, initiating individual sort algorithms, and reporting back unbiased times to the client code. Here is the code: #include #include #include #include #include #include using namespace std; long length = 500; const long max_length = 100000; long list[max_length]; int i = 0; #define INFILE "testing.txt" //defining name of the input file void read(); void bubbleSort(); void insertionSort(); void selectionSort(); int main()//running sorts and outputting results { double t1, t2; //time values for start and finish of sort for (length = 500; length <= max_length;) { cout << endl << "Length: : " << length << endl; cout << "Sorting..." << endl; read(); //calling in values to be sorted cout << "Sort Complete!!" << endl;; t1 = clock(); //start time bubbleSort(); // sort t2 = clock(); //end time cout << "Bubble Sort : " << (t2 - t1)/CLK_TCK << " sec" << endl; // outputting the results of the bubble sort read(); //calling in values to be sorted t1 = clock();//start time insertionSort();// sort t2 = clock();//end time cout << "Insertion Sort : " << (t2 - t1) / CLK_TCK << " sec" << endl;// outputting the results of the insertion sort read();//calling in values to be sorted t1 = clock();//start time selectionSort();// sort t2 = clock();//end time cout << "Selection Sort : " << (t2 - t1) / CLK_TCK << " sec" << endl;// outputting the results of the selection sort switch (length) { // changing the length of the sort case 500: length = 1000; break; case 1000: length = 5000; break; case 5000: length = 10000; break; case 10000: length = 25000; break; case 25000: length = 50000; break; case 50000: length = 100000; break; case 100000: length = 100001; break; } } return 0; } void read() //bringing in txt file with values { for (i = 0; i < length; i++) { list[i] = 0; } ifstream inputHandle(INFILE, ios::in); if (inputHandle.is_open() == true) { i = 0; while (i < length) { inputHandle >> list[i]; i++; } inputHandle.close(); } else { cout << "ERROR: The file \"" << INFILE << "\" could not be opened for reading..." << endl; } } void bubbleSort() //bubble sort function { int temp; for (long i = 0; i < length; i++) { for (long j = 0; j < length - i - 1; j++) { if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } } void insertionSort() //insertion sort function { int currentValue; for (int i = 1; i < length; i++) { int j = i - 1; currentValue = list[i]; while (list[j] > currentValue && j >= 0) { list[j + 1] = list[j]; j--; } list[j + 1] = currentValue; } } void selectionSort() //selection sort function { for (int i = 0; i < length - 1; i++) { int currentMin = i; for (int j = i + 1; j < length; j++) { if (list[j] < list[currentMin]) currentMin = j; } int tempNum = list[i]; list[i] = list[currentMin]; list[currentMin] = tempNum; } }
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
I need to change this code into object oriented code. here's how to objects are supposed to be setup.
First you will need to make a Timer object. This object should function like a traditional stopwatch with methods for starting, stopping, resetting, and reporting back times. The design of the object itself is up to you (it should minimally contain methods for the aforementioned ideas), but it must consist of a solitary object that provides interfaces appropriate for being compositionally included as part of a sorting object to facilitate the time keeping portion of this exercise. Make sure you have a properly separated specification and implementation file for your Timer/Stopwatch object.
The second object you will make should be a data housing object that has methods that enable client code to read in the formatted contents of data files, house the data in memory, and execute bubble sort, selection sort, and insertion sort
Here is the code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
using namespace std;
long length = 500;
const long max_length = 100000;
long list[max_length];
int i = 0;
#define INFILE "testing.txt" //defining name of the input file
void read();
void bubbleSort();
void insertionSort();
void selectionSort();
int main()//running sorts and outputting results
{
double t1, t2; //time values for start and finish of sort
for (length = 500; length <= max_length;)
{
cout << endl << "Length: : " << length << endl;
cout << "Sorting..." << endl;
read(); //calling in values to be sorted
cout << "Sort Complete!!" << endl;;
t1 = clock(); //start time
bubbleSort(); // sort
t2 = clock(); //end time
cout << "Bubble Sort : " << (t2 - t1)/CLK_TCK << " sec" << endl; // outputting the results of the bubble sort
read(); //calling in values to be sorted
t1 = clock();//start time
insertionSort();// sort
t2 = clock();//end time
cout << "Insertion Sort : " << (t2 - t1) / CLK_TCK << " sec" << endl;// outputting the results of the insertion sort
read();//calling in values to be sorted
t1 = clock();//start time
selectionSort();// sort
t2 = clock();//end time
cout << "Selection Sort : " << (t2 - t1) / CLK_TCK << " sec" << endl;// outputting the results of the selection sort
switch (length) { // changing the length of the sort
case 500:
length = 1000;
break;
case 1000:
length = 5000;
break;
case 5000:
length = 10000;
break;
case 10000:
length = 25000;
break;
case 25000:
length = 50000;
break;
case 50000:
length = 100000;
break;
case 100000:
length = 100001;
break;
}
}
return 0;
}
void read() //bringing in txt file with values
{
for (i = 0; i < length; i++) {
list[i] = 0;
}
ifstream inputHandle(INFILE, ios::in);
if (inputHandle.is_open() == true)
{
i = 0;
while (i < length)
{
inputHandle >> list[i];
i++;
}
inputHandle.close();
}
else {
cout << "ERROR: The file \"" << INFILE << "\" could not be opened for reading..." << endl;
}
}
void bubbleSort() //bubble sort function
{
int temp;
for (long i = 0; i < length; i++)
{
for (long j = 0; j < length - i - 1; j++)
{
if (list[j] > list[j + 1])
{
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}
void insertionSort() //insertion sort function
{
int currentValue;
for (int i = 1; i < length; i++) {
int j = i - 1;
currentValue = list[i];
while (list[j] > currentValue && j >= 0) {
list[j + 1] = list[j];
j--;
}
list[j + 1] = currentValue;
}
}
void selectionSort() //selection sort function
{
for (int i = 0; i < length - 1; i++) {
int currentMin = i;
for (int j = i + 1; j < length; j++) {
if (list[j] < list[currentMin])
currentMin = j;
}
int tempNum = list[i];
list[i] = list[currentMin];
list[currentMin] = tempNum;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 7 images