CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2009
    Posts
    6

    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:



    (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

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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>.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Mar 2009
    Posts
    6

    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?

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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);
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Mar 2009
    Posts
    6

    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?

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: writing a formula with powers and trig in C++ borland builder 4

    Well, try it!
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Mar 2009
    Posts
    6

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    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;
    }
    Last edited by dude_1967; March 18th, 2009 at 06:18 PM. Reason: details, details...
    You're gonna go blind staring into that box all day.

  10. #10
    Join Date
    Mar 2009
    Posts
    6

    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?

  11. #11
    Join Date
    May 2002
    Posts
    1,435

    Re: writing a formula with powers and trig in C++ borland builder 4

    Quote Originally Posted by benbridle38 View Post
    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.

  12. #12
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: writing a formula with powers and trig in C++ borland builder 4

    Quote Originally Posted by benbridle38 View Post
    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.
    You're gonna go blind staring into that box all day.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured