question archive Im very confused that there are three different codes, and I only can edit one? I also am confused if I can edit the parts of the code that I can edit that isn't marked with one of these: \
Subject:Computer SciencePrice: Bought3
Im very confused that there are three different codes, and I only can edit one? I also am confused if I can edit the parts of the code that I can edit that isn't marked with one of these: \. I need help with seeing where I went wrong, and how to fix it. Thank you :)
P.S. My personal code is in bold. Im not sure if I can edit the non bold parts
Problem:
9.16 LAB: Triangle area comparison (classes)
Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and set the base and height of triangle1 and of triangle2, determine which triangle's area is larger, and output that triangle's info, making use of Triangle's relevant member functions.
Ex: If the input is:
3.0 4.0 4.0 5.0
where 3.0 is triangle1's base, 4.0 is triangle1's height, 4.0 is triangle2's base, and 5.0 is triangle2's height, the output is:
Triangle with larger area: Base: 4.00 Height: 5.00 Area: 10.00
Main.cpp (The code that I can edit)
Original:
#include <iostream>
#include "Triangle.h"
using namespace std;
int main(int argc, const char* argv[]) {
Triangle triangle1;
Triangle triangle2;
// TODO: Read and set base and height for triangle1 (use SetBase() and SetHeight())
// TODO: Read and set base and height for triangle2 (use SetBase() and SetHeight())
// TODO: Determine larger triangle (use GetArea())
cout << "Triangle with larger area:" << endl;
// TODO: Output larger triangle's info (use PrintInfo())
return 0;
}
My code:
#include <iostream>
#include "Triangle.h"
using namespace std;
int main(int argc, const char* argv[]) {
Triangle triangle1;
Triangle triangle2;
cout<<"Enter the Base of Triangle Onet";
cin>>triangle1.base;
cout<<"Enter the Height of Triangle Onet";
cin>>triangle1.height;
cout<<"Enter the Base of Triangle Twot";
cin>>triangle2.base;
cout<<"Enter the Height of Triangle t";
cin>>triangle2.height;
if(triangle1.area()>triangle2.area())
{
cout << "Triangle with larger area:" << endl;
cout<<"Base : "<<triangle1.base<<endl;
cout<<"Height : "<<triangle1.height<<endl;
cout<<"Area :"<<triangle1.area();
}
else
{
cout<<"Triangle 2 with larger area"<<endl;
cout<<"Base:"<<triangle2.base<<endl;
cout<<"Height:"<<triangle2.height<<endl;
cout<<"Area:"<<triangle2.area();
}
return 0;
}
Triangle.h (I can't edit this one)
#ifndef TRIANGLEH
#define TRIANGLEH
class Triangle {
private:
double base;
double height;
public:
void SetBase(double userBase);
void SetHeight(double userHeight);
double GetArea() const;
void PrintInfo() const;
};
#endif
Triangle.cpp (I also can't edit this one)
#include "Triangle.h"
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void Triangle::SetBase(double userBase) {
base = userBase;
}
void Triangle::SetHeight(double userHeight) {
height = userHeight;
}
double Triangle::GetArea() const {
return 0.5 * base * height;
}
void Triangle::PrintInfo() const {
cout << fixed << setprecision(2);
cout << "Base: " << base << endl;
cout << "Height: " << height << endl;
cout << "Area: " << round(GetArea() * 100.0f) / 100.0f << endl;
}