943,936 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3528
  • C++ RSS
Mar 1st, 2006
0

(Noob) need some help w/ Prime Numbers

Expand Post »
I know there are tons of posts on this, I think I'm on the right track with this, but I can't quite get it to work. I can't figure out how to get the range of primes to print. I'd appreciate any input I can get, sorry about the indenting.

main() keeps doing the following, until user enters a 0 or a negative number:
• Ask user to enter a number;
• If it is positive, print all the prime numbers between 1 and it; when there’s no prime number, print “none�; ( A prime number is a whole number that can only be divided by 1 and itself, i.e., 2, 3, 5, 7, 11, 13, 17, 23, ….)
• Print the max of these prime numbers; or print “none� when there’s no prime.

The calling relation should be: main() calls printPrimes() and getMaxPrime();
printPrimes() and getMaxPrime() calls isPrime() .

My Code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5.  
  6. /* function isPrime (int num) finds out whether the input integer num is a prime number or not
  7. */
  8. bool isPrime (int num)
  9. {
  10. for (int fac = 2; fac <= num; fac++)
  11. if (num % fac == 0) return 0;
  12. return 1;
  13. }
  14.  
  15.  
  16.  
  17.  
  18. /* function printPrimes (int num) prints all the prime numbers between 1 and num
  19. */
  20. void printPrimes (int num)
  21. {
  22. int number = 1;
  23. while(number <= num)
  24. {
  25. ++number;
  26. if (isPrime(number))
  27. cout << number<<" ";
  28. }
  29. }
  30.  
  31.  
  32.  
  33.  
  34. /* function getMaxPrime(int num) returns the greatest prime number between 1 and num. if there is no prime number between 1 and num, getMaxPrime() should return a 0.
  35. */
  36. int getMaxPrime (int num)
  37. {
  38. int maxCount = 0;
  39. for (int count = 1; count <= num; count++)
  40. {
  41. if (isPrime(count))
  42. maxCount = count;
  43. }
  44. return int (maxCount);
  45. }
  46.  
  47.  
  48.  
  49.  
  50. int main ()
  51. {
  52. int number;
  53.  
  54. do
  55. {
  56. cout <<endl<<endl
  57. <<"Please enter a number, 0 or negative to stop :";
  58. cin >> number;
  59. if (number > 0)
  60. {
  61. cout << "All the prime numbers between 1 and "<< number<<" are: "<< endl;
  62. printPrimes(number);
  63. cout << "The max prime is :" ;
  64. int maxPrime = getMaxPrime(number);
  65. if (maxPrime==0) cout <<"none.";
  66. else cout << maxPrime;
  67. }
  68. } while (number>0);
  69. return 0;
  70. }
