Write a program that will sort an array of data using the following guidelines

Question:

Write a program that will sort an array of data using the following guidelines – DO NOT USE VECTORS, COLLECTIONS, SETS or any other data structures from your programming language.

C++

Ask the user for the number of elements, not to exceed SORT_MAX_SIZE = 16 (put appropriate input validation)

Ask the user for the type of data they will enter – Dollar or CIS22C_Dollar objects from Lab 1.

Use your Dollar and CIS22C_Dollar classes from Lab 1 as-is without making any changes. If you do make changes to be able to complete this lab, then there will be a small penalty.

Based on the input, create an appropriate array for the data to be entered.

Write a helper function called RecurMergeSort such that: It is a standalone function not part of any class of Lab 1, Takes in the same type of parameters as any standard MergeSort with recursion behavior, i.e. void MergeSort(Dollar arr[], int size) Prints out how the array looks every time a recursive step returns back to its caller You might need to pass in a third parameter which identifies the array to be printed – this is language-dependent.

In your main, allow the user to enter data of their own choosing up to their chosen array size. There is no sample output – you are allowed to provide user interactivity as you see fit but programs will be graded for clarity of interaction.

Then sort the array using your sort function of step 5.

Take screenshots to be uploaded with the project. In your main, make sure that the output is being written to console as well as an output file at the same time.

Do not copy and paste text from your console to create the output file.

Ensure input and any other data validations as needed and provide descriptive prompts with emphasis on usability. Upload your classes from Lab 1 and new code files for Lab 3, the output file and the screenshots in one zip file.

The Dollar and CISDOLLAR class are as follows:

class Dollar // Dollar class
{
private:
int noteValue;
int coinValue;
string currencyName;
public:
Dollar() {}; // default constructor

Dollar(int note, int coin) { // parameterized constructor
noteValue = note;
coinValue = coin;
}
~Dollar() { // copy constructor

};
// setters and getters.
int getNoteValue() {
return noteValue;
}
void setNoteValue(int x) {
noteValue = x;
}

int getCoinValue() {
return coinValue;
}
void setCoinValue(int x) {
coinValue = x;
}

Dollar operator +(Dollar obj) {
Dollar obj2;
obj2.noteValue = noteValue + obj.noteValue;
obj2.coinValue = coinValue + obj.coinValue;
return obj2;
}

Dollar operator -(Dollar obj) {
Dollar obj2;
obj2.noteValue = this->noteValue - obj.noteValue;
obj2.coinValue = this->coinValue - obj.coinValue;
return obj2;
}
bool operator ==(Dollar obj) {
if ((this->noteValue == obj.noteValue) && (this->coinValue == obj.coinValue))
return true;
else
return false;
}
double dollarToCsdollar(int note, int coin) // converter from Dollar to CIS22CDollar
{
double amount = (note + coin * 0.01);
return amount * 1.36;
}
double dollarAmount(int note, int coin) //Total calculator for Dollar
{
cout << "Dollars:" << (note + coin * 0.01);
return (note + coin * 0.01);
}
};
class CIS22CDollar :public Dollar { // CIS22CDollar class, derived from Dollar Class;
private:
int noteValue;
int coinValue;
double conversionFactor;
double convertTo22C()
{
conversionFactor = 1.36;
return conversionFactor;
}
double convertToDollar() {
conversionFactor = 0.74;
return conversionFactor;
}
public:
CIS22CDollar() {}; //default constructor
CIS22CDollar(int note, int coin) { // parameterized constructor
noteValue = note;
coinValue = coin;
};
~CIS22CDollar() {}; // copy constructor
// Setters and getters.
int getNoteValue() {
return noteValue;
}
void setNoteValue(int x) {
noteValue = x;
}

int getCoinValue() {
return coinValue;
}
void setCoinValue(int y) {
coinValue = y;
}
double conversionToDollar(int note, int coin) { // converter to DOLLAR class
double amount = (note + coin * 0.001);
return amount * 0.74;
}
double cisDollarAmount(int note, int coin) { // total number of cents which is used later in code.
(note + coin * 0.01);
}
};

Answers:

// Function to perform merge
void mergeOperation(CIS22CDollar dollar[], int left, int middle, int right)
{
int c, d, k;
int leftPos = middle - left + 1;
int rightPos = right - middle;

// Create temp arrays
CIS22CDollar Left[leftPos];
CIS22CDollar Right[rightPos];

// Copy data to temp arrays Left[] and Right[]
for (c = 0; c < leftPos; c++)
Left[c] = dollar[left + c];
for (d = 0; d < rightPos; d++)
Right[d] = dollar[middle + 1 + d];

// Merge the temp arrays back into dollar, [l..r]
// Initial index of first sub array
c = 0;
// Initial index of second sub array
d = 0;
// Initial index of merged sub array
k = left;

// Loops till either end of left array or right array
while (c < leftPos && d < rightPos)
{
// Checks if left array c index position value is less than or equals to right array d index position value
if (Left[c].getCoinValue() <= Right[d].getCoinValue())
{
// Stores the left array c index position value
dollar[k] = Left[c];
c++;
}// End of if condition
// Otherwise grater value
else
{
// Stores the right array c index position value
dollar[k] = Right[d];
d++;
}// End of else
k++;
}// End of while loop

// Loops till end of left array for rest numbers
while (c < leftPos)
{
// Stores the left array c index position value
dollar[k] = Left[c];
c++;
k++;
}// End of while loop

// Loops till end of right array for rest numbers
while (c < rightPos)
{
// Stores the right array c index position value
dollar[k] = Right[d];
d++;
k++;
}// End of while loop
}// End of function

// main function for merge sort
void mergeSort(CIS22CDollar dollar[], int left, int right)
{
// Checks if parameter left value is less than right value
if (left < right)
{
// Calculates the middle
int middle = left + (right - left) / 2;
// Sort first and second halves
mergeSort(dollar, left, middle);
mergeSort(dollar, middle + 1, right);
mergeOperation(dollar, left, middle, right);
}// End of if condition
}// End of function

int main()
{
int noteValue[] = {110, 550, 85, 77, 69, 70, 56, 10, 245, 310};
int coinValue[] = {10, 200, 115, 99, 33, 410, 11, 525, 618, 400};
CIS22CDollar dollar[MAX];

for(int c = 0; c < MAX; c++)
dollar[c] = CIS22CDollar(noteValue[c], coinValue[c]);

cout<<"\n ***************** Before sorting: ***************** \n";
for(int c = 0; c < MAX; c++)
cout<<setw(20)<<"\n Coin Value: "<<dollar[c].getCoinValue()<<
setw(20)<<" Note Value: "<<dollar[c].getNoteValue()<<endl;

mergeSort(dollar, 0, MAX-1);

cout<<"\n ***************** After sorting: ***************** \n";
for(int c = 0; c < MAX; c++)
cout<<setw(20)<<"\n Coin Value: "<<dollar[c].getCoinValue()<<
setw(20)<<" Note Value: "<<dollar[c].getNoteValue()<<endl;
return 0;
}

Sample Output:

***************** Before sorting: *****************

Coin Value: 10 Note Value: 110

Coin Value: 200 Note Value: 550

Coin Value: 115 Note Value: 85

Coin Value: 99 Note Value: 77

Coin Value: 33 Note Value: 69

Coin Value: 410 Note Value: 70

Coin Value: 11 Note Value: 56

Coin Value: 525 Note Value: 10

Coin Value: 618 Note Value: 245

Coin Value: 400 Note Value: 310

***************** After sorting: *****************

Coin Value: 10 Note Value: 110

Coin Value: 11 Note Value: 56

Coin Value: 33 Note Value: 69

Coin Value: 99 Note Value: 77

Coin Value: 115 Note Value: 85

Coin Value: 200 Note Value: 550

Coin Value: 400 Note Value: 310

Coin Value: 410 Note Value: 70

Coin Value: 525 Note Value: 10

Coin Value: 618 Note Value: 245

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.