My program has calculating problem

My calculating problem,
It is about the sinh x, cosh x and tanh x function,
the formula given is :
sinh x = (e^x - e^(-x))/2
cosh x = (e^x + e^(-x))/2
The program keep showing the result 0.
and i m not very sure about my formula in the code.

this program is not allowed to use <cmath> function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

#include <iostream>
#include <cstdlib>

using namespace std;

long double fraction;
long double input;
long double result;
long double counter =1;
long double ctr = 1;
long double count = 1;


long double fac (double ctr)
{
    long double fac = 1;
    while (count <= counter)
    {
        fac = fac * count;
        count = count + 1;
    }
    return fac;
}

long double pow (double )
{
    double x = 1;
    while (ctr <= counter)
    {
        x = x * ctr;
        ctr = ctr + 1;
    }
    return x;
}

long double euler (long double ctr, long double input)
{
    int euler = 1;
    while (counter <= input)
    {
        euler = input * (ctr) / fac (counter);
        counter = counter + 1;
    }
    return euler;
}

long double euler (long double, long double);

int main()
{
    char name [20];
    int selection;
    cout<<"Enter your name:"<<endl;
    cin>>name;
    cout<<"Hello, "<<name<<", "<<endl;
    cout<<"Welcome to hyperbolic function calculator!"<<endl;
    cout<<"Please select a function:"<<endl;
    cout<<"1. sinh x function"<<endl;
    cout<<"2. cosh x function"<<endl;
    cout<<"3. tanh x function"<<endl;
    cin>>selection;
    cout<<"Please enter the x value for the function:";
    cin>>input;
    if (selection = 1)
    {
        result = euler(ctr, input);
        result = (result - euler(ctr, - input))/ 2;
    }
    else if (selection = 2)
    {
        result = euler(ctr, input);
        result = (result + euler(ctr, - input)) / 2;
    }
    else if (selection = 3)
    {
        result = euler(ctr, input) ;
        result = (result - euler(ctr, - input))/ 2;
        result = result / (euler(ctr, input) + euler(ctr, - input) / 2);
    }
    cout<<"The answer is:" <<result<<endl;
    cout<<"Thank you for using hyperbolic function calculator!"<<endl;
}
Last edited on
I would suggest testing your functions separately from the rest of your program. Also, using global variables like that makes the program flow REALLY confusing. For example, in your fac() function, you never even use your input parameter, but you use -and modify- some global variables. If you called the same function twice you'll get different results!

Also:
1
2
3
if (selection = 1)
should be
if (selection == 1)

Change it for the others to.
= is assignment operator
== is equality tester

Also, I'm not sure why your goal with the pow function is. Once again, your function isn't using any input parameter, a power function would usually have 2 parameters.

You also never define the constant "e" (2.718..) anywhere. Saying "e to the x" is like saying multiple e by itself x times.


Here's a working example of an exp (e to the x) function, you should be able to apply this to your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
const double E = 2.71828;

long double exp(int x)
{
    if (x == 0) return 1;
    
    long double r = 1;
    for (int i = 0; i < x; i++)
    {
        r *= E;
    }
    return r;
}


int main()
{
    std::cout << exp(4); //54.something
}
Last edited on
You also never define the constant "e" (2.718..) anywhere. Saying "e to the x" is like saying multiple e by itself x times.


It is becoz I prefer to use fraction method instead of substitute e value method, so i didnt define e constant.

The formula of e^x is 1 + (x^0)/0! + (x^1)/1! + (x^2)/2!...

Anyway, thank you for answering.
Last edited on
and what about the equation for the negative power?
negative power is pretty simple.
e-x can be calculated as 1.0/ex
Yeah I have got it. Thank you!
The formula of e^x is 1 + (x^0)/0! + (x^1)/1! + (x^2)/2!...

The nice thing about that series (when written correctly) is that each term can be derived from the previous in a very simple way.


1st term: 1
2nd term: x        = 1st term * x / 1
3rd term: x2 / 2!  = 2nd term * x / 2
4th term: x3 / 3!  = 3rd term * x / 3
5th term: x4 / 4!  = 4th term * x / 4
6th term: x5 / 5!  = 5th term * x / 5
... etc.
Last edited on
Problem solved, thank you!
Topic archived. No new replies allowed.