NEED HELP !Assignment to Approximate pi

• (2 Pages)
• 1
• 2

20 Replies - 7344 Views - Last Post: 11 March 2010 - 06:14 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=160909&amp;s=4705aa1da2326dac76c5607680ac8069&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 keeper962

• New D.I.C Head

Reputation: -1
• Posts: 39
• Joined: 23-February 10

NEED HELP !Assignment to Approximate pi

Posted 09 March 2010 - 04:26 PM

So I have been ok up to this point but I have an assignment due in two days and I don't know where to start. I feel really overwhelmed.

Heres the assigment:

Write a program that approximates the value of pi.

1.Produce a pair of random #s corresponding to the coordinates of a point with in the unit square.

x=random (seed)
y=random(seed)

This is what I was given but I don't know what to do with it:

On graph you have 1/4 of circle of that circle you make a square that surrounds it Kind of like below
|
1___________________
|..................|
|..................|
|..................|
|..................|
|..................|
|..................|
0__________________|_________________
|..................1
|
|
|

Area of square =1*1=1
Area of fragment circle=(pi)/4

Area of about circle with radius of 1= pi

AS/Ac= number of generated circle point(AS)/#of points in circle(Ac)(start at 0 if in circle AC+1)

Ac= nc*1/n total(assume n total is 10000)

pi=nc*4/n total

All the formulas are confussing me
I know I have to post code for people to start helping me but I hvae none; I don't even no where to start
I really need help.

This post has been edited by NickDMax: 10 March 2010 - 12:49 PM
Reason for edit:: updated formatting

Is This A Good Question/Topic? 0

Replies To: NEED HELP !Assignment to Approximate pi

#2 JackOfAllTrades

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

Re: NEED HELP !Assignment to Approximate pi

Posted 09 March 2010 - 04:43 PM

Whenever I see someone with an assignment from a class accompanied by the statement, "I don't know where to start," I think that person needs to go talk to their teacher.

You see, we can't tell you where to start, because:

WE DON'T KNOW what you've been taught!
WE DON'T KNOW in what class you're studying.
WE DON'T KNOW what is the purpose of the assignment; that is, an assignment is a demonstration of your understanding of something taught in your class...but we have no context in which to begin to assist.

I get the feeling you may have waited until the last minute, and you're overwhelmed. NOW is the time to take care of that the RIGHT way. Go see your teacher ASAP.
Was This Post Helpful? 2

#3 keeper962

• New D.I.C Head

Reputation: -1
• Posts: 39
• Joined: 23-February 10

Re: NEED HELP !Assignment to Approximate pi

Posted 09 March 2010 - 05:42 PM

He just assigned it and ussually he gives us examples of how to write a full code for the topic but he ran out of time and didn't show us an example.
I will speak to him tommorow to try and clarify the assignment.

Then I will go from there thank you.

He wants us to use fixed repition loop.

Can anyone show me an example of the full sample code for this loop because the book we are using doesn't have one and before I see him I like to understand this loop better I think this is where I am hving trouble I need an example of this loop.
Was This Post Helpful? 0

#4 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

Re: NEED HELP !Assignment to Approximate pi

Posted 09 March 2010 - 05:46 PM

What you are asking for seems similar to the Monte Carlo method to approximate pi. Check out the link, which provides some pseudocode and a discussion on it.

http://www.soe.ucsc..../lab5/lab5.html
Was This Post Helpful? 1

#5 keeper962

• New D.I.C Head

Reputation: -1
• Posts: 39
• Joined: 23-February 10

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 12:39 PM

erik.price, on 09 March 2010 - 04:46 PM, said:

What you are asking for seems similar to the Monte Carlo method to approximate pi. Check out the link, which provides some pseudocode and a discussion on it.

http://www.soe.ucsc..../lab5/lab5.html

Thanks that helped alot!!

I was able to come up with this code that gives me the value of pi but it doesn't display the 2 random numbers x and y. also for some reason when I just to that part seperately the x and y values do appear but they are negitive how do I make them positive.

Also I am not sure how to write the code for getting all the number of points in the circle starting at 0 The formula is nc*1/n total (assume ntotal is 1000)

What is the code for number of generated points in circle.

I feel like I am missing something because I don't use the formula pi= nc*4/ntotal right

(I am not sure how to get nc)

That is my big problem then I think the program is right if I fix that and am able to display x and y positive values

Here is the code:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>

using namespace std;

int as, nc, total;
double x,y;

double random (unsigned int & seed);
unsigned seed=1;

int main()
{
as=1000;
nc=0;//this is the part that is wrong I think
total=10000;

double x,y;
srand((unsigned)time(NULL));
for (int i =0; i<1;++i)
{
x=((double) rand() / (RAND_MAX+1)) ;
cout<<"x= "<<x<<endl;
y=((double) rand() / (RAND_MAX+1)) ;
cout<<"y= "<<y<<endl;
}
if (sqrt((x*x)+(y*y))<1)
{
++nc;

}

}
cout<<nc*4/total<<endl;

}

This post has been edited by keeper962: 10 March 2010 - 12:40 PM

Was This Post Helpful? 0

#6 keeper962

• New D.I.C Head

Reputation: -1
• Posts: 39
• Joined: 23-February 10

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 05:27 PM

Can anyone help me its due tommorrow
Was This Post Helpful? 0

#7 CTphpnwb

• D.I.C Lover

