My Program Calculating Problem (2)

Another calculating problem,
this time 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
this time the program keep showing the result -0.5
and i m not very sure about my formula in the code.

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

#include <iostream>
#include <cstdlib>

using namespace std;

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

long double pow (double);

long double euler (double);

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

long double euler (double);

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

long double pow (double);

long double euler (long double ctr, long double input)
{
    int euler = 1;
    while (counter <= input)
    {
        euler = input * (ctr, theta) / fac (ctr);
        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
1
2
3
  if (selection = 1)
   else if (selection = 2)
   else if (selection = 3)


I think you want the equality operator ==, not the assignment operator =.

You have some duplicate function declarations. They're not really needed if you're going to have the full function definition before main().

1
2
3
4
5
long double pow (double);
long double pow (double);

long double euler (double);
long double euler (double);


I don't see where you assign a value to the theta variable you've declared?
1
2
     x = x * theta;
       euler = input * (ctr, theta) / fac (ctr);



If you use the <> button at the right on your code instead of the other one, it'll format your code for the forum.
The newest code, this time the system shows the result 1 for cosh x function and 0 for the other 2 functions,...and I cancelled the theta
#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;
}
and sorry for keep asking becoz i m still a beginner.
Anyway, another formula has already given as follows:
sinh x = x + (x^3)/3! + (x^5)/5! + (x^7)/7! + ... = ∑(infinity && n=0) (x^(2n+1))/(2n+1)!
Last edited on
I think Ganado has given some useful advice here:
http://www.cplusplus.com/forum/beginner/138176/#msg732481

in particular, get rid of all of the global variables:
6
7
8
9
10
11
long double fraction;
long double input;
long double result;
long double counter =1;
long double ctr = 1;
long double count = 1;

Remove those lines from the global space. If necessary declare the variable locally inside the function where it will be used. Use the function parameters and return values instead, as a means of communicating between functions.

Also, please use the actual code tags, like this:
[code]your code here[/code]
select your code and click the <> button.
Thank you for answering
Topic archived. No new replies allowed.