writing a formula with powers and trig in C++ borland builder 4
Hi,
dont really know much about programming or anything like this.
But basicly in my program i am trying to write the following formula:
http://img8.imageshack.us/img8/4379/fomula.jpg
(linked formula as a picture to keep its format)
basicly does anyone have any ideas how i would write this?
my guess was Y=(sin((acos(x))^(2/N)))^(2/N);
but i dont think ive done the powers right, ive read somewere that you have to use pow(i,j) for powers?
any help would be apreciated, thanks
Re: writing a formula with powers and trig in C++ borland builder 4
Yes, operator ^ means XOR. For power there is a library function called pow(), that you can find in <cmath>.
Re: writing a formula with powers and trig in C++ borland builder 4
i had a go using the pow notation,
Y = pow(sin(pow(acosx,2/N),2/N);
does that look right at all?
Re: writing a formula with powers and trig in C++ borland builder 4
There is something wrong there. You open 3 parenthesis and close 2.
Code:
y = pow(sin(pow(acos(x), 2/N)), 2/N);
Re: writing a formula with powers and trig in C++ borland builder 4
yea sorry typo
* Y=pow(sin(pow(acos(x), 2/N)), 2/N);
there u go, does this look like it might work though now?
Re: writing a formula with powers and trig in C++ borland builder 4
Re: writing a formula with powers and trig in C++ borland builder 4
can't, lol dont have the program,
will try it tommorrow when i can get to the computer labs.
Thanks for your help bud
Re: writing a formula with powers and trig in C++ borland builder 4
Quote:
Originally Posted by benbridle38
can't, lol dont have the program,
will try it tommorrow when i can get to the computer labs.
Download and install a C or C++ compiler, as the case may be. Search for GCC and/or MSVC.
Re: writing a formula with powers and trig in C++ borland builder 4
Well,
You are writing a higher-level code such as Mathematica or Maple code.
C++ code has a different syntax.
Guru cilu really pointed you in the right direction.
If his hints weren't enough, then take a look at the sample shown below.
Try to understand everything that I have written, but within a C++ context.
Sincerely, Chris.
Code:
#include <iostream>
#include <cmath>
double trig_function(const double x, const unsigned int N)
{
static const double two_pi = 2.0 * 3.141592653589793238462643383;
if(x > two_pi)
{
return 0.0;
}
const double two_over_n = 2.0 / static_cast<double>(N);
const double sin_acos_term = sin(acos(x));
return pow(sin_acos_term, two_over_n);
}
int main(void)
{
std::cout << trig_function(0.5, 3) << std::endl;
}
Re: writing a formula with powers and trig in C++ borland builder 4
oky, really really stuck now,
this is my code:
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
for(float N=0.5;N<10;N=N+0.5)
{
for(int Z=0;Z<=1000000;Z++)
{
x=random(10000)*0.0001;
y=random(10000)*0.0001;
Y=pow(sin(pow(acos(x), 2.0/N)), 2.0/N);
if(y<=Y)
{
P=P+1;
}
}
A=P/250000.0;
cout <<"For value of N = "<<N<<" Area eclosed A = "<<A<<endl;
}
getch();
return 0;
}
trying to run on c++ borland, and it doesn't run.
any ideas?
Re: writing a formula with powers and trig in C++ borland builder 4
Quote:
Originally Posted by
benbridle38
trying to run on c++ borland, and it doesn't run.
any ideas?
Is there something wrong with your compiler? A program should run after it's been compiled properly, but I don't see how that program could be compiled as it's missing some variable definitions among other things.
Re: writing a formula with powers and trig in C++ borland builder 4
Quote:
Originally Posted by
benbridle38
trying to run on c++ borland, and it doesn't run.
any ideas?
Yes Ben, it looks like there might be various slight points of confusion here.
Are you sure that the code compiled? I do not know if the function random(...) is actually defined anywhere. Furthermore your code snippet contains the use of many variables which are undefined.
It looks like you are simply trying to do some kind of a standard assignment which uses random numbers to calculate the area under some kind of curve.
You seem to be getting off to a rough start. Try some of these suggestions.
- Make sure all of your variables are properly defined and initialized.
- Write the loop over N using integers.
- Make the loop over Z much smaller during the development phase allowing for code-verification with a short run-time. As soon as the algorithm functions properly, extend to the full length calculation, increasing by successive factors of 10 so you can see how the run-time develops with increasing order of magnitude of the loop.
Chris.