Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(All in C++) How would I sort a vector of objects (each object contains three si

ID: 644829 • Letter: #

Question

(All in C++)

How would I sort a vector of objects (each object contains three sides of a triangel) based on their areas without another vector using another vector with areas? I have a selection sort function coded as well as a get area function. I am just not sure how to feed the vector of objects into the selection sort function I have.

Here is my code as requested. I am looking to plug vecRect and vecTriangle into the function selSort

#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;

class Shape{
public:
   virtual double GetArea() = 0;
   virtual bool CheckValidity() = 0;
};

class Rectangle: public Shape{

public:
   Rectangle(double a, double b);
   double getA() const { return a; }
   double getB() const { return b; }
   void setA(double val){ a = val; }
   void setB(double val){ b = val; }
   bool CheckValidity(){
       if (a > 0 && b > 0){
           return true;
       }
       return false;
   }
   double GetArea(){
       return a * b;
   }
private:
   double a, b;
};

class Triangle : public Shape {
private:
   double a, b, c;
public:
   Triangle(double a, double b, double c);
   double getA() const { return a; }
   double getB() const { return b; }
   double getC() const { return c; }

   void setA(double val){ a = val; }
   void setB(double val){ b = val; }
   void setC(double val){ c = val; }
   bool CheckValidity(){
       if (a > 0 && b > 0 && c > 0){
           if (a > b && a > c){
               return a < (b + c);
           }
           else if (b > a && b > c){
               return b < (a + c);
           }
           else if (c > a && c > b){
               return c < (a + b);
           }
       }
       return false;
   }
   double GetArea(){
       double p = (a + b + c) / 2;
       return sqrt(p * (p - a) * (p - b) * (p - c));
   }
};

Rectangle::Rectangle(double a, double b)
   {
       setA(a);
       setB(b);
   }
Triangle::Triangle(double a, double b, double c){
   setA(a);
   setB(b);
   setC(c);
}

void selSort(vector<double> ta){
   int i = 0;
   int j = 0;
   int indexSmallest = 0;
   int temp = 0;
   for (i = 0; i < ta.size(); ++i){
           indexSmallest = i;
           for (j = i + 1; i < ta.size(); ++j){
               if (ta.at(j) < ta.at(indexSmallest)){
                   indexSmallest = j;
               }
           }
           temp = ta.at(i);
           ta.at(i) = ta.at(indexSmallest);
           ta.at(indexSmallest) = temp;
       }
   return;
}


int main(){
   vector<Rectangle> vecRect;
   vector<Triangle> vecTriangle;

   ifstream input;
   input.open("Tsides.txt");
   double a, b, c;
   if (input.is_open()){
       while (input >> a){
           input >> b >> c;
           Triangle temp = Triangle(a, b, c);
           if (temp.CheckValidity()){
               vecTriangle.push_back(temp);
           }
       }
   }
   input.close();
   input.open("Rsides.txt");
   if (input.is_open()){
       while (input >> a){
           input >> b;
           Rectangle temp = Rectangle(a, b);
           if (temp.CheckValidity()){
               vecRect.push_back(temp);
           }
       }
   }
  
  

   ofstream output;
   output.open("Rareas.txt");
   for (int i = 0; i < vecRect.size(); ++i){
       output << vecRect[i].GetArea() << endl;
   }
   output.close();
   return 0;
   input.close();
  
   output.open("Tareas.txt");
   for (int i = 0; i < vecTriangle.size(); ++i){
       output << vecTriangle[i].GetArea() << endl;
   }
   output.close();
  
}

Explanation / Answer

#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
class Shape{
public:
   virtual double GetArea() = 0;
   virtual bool CheckValidity() = 0;
};
class Rectangle: public Shape{
public:
   Rectangle(double a, double b);
   double getA() const { return a; }
   double getB() const { return b; }
   void setA(double val){ a = val; }
   void setB(double val){ b = val; }
   bool CheckValidity(){
       if (a > 0 && b > 0){
           return true;
       }
       return false;
   }
   double GetArea(){
       return a * b;
   }
private:
   double a, b;
};
class Triangle : public Shape {
private:
   double a, b, c;
public:
   Triangle(double a, double b, double c);
   double getA() const { return a; }
   double getB() const { return b; }
   double getC() const { return c; }
   void setA(double val){ a = val; }
   void setB(double val){ b = val; }
   void setC(double val){ c = val; }
   bool CheckValidity(){
       if (a > 0 && b > 0 && c > 0){
           if (a > b && a > c){
               return a < (b + c);
           }
           else if (b > a && b > c){
               return b < (a + c);
           }
           else if (c > a && c > b){
               return c < (a + b);
           }
       }
       return false;
   }
   double GetArea(){
       double p = (a + b + c) / 2;
       return sqrt(p * (p - a) * (p - b) * (p - c));
   }
};
Rectangle::Rectangle(double a, double b)
{
   setA(a);
   setB(b);
}
Triangle::Triangle(double a, double b, double c){
   setA(a);
   setB(b);
   setC(c);
}
template<typename T> // Template function that takes both Triangle and Rectangle to sort.
void selSort(vector<T>& ta){
   int i = 0;
   int j = 0;
   int indexSmallest = 0;
   for (i = 0; i < ta.size(); ++i){
           indexSmallest = i;
           for (j = i + 1; j < ta.size(); ++j){
               if (ta.at(j).GetArea() < ta.at(indexSmallest).GetArea()){
                   indexSmallest = j;
               }
           }
           T temp = ta.at(i);
           ta.at(i) = ta.at(indexSmallest);
           ta.at(indexSmallest) = temp;
       }
   return;
}

int main(){
   vector<Rectangle> vecRect;
   vector<Triangle> vecTriangle;
   ifstream input;
   input.open("Tsides.txt");
   double a, b, c;
   if (input.is_open()){
       while (input >> a){
           input >> b >> c;
           Triangle temp = Triangle(a, b, c);
           if (temp.CheckValidity()){
               vecTriangle.push_back(temp);
           }
       }
   }
   input.close();
   input.open("Rsides.txt");
   if (input.is_open()){
       while (input >> a){
           input >> b;
           Rectangle temp = Rectangle(a, b);
           if (temp.CheckValidity()){
               vecRect.push_back(temp);
           }
       }
   }
   selSort<Rectangle> (vecRect); // Sort Rectangle by area
   selSort<Triangle> (vecTriangle); // Sort Triangle by area

    ofstream output;
   output.open("Rareas.txt");
   for (int i = 0; i < vecRect.size(); ++i){
       output << vecRect[i].GetArea() << endl;
   }
   output.close();
   input.close();
   output.open("Tareas.txt");
   for (int i = 0; i < vecTriangle.size(); ++i){
       output <<vecTriangle[i].GetArea() << endl;
   }
   output.close();
}