Triangle , C++ construction, problem with calculating angles

1k views Asked by At

I have a problem with my homework.

1) I should write the function SSS with inputs a, b and c. I need to output alpha, beta and gamma.

2) I should write the function SWS with inputs a, b, and gamma. And I need to output c, alpha and beta.

Here is my code:

#include <iostream>
#include <cmath>
#include <math.h>
#include <cstdio>

using namespace std;

double SSS_alpha(double a, double b, double c){

    int bot1 = -2*b*c;
    if(bot1==0.0){
        return 0.0;
    }

    double alpha = acos((a*a-b*b-c*c)/bot1);
    const double rad = 0.5729577951308232088;
    return alpha*rad;

}

double SSS_beta(double a, double b, double c){

    double bot2 = -2*c*a;
    if(bot2==0.0){
        return 0.0;
    }

    double beta = acos((b*b-c*c-a*a)/bot2);
    const double rad = 0.5729577951308232088;
    return beta*rad;
}

double SSS_gamma(double a, double b, double c){

    double bot3 = -2*b*a;
    if(bot3==0.0){
        return 0.0;
    }

    double gamma = acos((c*c-a*a-b*b)/bot3);
    const double rad = 0.5729577951308232088;
    return gamma*rad;
}


double WSW_seite_c(double a, double b, double gamma){
    double c_1 = (a*a+b*b-2*a*b*cos(gamma));
    double c = sqrt(c_1);
    return c;

}



int main(){
    cout << SSS_alpha(5.0, 7.0, 8.0)<<endl;
    cout <<SSS_beta(5.0, 7.0, 8.0)<<endl;
    cout <<SSS_gamma(5,7,8)<<endl;
    cout <<"Seite c: "<<WSW_seite_c(5, 7, 0.81)<<endl;

}

I can only return one argument in one function. And I have a lot of functions for task 1:for alpha, for beta, for gamma. And I don't know how I can write it in one function. I wrote only one function for finding c for task 2.

I am new to C++. I would be happy if you can help me.:)

4

There are 4 answers

10
Sirmyself On

A struct will be the simplest way of doing what you want. You can also create a class, but it might be an overkill solution for a homework (they basically are the same, but see my comment thread to get my point of view on that matter).

a struct declaration goes like this :

struct containerName
{
    int val_a;
    double val_b;
    //and so on : you put what ever you need
}

The struct then basically becomes a type you can use pretty much like int, double, bool etc.

Here's a nice reading on struct and how to use it

5
Yastanub On

First you dont need cstdio if you include iostream which you should preferably do in C++ since cstdio is just the C library migrated to C++.

As for your problem it sounds like you need to pass the variables by value. Your favourite C++ Book should tell you that there are cases like yours, where you need to "return" more than one value. In that case you can pass pointers(C-Style) or the safer references to functions which kind of act like pointers.

So you actually would define your function as follows:

void SSS(double a, double b, double c, double & alpha, double & beta, double & gamma);

Or as stated by @Ripi2 pack the values in a struct and return a value of that type :

/* Thanks to NathanOliver in the comments */
struct Values {
    double a;
    double b;
    double c;
};

Values SSS(double a, double b, double c) {
    Values values;
    /* Do stuff */
    return values;
}
0
Damien On

Returning a struct or using a class Triangle as proposed are perfect solutions and it is what I generally use.

However, in some simple cases, returning more than one value through a tuple can be quite convenient.
I showed a simple "working" example below.
Note that I will generally avoid this kind of solution for more than 3 return objects, to avoid confusion.

#include    <iostream>
#include    <tuple>
std::tuple<double, double, double> f(double x, double y){
    double a = x;
    double b = x+y;
    double c = x-y;
    return std::make_tuple(a, b, c);
}
int main() {
    double a, b, c;
    std::tie (a, b, c) = f (1.0, 2.0);
    std::cout << "a = " << a << "\n";
    std::cout << "b = " << b << "\n";
    std::cout << "c = " << c << "\n";
    return 0;
}

I hope the code is self explaining.
The syntax can be simplified with C++17. I did not use these simplifications because my current compiler is not compatible!
This approach can be combined with the struct one, as one can return several structs through a tuple.

Edit: I just see that NathanOliver already mentioned the tuple possibility in a comment...

4
stimulate On

Yes, you can only return one object from a function. But you can have it return a compound object, like a struct or a class. Traditionally you use structs for data only objects, because that is what they were in C.

struct Triangle
{
    double a;
    double b;
    double c;
};

Then you can return a Triangle object from a function. And access its members this way:

Triangle tri1;     // first, create an instance of the struct
tri1.a = 5.0f;      // assign a value to a member

It's good practice to choose meaningful names for your data types (just like with your variables) and always design the data types so that they represent what they are named.


To make the code more expressive you could take it further with a more object oriented approach. You could give the data type Triangle member functions which calculate its angles, so that you can "ask" the Triangle what its angles are.

(You should also name it a class at that point because it it is not a pure data structure anymore, but that is a matter of convention. in C++ structs and classes are almost the same.)

class Triangle
{
    public:
    // everything in this section can be accessed from outside

    // this is a constructor. It is a special function that lets you create a Triangle
    Triangle(double pa, double pb, double pc)
        : a(pa), b(pb), c(pc)   // initialize the members
    {}

    // getters for the private members (not needed but that way they can't directly be changed)
    double get_a() const
    {
        return a;
    }
    double get_b() const
    {
        return b;
    }
    double get_c() const
    {
        return c;
    }

    double alpha() const;
    double beta() const;
    double gamma() const;
    // should be const because they dont change anything about the object

    private:   
    // the default access for classes, everything in this section can only be accessed by member functions

    double a;
    double b;
    double c;
};

// member function definitions outside of class declaration
double Triangle::alpha() const
{
    // calculate and return alpha...
}
double Triangle::beta() const
{
    // calculate and return beta...
}
double Triangle::gamma() const
{
    // ...
}

You can use this class this way:

int main()
{
    // create a Triangle by calling the constructor
    Triangle tri1(5.0, 7.0, 8.0);
    std::cout << "Alpha: " << tri1.alpha() << std::endl;
    std::cout << "Beta: " << tri1.beta() << std::endl;
    std::cout << "Gamma: " << tri1.gamma() << std::endl;
    return 0;
};