Attached Files
File Type: cpp lab3work.cpp (1.5 KB, 23 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
coopedw is offline Offline
3 posts
since Mar 2006
Mar 1st, 2006
0

Re: (Noob) need some help w/ Prime Numbers

i'm not sure if I understood the problem but try to replace
C++ Syntax (Toggle Plain Text)
  1. if (num % fac == 0)
with
C++ Syntax (Toggle Plain Text)
  1. if ((num % fac == 0) && (num != fac))

and use this

C++ Syntax (Toggle Plain Text)
  1. void printPrimes (int num)
  2. {
  3. int number = 2;
  4. while(number <= num)
  5. {
  6.  
  7. if (isPrime(number))
  8. cout << number<<" ";
  9. ++number;
  10. }
  11. }
instead your printPrimes
I didn't had the time to test it
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Mar 1st, 2006
0

Re: (Noob) need some help w/ Prime Numbers

if you want between 1 and num (num not included) than
C++ Syntax (Toggle Plain Text)
  1. /* lab3.cpp Working with Functions
  2. */
  3.  
  4. #include <iostream>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8.  
  9. /* function isPrime (int num) finds out whether the input integer num is a prime number or not.
  10. If num is a prime, return true; else return false;
  11.   A prime number is a number whose only factors are 1 and itself.
  12. Note: 0 and 1 are not prime numbers. The minimal prime number is 2.
  13.  
  14. */
  15. bool isPrime (int num)
  16. {
  17. for (int fac = 2; fac <= num; fac++)
  18. if ((num % fac == 0) && (num != fac))
  19. return 0;
  20. return 1;
  21. }
  22.  
  23.  
  24.  
  25.  
  26. /* function printPrimes (int num) prints all the prime numbers between 1 and num
  27. */
  28. void printPrimes (int num)
  29. {
  30. int number = 2;
  31. while(number < num)
  32. {
  33.  
  34. if (isPrime(number))
  35. cout << number<<" ";
  36. ++number;
  37. }
  38. }
  39.  
  40.  
  41.  
  42.  
  43. /* function getMaxPrime(int num) returns the greatest prime number between 1 and num.
  44. if there is no prime number between 1 and num, getMaxPrime() should return a 0.
  45. */
  46. int getMaxPrime (int num)
  47. {
  48. int maxCount = 0;
  49. for (int count = 1; count < num; count++)
  50. {
  51. if (isPrime(count))
  52. maxCount = count;
  53. }
  54. return int (maxCount);
  55. }
  56.  
  57.  
  58.  
  59.  
  60. int main ()
  61. {
  62. int number;
  63.  
  64. do
  65. {
  66. cout <<endl<<endl
  67. <<"Please enter a number, 0 or negative to stop :";
  68. cin >> number;
  69. if (number > 0)
  70. {
  71. cout << "All the prime numbers between 1 and "<< number<<" are: "<< endl;
  72. printPrimes(number);
  73. cout << "The max prime is :" ;
  74. int maxPrime = getMaxPrime(number);
  75. if (maxPrime==0) cout <<"none.";
  76. else cout << maxPrime;
  77. }
  78. } while (number>0);
  79. return 0;
  80. }
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Mar 1st, 2006
0

Re: (Noob) need some help w/ Prime Numbers

I still can't get the printPrimes function to work. It doesn't output anything. and the getMaxPrime function returns 1. does anybody have any suggestions for this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
coopedw is offline Offline
3 posts
since Mar 2006
Mar 2nd, 2006
0

Re: (Noob) need some help w/ Prime Numbers

Finally got it working. I thought I'd post the code in case someone else has the same problem
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. /* function isPrime (int num) finds out whether the input integer num is a prime number or not.
  7. If num is a prime, return true; else return false;
  8.   A prime number is a number whose only factors are 1 and itself.
  9. Note: 0 and 1 are not prime numbers. The minimal prime number is 2.
  10.  
  11. */
  12. bool isPrime (int num)
  13. {
  14. bool primeCheck = true;
  15. for (int fac = 2; fac < num; fac++ )//runs through factors between 2 and num
  16. {
  17. if (num % fac == 0) //checks to see if a number is divisible
  18. primeCheck = false;
  19. }
  20. return (primeCheck); //returns true or false to calling functions printPrimes and getMaxPrime.
  21. }
  22.  
  23. /* function printPrimes (int num) prints all the prime numbers between 1 and num
  24. */
  25. void printPrimes (int num)
  26. {
  27. bool print;
  28. if (num <= 1)
  29. cout << "none!"; // Determines if their are any primes in the range.
  30.  
  31. for (int count = 2; count <= num; count++ )
  32. {
  33. print = isPrime(count); //Creates "print" function to determine if a number is prime.
  34. if (print == true)
  35. {
  36. cout << count << ", "; // outputs prime number
  37. }
  38. }
  39. }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /* function getMaxPrime(int num) returns the greatest prime number between 1 and num.
  48. if there is no prime number between 1 and num, getMaxPrime() should return a 0.
  49. */
  50. int getMaxPrime (int num)
  51. {
  52. int maxPrime;
  53. int prime = 2;
  54. if (num <= 1) //sets maxPrime to "0" (none)
  55. {
  56. maxPrime = 0;
  57. prime = 0;
  58. }
  59.  
  60. do //checks if a number is prime, sets maxPrime equal to found primes.
  61. {
  62. bool check = isPrime(prime);
  63. if (check == true)
  64. maxPrime = prime; //sets maxPrime to last prime number found in range.
  65. prime ++;
  66.  
  67. }while (prime <= num);
  68. return (maxPrime); // returns the value of maxPrime to main function
  69. }
  70.  
  71.  
  72.  
  73.  
  74. int main ()
  75. {
  76. int number;
  77.  
  78. do
  79. {
  80. cout <<endl<<endl
  81. <<"Please enter a number, 0 or negative to stop :";
  82. cin >> number;
  83. if (number > 0)
  84. {
  85. cout << "All the prime numbers between 1 and "<< number<<" are:"<< endl;
  86. printPrimes(number);
  87. cout << endl;
  88. cout << "The max prime is :" ;
  89. int maxPrime = getMaxPrime( number );
  90. if (maxPrime<=1) cout <<"none.";
  91. else cout << maxPrime;
  92. }
  93. } while (number>0);
  94. return 0;
  95. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
coopedw is offline Offline
3 posts
since Mar 2006
Mar 3rd, 2006
0

Re: (Noob) need some help w/ Prime Numbers

Do you know what is the definition of the prime number? It is a number that has no other diveders other than one and itself. You tested the number to itself (every number devides it self, doesn't it?) so that is why the function always return false.
If the number isn't prime it must have deviders to (inclusively) its square root. Think about it, and say why.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
brahle is offline Offline
18 posts
since Mar 2006
Mar 4th, 2006
0

Re: (Noob) need some help w/ Prime Numbers

If you exclude number 2, and start you for loop with 3, you need to check only odd numbers, increment you for loop by 2.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Installing the SDK for VC++ 2005???
Next Thread in C++ Forum Timeline: Why program works in Dev-C++ and not in VC++ 2005?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC