6 Replies - 727 Views - Last Post: 15 August 2009 - 11:39 AM Rate Topic: -----

#1 daliash  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 30-November 08

infinite loop

Posted 15 August 2009 - 08:17 AM

why this code inter in infinite loop when the input is

19000 100000

* this program accept two integers and output the number of primes number between them ( inclusive the begin and end number ) and also show the number of primes number which be able to write as the result of 2 squsred number p = c^2 + b^2 , the program stopped when inter -1 -1 as input

# include  <iostream>
# include < math.h>
using namespace std;
bool func( int num)
{
	bool square= false;
	int sum1=-1, sum2=-1;
	float number = num;
	float temp1;
	int temp2;

	while ( number !=1)
	{
			temp1=sqrt(number);
			temp2= (int)temp1;

			if (temp1-temp2==0)
			{
				sum1=sqrt(number);
				number =num-(sum1*sum1);

				temp1=sqrt(number);
				temp2= (int)temp1;
				if (temp1-temp2==0)
				{
				   sum2=sqrt(number);
					square= true;
 				   break;

				}
				number= sum1*sum1;
			}
			 
	
	number--;

	}
	return square;


}
bool IsPrime(int num)
{
	bool prime = true;
	if ( num<= 1)
		return false; 
	for ( int i=2; i<num;i++)
		if ( num%i== 0  )
			prime =false;

return prime;
}

int main()
{
	
	int first_Num, last_Num,Total_primes=0,NUm_primes_has_squares=0;
	bool change = false;
		cin >> first_Num >> last_Num;
		while ( first_Num!=-1 || last_Num!=-1)
		{
			if ( first_Num ==last_Num)
			{ change = true;
				last_Num++;
			}
			for ( int i=first_Num; i<=last_Num;i++)
			{
				if ( IsPrime(i))
				{
					Total_primes++;
					if  ( func(i))
					  NUm_primes_has_squares++;
				}
			
				

			}
		if ( change)
				last_Num--;
			cout << first_Num << " " << last_Num<<" "<<Total_primes <<" " <<NUm_primes_has_squares<<endl;

				cin >> first_Num >> last_Num;
				Total_primes=0;
				NUm_primes_has_squares=0;

		}

return  0;
}



This post has been edited by daliash: 15 August 2009 - 08:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: infinite loop

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: infinite loop

Posted 15 August 2009 - 08:24 AM

I tried it, and it is not infinite. If you enter 2 negative numbers, the process terminates.

Cheers
Was This Post Helpful? 0
  • +
  • -

#3 deery5000  Icon User is offline

  • D.I.C Lover

Reputation: 87
  • View blog
  • Posts: 1,097
  • Joined: 09-May 09

Re: infinite loop

Posted 15 August 2009 - 08:40 AM

to create a never ending loop just use

while(1)
{

}




to come out of the loop if you meet a condition use the keyword
break;

Hope this helps
Kevin
Was This Post Helpful? 0
  • +
  • -

#4 daliash  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 30-November 08

Re: infinite loop

Posted 15 August 2009 - 08:41 AM

View PostDogstopper, on 15 Aug, 2009 - 07:24 AM, said:

I tried it, and it is not infinite. If you enter 2 negative numbers, the process terminates.

Cheers


thanks a lot for reply

try to inter 19000 100000 as a input there is no output appear ( it log in infinite loop )

I do not know where this loop and when was enterd


Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: infinite loop

Posted 15 August 2009 - 08:45 AM

View Postdaliash, on 15 Aug, 2009 - 07:41 AM, said:

View PostDogstopper, on 15 Aug, 2009 - 07:24 AM, said:

I tried it, and it is not infinite. If you enter 2 negative numbers, the process terminates.

Cheers


thanks a lot for reply

try to inter 19000 100000 as a input there is no output appear ( it log in infinite loop )

I do not know where this loop and when was enterd


Thanks in advance


I think that this is due to the fact that int datatypes are too small to hold the product of these numbers. try using a long datatype instead.

long sum1, sum2;


Was This Post Helpful? 0
  • +
  • -

#6 daliash  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 30-November 08

Re: infinite loop

Posted 15 August 2009 - 09:33 AM

View PostDogstopper, on 15 Aug, 2009 - 07:45 AM, said:

View Postdaliash, on 15 Aug, 2009 - 07:41 AM, said:

View PostDogstopper, on 15 Aug, 2009 - 07:24 AM, said:

I tried it, and it is not infinite. If you enter 2 negative numbers, the process terminates.

Cheers


thanks a lot for reply

try to inter 19000 100000 as a input there is no output appear ( it log in infinite loop )

I do not know where this loop and when was enterd


Thanks in advance


I think that this is due to the fact that int datatypes are too small to hold the product of these numbers. try using a long datatype instead.

long sum1, sum2;



I correct it as u say , but
it still not appear the output
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: infinite loop

Posted 15 August 2009 - 11:39 AM

well this is a good time to learn a little bit about debugging. You might want to add some print statements into the program in a few places to figure out what the values are. You should be able to extract what is happening.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1