Reputation: 3786
• Posts: 13,715
• Joined: 08-August 08

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 05:41 PM

This:
for (int i =0; i<1;++i)

will set i to 0, then when i is incremented the loop will end.
Was This Post Helpful? 0

#8 keeper962

• New D.I.C Head

Reputation: -1
• Posts: 39
• Joined: 23-February 10

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 05:44 PM

CTphpnwb, on 10 March 2010 - 04:41 PM, said:

This:
for (int i =0; i<1;++i)

will set i to 0, then when i is incremented the loop will end.

how do you fix it

and what about nc how do you get it I don't think it is supposed to be o
Was This Post Helpful? 0

#9 CTphpnwb

• D.I.C Lover

Reputation: 3786
• Posts: 13,715
• Joined: 08-August 08

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 06:42 PM

Use:
i < total

You're getting negative numbers because RAND_MAX is typically the largest positive integer the system can store. Adding 1 to it sets the highest bit, which makes it a negative number. Don't add one.

Oh, and you're adding to nc outside of the loop, so that can only happen once! Put its 'if' statement inside the loop.

This post has been edited by CTphpnwb: 10 March 2010 - 06:43 PM

Was This Post Helpful? 0

#10 keeper962

• New D.I.C Head

Reputation: -1
• Posts: 39
• Joined: 23-February 10

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 07:34 PM

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>

using namespace std;

int as, nc, total;
double x,y;

double random (unsigned int & seed);
unsigned seed=1;

int main()
{
as=1000;
nc=0;//this is the part that is wrong I think
total=10000;

double x,y;
srand((unsigned)time(NULL));
for (int i =0; i<total;++i)
{
x=((double) rand() / (RAND_MAX+1)) ;
cout<<"x= "<<x<<endl;
y=((double) rand() / (RAND_MAX+1)) ;
cout<<"y= "<<y<<endl;

if (sqrt((x*x)+(y*y))<1)
{
++nc;

}

}
cout<<nc*4/total<<endl;

}

So I cahnged it but now the loop is continuous I only need it to show two random numbers x and y and display Pi using those numbers

What do I need to fix?
Was This Post Helpful? 0

#11 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 07:43 PM

As CTphpnwb said, this expression causes an integer overflow:
(RAND_MAX+1)
Make it just RAND_MAX instead.

Also, don't forget to cast to a double, otherwise you'll get an int answer!
cout<<(double) nc * 4 / total<<endl;

Besides that, your program compiles and runs. It estimated 3.1496 for me

This post has been edited by erik.price: 10 March 2010 - 07:43 PM

Was This Post Helpful? 0

#12 keeper962

• New D.I.C Head

Reputation: -1
• Posts: 39
• Joined: 23-February 10

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 07:50 PM

erik.price, on 10 March 2010 - 06:43 PM, said:

As CTphpnwb said, this expression causes an integer overflow:
(RAND_MAX+1)
Make it just RAND_MAX instead.

Also, don't forget to cast to a double, otherwise you'll get an int answer!
cout<<(double) nc * 4 / total<<endl;

Besides that, your program compiles and runs. It estimated 3.1496 for me

It is it just a loop of x and ys it keeps going on

How do I make it so only one x and one x are displayed along with pi.

Did I use all of the formuals I was supposed to (see first post)or am I missing something
Was This Post Helpful? 0

#13 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 07:56 PM

Once the loop stops, the estimate is produced. Maybe you're working on an extremely old computer if you see this as an infinite loop, because it worked for me in .939 seconds (and my computer is horrible!)

Since the variables x and y are not local to the loop, you could just move the two cout's to where you print out the estimate:
cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl;
cout<<(double) nc * 4 / total<<endl;

I'm not sure if this is what you want though, since x and y simply represent the last random guesses taken
Was This Post Helpful? 0

#14 keeper962

• New D.I.C Head

Reputation: -1
• Posts: 39
• Joined: 23-February 10

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 08:31 PM

erik.price, on 10 March 2010 - 06:56 PM, said:

Once the loop stops, the estimate is produced. Maybe you're working on an extremely old computer if you see this as an infinite loop, because it worked for me in .939 seconds (and my computer is horrible!)

Since the variables x and y are not local to the loop, you could just move the two cout's to where you print out the estimate:
cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl;
cout<<(double) nc * 4 / total<<endl;

I'm not sure if this is what you want though, since x and y simply represent the last random guesses taken

I am using windows Dev C++ if that has anything to do with why it doesn't work

I think now the output screen doesn't show up when I run the program

At school I use terminal is there a way to use it on a apple or windows

And....

Besides it not working for me I can probably work it at school but

This might be a odd question but the assignment question I gave in the first post are all the parts used and answered

Is the program doing all it is supposed to?

Don't know if you can answer but it would help because I feel like I might be missing something/not answering all parts.

Thanks

This post has been edited by keeper962: 10 March 2010 - 08:33 PM

Was This Post Helpful? 0

#15 CTphpnwb

• D.I.C Lover

Reputation: 3786
• Posts: 13,715
• Joined: 08-August 08

Re: NEED HELP !Assignment to Approximate pi

Posted 10 March 2010 - 09:01 PM

I think you're making this harder than it needs to be. I did it in 20 lines, 22 if you count blank ones for formatting.

keeper962, on 10 March 2010 - 10:31 PM, said:

At school I use terminal is there a way to use it on a apple or windows

I did it in Xcode on my Mac.
Was This Post Helpful? 0

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }