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;
}
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;
}
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.