Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2006, 2:45 PM   #1
can342man
Newbie
 
can342man's Avatar
 
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0 can342man is on a distinguished road
Hanging For loop in C++

Hi everyone,

I am just curious if anyone has come across why a for loop used in a number crunching scheme would hang when and not return the command prompt. No compilation errors and the conditions for the loops are defined.

Just to be more direct, here is the code. It might be too complex but it is fairly simple 10 loops represent summations of a mathematical function and 1 outer loop for indexing them all into an array. This is just the basics of it.

void h1_tm(double *inner1_tm)
{
//h1 variables
	double t1_1a, t1_2a, t1_3a, t1_4a, t1_5a;		//variables
	double re1_1a, im1_1a, re1_2a, im1_2a, tot1_a;		//variables
	double re1_1b, im1_1b, re1_2b, im1_2b, tot1_b;		//conjugates

	int i, j, l, m, n, p;				//indices (int)
//	double j, l, m, n;			//indices (double)

        double c = GSL_CONST_MKSA_SPEED_OF_LIGHT;
        double k = (4.0*M_PI*f_0*R_E*sin(alpha))/c;

        double w_0 = 2.0*M_PI*f_0;             // radial frequency of GW signal
        double w_r = R;           // rotational frequency of Earth
        double w_orb = 1.0;                  // Earth orbital rotational frequency	

//outer loop for indexing total
	for (i = 0; i <= 0; i++) {

//inner summation loops for functions and conjugates
        for (j = 1; j <= 365; j++) {
		for (j = 1; j <= 365; j++) {
			for (p = 0; p <= 5; p++) {
				for (p = 0; p <= 5; p++) {
					for (n = -5; n <= 5; n++) {
					for (n = -5; n <= 5; n++) {
                                for (l = 0; l <= 5; l++) {
			for (l = 0; l <= 5; l++) {
		for (m = l; m <= l; m++) {       
	for (m = l; m <= l; m++) {

//h1 functions
        gsl_complex cpxr1_a = gsl_complex_rect(0.0, 1.0);            // z = 0 + i
        gsl_complex t1_1a = gsl_complex_pow_real(cpxr1_a, l+n);
	im1_1a = GSL_IMAG(t1_1a);
	re1_1a = GSL_REAL(t1_1a); 
        t1_2a = (pow(2.0,0.5)*pow(M_PI,(5.0/2.0)))/w_r;
        t1_3a = (pow(k,l+0.5))/(pow(2.0,2.0*l+1.0));
	t1_5a = gsl_sf_fact(p);
	t1_4a = pow(((-k*k)/16.0),p)/t1_5a;                
        gsl_complex cpxp1_2a = gsl_complex_polar(1.0, M_PI*j*l-M_PI*l);
	re1_2a = GSL_REAL(cpxp1_2a);
	im1_2a = GSL_IMAG(cpxp1_2a);
	tot1_a = im1_1a*re1_1a*t1_2a*t1_3a*t1_4a*im1_2a*re1_2a;
//	cout << tot1_a  << endl;

//h1 conjugates (all other terms same as above except for complex terms)
        gsl_complex cpxr1_b = gsl_complex_rect(0.0, -1.0);            // z = 0 - i
        gsl_complex t1_1b = gsl_complex_pow_real(cpxr1_b, l+n);       
        im1_1b = GSL_IMAG(t1_1b);
        re1_1b = GSL_REAL(t1_1b); 
        gsl_complex cpxp1_2b = gsl_complex_polar(1.0, -1.0*M_PI*j*l-M_PI*l);
        re1_2b = GSL_REAL(cpxp1_2b);
        im1_2b = GSL_IMAG(cpxp1_2b);
        tot1_b = im1_1b*re1_1b*t1_2a*t1_3a*t1_4a*im1_2b*re1_2b;
//      cout << tot1_b  << endl;

	inner1_tm[i] = tot1_b*tot1_a;
//	cout << inner1_tm[i] << endl;

                   					                     }
                                					}
                       						 }
              						  }
      						  }
					}
				}
			}
		}
	}
 }

}

Anyone have any ideas???

Thanks.

:banana:
__________________
Greatness courts failure and solitude.
--- Anonymous

Last edited by can342man; Apr 27th, 2006 at 3:03 PM.
can342man is offline   Reply With Quote
Old Apr 27th, 2006, 2:55 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
We're not mind-readers. Post some code. Read about how to post a sensible question somewhere. Eat that freakin' banana, it might help.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 27th, 2006, 3:30 PM   #3
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
I would just start the debugger and step through the loop once.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Apr 27th, 2006, 8:11 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 850
Rep Power: 4 The Dark is on a distinguished road
Whether it is hanging depends on how long you think it should take for 3.5 billion sets of calculations.
The Dark is offline   Reply With Quote
Old Apr 27th, 2006, 8:13 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
ROFL @ Steven and The Dark. See how editing the code in cleared that up?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 27th, 2006, 11:55 PM   #6
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
hmmm ..I wonder if he is still stepping

..how to keep a canman occupied
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Apr 28th, 2006, 12:29 AM   #7
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 309
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
What I want to know is what he THOUGHT that code was going to do...
andro is offline   Reply With Quote
Old Apr 28th, 2006, 9:01 AM   #8
can342man
Newbie
 
can342man's Avatar
 
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0 can342man is on a distinguished road
The code is just one part of a long calculation for the gravitational wave signal from a pulsar star. The mathematical functions are not that significant in this particular problem. It is mainly a for loop problem and hence the whole thing can be reduced to a simple for loop question.

I will try the debugger.

Thanks
__________________
Greatness courts failure and solitude.
--- Anonymous
can342man is offline   Reply With Quote
Old Apr 28th, 2006, 9:57 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5 grumpy is on a distinguished road
Given that some of your inner loops are changing variables that control your outer loops, it is a little hard to predict the behaviour of your program at first glance.

I'm not necessarily sure that the loops are infinite, but you will certainly find the sequence entertaining when stepping through in a debugger.
grumpy is offline   Reply With Quote
Old Apr 28th, 2006, 2:43 PM   #10
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 309
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
1*365*365*6*6*11*11*6*6*1*1=20891811600

That's 20,891,811,600 times you are calculating whatever is inside the for loops. I think there is a mistake in your logic somewhere..?
andro is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC