isPrime()

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 25
Reputation: forumposters is an unknown quantity at this point 
Solved Threads: 1
forumposters forumposters is offline Offline
Light Poster

isPrime()

 
0
  #1
Jul 6th, 2007
I need to write a main() function, that loops over a bunch of
numbers from 0 to 100, calls another function called isPrime() for each of them, and print out the numbers that are prime.
I'm new to C++ and any help would be appreciated.
Last edited by forumposters; Jul 6th, 2007 at 9:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: isPrime()

 
0
  #2
Jul 6th, 2007
>I'm new to C++ and any help would be appreciated.
At least start trying to solve your problem before asking for help. It makes us like you more and ignore you less.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: isPrime()

 
0
  #3
Jul 6th, 2007
What narue said applies. But I'll help you a little: isPrime(int x)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 25
Reputation: forumposters is an unknown quantity at this point 
Solved Threads: 1
forumposters forumposters is offline Offline
Light Poster

Re: isPrime()

 
0
  #4
Jul 7th, 2007
Here's what I've come up with so far:
  1. #include <iostream>
  2.  
  3. bool IsPrime(int num)
  4. {
  5. if(num == 0)
  6. return true;
  7.  
  8. num = abs(num);
  9.  
  10. for(int i = 2; i <= sqrt(num); i++)
  11. if(num % i == 0)
  12. return false;
  13.  
  14. return true;
  15. }
Last edited by ~s.o.s~; Jul 7th, 2007 at 3:38 am. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: isPrime()

 
0
  #5
Jul 7th, 2007
You've been around over a year and haven't read the Rules yet? You also need to read this. It will explain that you need to tell us what problem you are having so we don't have to guess -- among other things
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: isPrime()

 
0
  #6
Jul 7th, 2007
>Here's what I've come up with so far
Somehow I find it hard to believe that you can write IsPrime but not the main driver to print the primes in the range [0..100). I'm guessing you were given IsPrime and told to write main, which means you've come up with nothing so far. But, the hard part is done already. All you need to do is loop from 0 to 100 and call IsPrime on the current number. If it returns true, print the number:
  1. for ( int i = 0; i < 100; i++ ) {
  2. if ( IsPrime ( i ) )
  3. cout<< i <<" is prime\n";
  4. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 222
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: isPrime()

 
0
  #7
Jul 7th, 2007
Originally Posted by Narue View Post
>Here's what I've come up with so far
Somehow I find it hard to believe that you can write IsPrime but not the main driver to print the primes in the range [0..100). I'm guessing you were given IsPrime and told to write main, which means you've come up with nothing so far.
[/code]
LOL! If some people would put half of the effort that they put into "avoiding work" into something productive, they would have had the task acomplished by now!

If laziness is truely the mother of invention, we have quite a group of potential inventors here!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC