Please check this loop

This is a discussion on Please check this loop within the C++ Programming forums, part of the General Programming Boards category; I appologize for putting this code up again, but I can not find where it is not looping this time ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23

    Please check this loop

    I appologize for putting this code up again, but I can not find where it is not looping this time again and I don't think it's a simple mistake like last time.
    Code:
    //The program checks to see how many parameters it takes to near Pi using the equation 4X(1-1/3+1/5-1/7+1/9-1/11....)
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    int main()
    {
    double pi = 3.14195;
    double a = 1, p = 1;
    double n = 3, sign = -1;
    bool check = true;
    int finish = 0, t = 0;
    do
    	{
    	while (check)
    		{
    		a += sign*(1.0/(n)); //the equation 
    		sign = -sign; // reversing the signs
    		t++; // counting how many parameters are needed to near Pi
    		n+= 2;
    		if (a*4 <= pi + 1*(pow(10,-(p+1))) && a*4 >= pi - 1*(pow(10, -(p+1)))) //checking if it is close enough to Pi
    		check = false; //breaking loop if it is close enough to Pi		
    		}
    	cout << fixed << setprecision(p);
    	cout << a*4 << " " << t <<  endl;
    	finish++; // adding 1 for each loop completed up to the limit of 6
    	p++; //adding 1 to the power and precision decimal places
    	t = 0; //reseting counter
    	a = 1; //reseting a
    	check = true; //switching check back to true to loop while again
    	}while(finish < 6);
    return 0;
    }
    There are some other errors with it such as a is displaying too inaccurate of a number for the single loop that it does but I first want to get the loop to work then I could fix the problem with a.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    heres a warning you could fix:

    Code:
    Untitled1.cpp [Warning] passing `double' for converting 1 of `std::_Setprecision std::setprecision(int)'

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    Fixed the warning, just set another int variable, but that still didn't fix the huge problem heh. I think the while loop is failing to loop and I don't know why.

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    The problem isn't what I think you think it is

    The series you're using to describe pi is very slow to get within the restraints you give it, so I imagine that you're surprised when it takes until p=96 to get past the first step. Maybe not, I don't really know, since you didn't say why you think the while loop is failing. It works fine for me...
    There is a difference between tedious and difficult.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    Decrypt: Thanks for checking, I'm not sure. Maybe it does work fine but it takes a long time to get the answer, I'm not really sure. I am not sure if the 96 is even correct. My cousin did a code, much different and much more ugly, crude, and all around confusing to read, but it worked. I understand what he did, I was just trying to simplify it, however I am not sure if it worked. If you confirm it works, do you eventually get all the answers?

  6. #6
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    OK, I take it back, it's not just that. You're resetting a at the end of every run through the while loop, but not n. So at the beginning of the second loop, a = 1, but n still equals 197. Then a+=sign*1/n: 1 + (1/197), then 1 + (1/197) - (1/196)...etc.

    That's not the series you're looking for.
    There is a difference between tedious and difficult.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    Ohhhh! That's probly it, let me try that one out.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    BAM! That was it! ha ha ha thanks! Now I just need it to display 3.0, not 3.2 the first time and so forth to 3.14195

    Edit: Quick edit, reading your reply also I should set sign back to -1 as well just in case sign ended in a possitive number, that woudl throw the entire thing off as well.
    Last edited by Daesom; 10-31-2006 at 06:04 PM.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Throw some tracing messages into your code to track the numbers to figure out the solution.

    Also... I suggest switching these loops over to for loops. It would be quite a bit more readable.

    Code:
    //The program checks to see how many parameters it takes to near Pi using the equation 4X(1-1/3+1/5-1/7+1/9-1/11....)
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    int main()
    {
    	double pi = 3.14195;
    	double a = 1, p = 1;
    	double n = 3, sign = -1;
    	bool check = true;
    	int finish = 0, t = 0;
    	do
    	{
    		cout << "p is " << p << '\n';
    		while (check)
    		{
    			a += sign*(1.0/(n)); //the equation 
    			sign = -sign; // reversing the signs
    			t++; // counting how many parameters are needed to near Pi
    			n+= 2;
    
    			cout << "a * 4 " << a * 4 << '\n';
    			cout << "pi + 1 * pow(10, -(p+1))" << pi + 1 * pow(10, -(p+1)) << '\n';
    			cout << "pi - 1 * pow(10, -(p+1))" << pi - 1 * pow(10, -(p+1)) << '\n' << endl;
    
    				if (a*4 <= pi + 1*(pow(10,-(p+1))) && a*4 >= pi - 1*(pow(10, -(p+1)))) //checking if it is close enough to Pi
    					check = false; //breaking loop if it is close enough to Pi		
    		}
    		cout << fixed << setprecision(p);
    		cout << a*4 << " " << t <<  endl;
    		finish++; // adding 1 for each loop completed up to the limit of 6
    		p++; //adding 1 to the power and precision decimal places
    		t = 0; //reseting counter
    		a = 1; //reseting a
    		check = true; //switching check back to true to loop while again
    	}while(finish < 6);
    	return 0;
    }
    Code:
    a * 4 3.13118
    pi + 1 * pow(10, -(p+1))3.15195
    pi - 1 * pow(10, -(p+1))3.13195
    
    a * 4 3.1519
    pi + 1 * pow(10, -(p+1))3.15195
    pi - 1 * pow(10, -(p+1))3.13195
    
    3.2 96
    p is 2.0
    a * 4 4.0
    pi + 1 * pow(10, -(p+1))3.1
    pi - 1 * pow(10, -(p+1))3.1
    
    a * 4 4.0
    pi + 1 * pow(10, -(p+1))3.1
    pi - 1 * pow(10, -(p+1))3.1
    
    a * 4 4.0
    pi + 1 * pow(10, -(p+1))3.1
    pi - 1 * pow(10, -(p+1))3.1
    
    a * 4 4.0
    pi + 1 * pow(10, -(p+1))3.1
    pi - 1 * pow(10, -(p+1))3.1
    The value of a is not changing appreciably, because you are forgetting to reset the value of n to 3 at the start of each loop.
    Callou collei we'll code the way
    Of prime numbers and pings!

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    Thank you everyone who has helped me fine tune this code, the final code looks like this:
    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    int main()
    {
    double pi = 3.14195;
    double a = 1, p = 2;
    double n = 3, sign = -1;
    bool check = true;
    int finish = 1, t = 0, e = 1;
    do
    {
    	while (check)
    		{
    		a += sign*(1.0/(n));
    		sign = -sign;
    		t++;
    		n+= 2;
    		if (a*4 <= pi + 1*(pow(10,-(p))) && a*4 >= pi - 1*(pow(10, -(p))))
    		check = false;		
    		}
    	cout << fixed << setprecision(e);
    	cout << a*4 << " " << t <<  endl;
    	finish++;
    	p++;
    	e++;
    	t = 0;
    	a = 1;
    	n = 3;
    	sign = -1;
    	check = true;
    }while(finish < 6);
    return 0;
    }
    The only problem I am having is it is rounding where I now don't want it to round. I am not sure if this is solveable, I am only a novice, but if anyone has a suggestion to fix that minute problem, please post a reply, thanks.

  11. #11
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Throw some tracing messages into your code to track the numbers to figure out the solution.
    That or use a debugger. I like to run through the code myself first, (it helps, I think, to understand what's going on) but if I'm stumped I use a debugger to see what's really happening.
    There is a difference between tedious and difficult.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    I dunno, I like to read the code and all too. Never really used the debugger yet. In truth I am just starting to learn C++. This is my first class, and I know I make lots of small, stuipid mistakes, but ask you all can plainly see I understood the code, I just needed some more eyes to spot the error, just like proof reading a paper. I'll try to use the debugger and see if that helps more, but a human touch is usually better heh.

  13. #13
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    but a human touch is usually better
    That's true a lot of the time, but make sure you're putting full effort into solving it yourself before you post. This was good, you told us about what you thought was wrong, etc. If you can add in the things you've tried on your own, more people will be willing to look into it for you. Good luck.
    There is a difference between tedious and difficult.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Just so you know, that's one of the slowest pi algorithms in existence. You might want Machin or Ramanujan series, or the simpler (actually simpler than the above code) Gause-Legendre algorithm, which converges very quickly.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM
  3. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. Replies: 1
    Last Post: 11-19-2001, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21