8 Replies - 795 Views - Last Post: 04 October 2012 - 07:40 PM Rate Topic: ***-- 2 Votes

#1 nyb17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 12

approximating the value of pi

Posted 04 October 2012 - 06:57 PM

import java.util.Scanner;

public class PiApprox {

    public static void main(String[] args) {
    
	    Scanner keyboard = new Scanner(System.in);
	    	
	    double n, i; 
	    double x = 0;
	    
	    do {
	    	System.out.println("Please enter the ending value (the greater this is, the more accurate the approximation will be): ");
	    	i = keyboard.nextDouble();
	    	
		    for (n = 0 ; n <= i ; n++ )
		    	x += ((Math.pow(-1,n)) / ((2 * n) + 1));		
		   		System.out.println("The approximated value of pi is: " + (4 * x));
		   	
		       	System.out.println("Do you want to run this program again? [y/n]: ");
			   	char y = keyboard.next().charAt(0);
		   		
			   	if (y == 'n' || y == 'N')
			   	break; 
		} while (true);
	
	}
	   		
}



the task indicates the we must:
Prompt the user for an ending value
Approximate the value of pi and display the answer
Ask the user if they want to try again, and do so accordingly.

I've got most of it; however, when i run it again, the formula gets messed up, and instead of an answer along hte lines of 3.14... i get 6(and a bunch of decimals). I cant understand why, so help would be greatly apprecaited. Thanks a bunch

This post has been edited by jon.kiparsky: 04 October 2012 - 07:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: approximating the value of pi

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8029
  • View blog
  • Posts: 13,741
  • Joined: 19-March 11

Re: approximating the value of pi

Posted 04 October 2012 - 07:19 PM

This was subtle - I actually had to run it to see the problem

I'm going to throw the output at you:

Please enter the ending value (the greater this is, the more accurate the approximation will be):
100
The approximated value of pi is: 3.1514934010709914
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
1000
The approximated value of pi is: 6.294085055410544
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
200
The approximated value of pi is: 9.440652802593487
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
101
The approximated value of pi is: 12.572441770166934
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
99
The approximated value of pi is: 15.704034673725477
Do you want to run this program ? [y/n]:
y
Please enter the ending value (the greater this is, the more accurate the approximation will be):
100
The approximated value of pi is: 18.855528074796464
Do you want to run this program ? [y/n]:
n


Do you see the problem now?
Was This Post Helpful? 2
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: approximating the value of pi

Posted 04 October 2012 - 07:26 PM

Edit: Actually, let's run with Jon's problem solving approach.

And it's not subtle, it's sloppy coding. This ain't c ;) !

This post has been edited by cfoley: 04 October 2012 - 07:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 nyb17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 12

Re: approximating the value of pi

Posted 04 October 2012 - 07:30 PM

Thank you very much, to both of you. I really appreciate it. I actually got it while reasoning things out, but none the less, i really appreciate the help.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8029
  • View blog
  • Posts: 13,741
  • Joined: 19-March 11

Re: approximating the value of pi

Posted 04 October 2012 - 07:33 PM

View Postcfoley, on 04 October 2012 - 09:26 PM, said:

Edit: Actually, let's run with Jon's problem solving approach.

And it's not subtle, it's sloppy coding. This ain't c ;) !


Subtle, sloppy... you say po-tay-to and I say po-tah-to... :)
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: approximating the value of pi

Posted 04 October 2012 - 07:34 PM

In that case, I hope you also fixed the two other occurrences of that problem, even though they didn't have obvious side effects.
Was This Post Helpful? 0
  • +
  • -

#7 nyb17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 12

Re: approximating the value of pi

Posted 04 October 2012 - 07:35 PM

And yeah, I agree, it was a stupid mistake on my part. If only i took the time to look things over 4 hours ago. haha, thanks again. As mentioned, I ended up fixing this mistake as I reasoned things out. I am an idiot!
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8029
  • View blog
  • Posts: 13,741
  • Joined: 19-March 11

Re: approximating the value of pi

Posted 04 October 2012 - 07:36 PM

View Postnyb17, on 04 October 2012 - 09:30 PM, said:

Thank you very much, to both of you. I really appreciate it. I actually got it while reasoning things out, but none the less, i really appreciate the help.


As long as you get it, that's enough. Sometimes the best way to come up with the answer is to ask the question.
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: approximating the value of pi

Posted 04 October 2012 - 07:40 PM

Po-tay-to, po-tah-to -- all tastes the same. And accents are sexy anyway.

Sloppy scoping causes bugs. ;)

Anyway, I know you know that. The important point here is that accents are sexy.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1