(C++) I could use some help with this C++ program, I have some bugs in my code t
ID: 3694416 • Letter: #
Question
(C++)
I could use some help with this C++ program, I have some bugs in my code that I can't quite figure out.
Modify the bubble sort function (listed below) so that it sorts an array of Boxes from greatest volume to least volume (using the provided box.hpp and box.cpp code below). It should take two parameters, an array of Boxes, and the size of the array. Your function must be named boxSort.
The files must be named Box.hpp, Box.cpp and boxSort.cpp
----------------------------------------------------------------------------------------------------------------------------------------
bubbleSort example (to be modified):
/************************************************************
* sortArray *
* This function performs an ascending-order bubble sort on *
* array. The parameter size holds the number of elements *
* in the array. *
************************************************************/
void sortArray(int array[], int size)
{
int temp;
bool swap;
do
{ swap = false;
for (int count = O; count < (size - 1); count++)
{
if (array[count] > array[count + l])
{
temp = array[count];
array[count] = array[count + l];
array[count + l] = temp;
swap = true;
}
}
} while (swap); II Loop again if a swap occurred on this pass.
}
--------------------------------------------------------------------------------------------------------------------------------------
Box.hpp:
class Box
{
private:
double height, width, length;
public:
Box(double h, double w, double l);
Box();
void setHeight(double h);
void setWidth(double w);
void setLength(double l);
double getVolume();
double getSurfaceArea();
};
__________________________________________________
Box.cpp:
#include "Box.hpp"
Box::Box()
{
setHeight(1);
setWidth(1);
setLength(1);
}
Box::Box(double h, double w, double l)
{
setHeight(h);
setWidth(w);
setLength(l);
}
void Box::setHeight(double h)
{
height = h;
}
void Box::setWidth(double w)
{
width = w;
}
void Box::setLength(double l)
{
length = l;
}
double Box::getVolume()
{
return length * width * height;
}
double Box::getSurfaceArea()
{
return 2*width*length + 2*width*height + 2*height*length;
}
Explanation / Answer
main.cpp
#include <iostream>
#include "Box.hpp"
void boxSort(Box array[], int size);
int main()
{
const int NUM_BOXES= 5;
Box array[NUM_BOXES];
array[0].setLength(25);
array[0].setWidth(25);
array[0].setHeight(25);
array[1].setLength(5.5);
array[1].setWidth(5.5);
array[1].setHeight(5);
array[2].setLength(18.5);
array[2].setWidth(15);
array[2].setHeight(15);
array[3].setLength(250);
array[3].setWidth(250);
array[3].setHeight(250);
array[4].setLength(205);
array[4].setWidth(205);
array[4].setHeight(205);
boxSort(array, NUM_BOXES);
for (int i = 0; i < NUM_BOXES; i++)
{
std::cout << array[i].getVolume() << std::endl;
}
return 0;
}
boxSort.cpp
#include <iostream>
#include "Box.hpp"
void boxSort(Box array[], int size);
int main()
{
const int NUM_BOXES= 5;
Box array[NUM_BOXES];
array[0].setLength(25);
array[0].setWidth(25);
array[0].setHeight(25);
array[1].setLength(5.5);
array[1].setWidth(5.5);
array[1].setHeight(5);
array[2].setLength(18.5);
array[2].setWidth(15);
array[2].setHeight(15);
array[3].setLength(250);
array[3].setWidth(250);
array[3].setHeight(250);
array[4].setLength(205);
array[4].setWidth(205);
array[4].setHeight(205);
boxSort(array, NUM_BOXES);
for (int i = 0; i < NUM_BOXES; i++)
{
std::cout << array[i].getVolume() << std::endl;
}
return 0;
}
Box.cpp
#include "Box.hpp"
/*********************************************************************
** Description: The default Box constructor.
** Sets the width, height and length to 1
*********************************************************************/
Box::Box()
{
setHeight(1);
setWidth(1);
setLength(1); // Calls set methods for all 3 private member variables, to initialize to 1
}
/*********************************************************************
** Description: A Box constructor called with 3 arguments. Sets the
** width, height, and length to the appropriate values.
*********************************************************************/
Box::Box(double heightIn, double widthIn, double lengthIn)
{
setHeight(heightIn);
setWidth(widthIn);
setLength(lengthIn); // Calls the set methods for all 3 private member variables to initialize them
}
void Box::setHeight(double heightIn)
{
height = heightIn;
}
void Box::setWidth(double widthIn)
{
width = widthIn;
}
void Box::setLength(double lengthIn)
{
length=lengthIn;
}
/*********************************************************************
** Description: Calculates the volume of the Box.
*********************************************************************/
double Box::getVolume()
{
return width*length*height; // Calculating and returning the volume of a box
}
/*********************************************************************
** Description: Calculates the total surface area of the box
*********************************************************************/
double Box::getSurfaceArea()
{
return 2*width*length + 2*width*height + 2*length*height; // Calculating and returning the total surface area.
}
Box.hpp
#ifndef BOX_HPP
#define BOX_HPP
class Box
{
private:
double height,
width,
length; // Declaring private member variables to hold width, length and height of box
public:
Box();
Box(double, double, double);
void setHeight(double);
void setWidth(double);
void setLength(double);
double getVolume();
double getSurfaceArea(); // Declaring public methods for Box class, and Box class constructors.
};
#endif
sample output
1.5625e+07
8.61512e+06
15625
4162.5
151.25
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.