Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[Homework] How to compute area under a curve using monte carlo method

monte carlo

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1 SoumyaNandanMishra

SoumyaNandanMishra

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 26 January 2015 - 03:53 AM

#include <iostream>
#include <cstdlib> // for: rand(), srand(), and RAND_MAX
#include <ctime> // for: time(), accessing the system clock
#include <cmath> // for: fabs(), sin()
using namespace std;
float const PI = 3.141593;
float find_area(float hi_x);
// evaluate the area under the sine(x) curve,
// x between [0, hi_x], where hi_x is <= PI
int main()
{
    float area, rel_error, true_value;
    cout << "integral of sine(x) between 0 and PI\n";
    area = find_area(PI);
    true_value = 2.0; // known result
    rel_error = fabs((true_value - area)/true_value);
    cout << "The correct analytical result is " << true_value << endl;
    cout << "integral is approximately " << area << endl;
    cout << "relative error is " << rel_error << endl;
    return 0;
}

// find area under sine(x) curve between x = [0, hi_x]
// by the Monte Carlo Method
// constants: PI = 3.141593
// pre: argument hi_x has a positive value
// post: estimated area has been returned
// <your name> <date>
float find_area(float hi_x)
{
    // the method goes here;
    return 0; // (replace this stub)
}

Edited by dargueta, 26 January 2015 - 02:46 PM.
Added code tags


#2 faizybaig

faizybaig

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 26 January 2015 - 10:47 AM

Whats this code saying?



#3 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 26 January 2015 - 02:48 PM

faizybaig: This is template code for an assignment.

 

Soumya: We gladly help with homework, but we won't do it for you. What do you know about the Monte Carlo method?


Edited by dargueta, 26 January 2015 - 02:48 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 30 January 2015 - 07:56 PM

Just in case, this is what Monte Carlo method for finding area is about.

 

And this is a programing approach on how to work with it.



#5 SoumyaNandanMishra

SoumyaNandanMishra

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 30 January 2015 - 09:24 PM

Actually i want an exact c code for this.


anyone can mail me the complete c code (soumyanandanmishra@gmail.com)



#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 30 January 2015 - 11:14 PM

Like I said before, we don't do homework for you, but we're happy to walk you through it.


sudo rm -rf / && echo $'Sanitize your inputs!'


#7 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 31 January 2015 - 08:56 AM

That's wishful thinking. Someone coming to do your homework? Hmm.

Anyway, what good will it be if later you wouldn't be able to defend it with the teacher ?



#8 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 02 February 2015 - 06:29 AM

Hrmm... I suppose I could work up an "obfuscated Monte Carlo approach to finding area". Maybe use some typedefs and pointers to functions and pointers to pointers to pointers to make it as hard as possible to see why the thing even compiles as possible. Of course, to make it optimal, the source code should be spread across 17 files with a make file provided to compile it :)


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#9 PBJ

PBJ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 176 posts

Posted 02 February 2015 - 04:16 PM

@soumya

This might help: lmgtfy



#10 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 02 February 2015 - 04:40 PM

Maybe use some typedefs and pointers to functions and pointers to pointers to pointers to make it as hard as possible to see why the thing even compiles as possible. Of course, to make it optimal, the source code should be spread across 17 files with a make file provided to compile it :)

 

You must be one of my coworkers.  <_<


sudo rm -rf / && echo $'Sanitize your inputs!'


#11 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 03 February 2015 - 05:34 AM

You must be one of my coworkers.  <_<

I just figured the next time we get a "please solve problem X", we could turn it into an Obfuscated Coding challenge for all our regulars. We could provide the code the user needed in a manner they can never turn in.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#12 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 03 February 2015 - 05:06 PM

Nah, I meant some of my former coworkers coded like that normally.

 

Anyway, you'd be surprised what people turn in. One of my teachers told me about a time that a girl copied and pasted a Wikipedia article and turned that in as her paper. Problem is, she didn't remove the hyperlinks... FAIL.


sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download