473,320 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Prime Checker C++

Hi
I am a beginner in C++ and would like someone to give me an idea or push me in the right direction of how to solve this question , It is related to classes

Design and implement a class called PrimeChecker to check if a number is
prime. The class stores an array of consecutive primes. It uses this array
to check if a given number is prime. To simplyfy the excercise the array
will store at most 1000 prime numbers. The class has the following public
methods:
(a) Constructor
The array to store up to 1000 primes is created and the prime number
’2’ and ’3’ are stored in side the array.
(b) Destructor
(c) IsPrime() which returns a boolean and takes a parameter which is
an integer.
Note: Whether a number n is prime can be checked by testing divis-
ibility of all primes from 2 to the √n.
Check if the array stores all needed prime numbers and extend place
more consecutive prime numbers into array if needed.
(d) printAllKnownPrimes() which prints all the primes stored in the ar-
ray using cout.


Thank you in advance =)
Oct 30 '08 #1
3 2996
Ganon11
3,652 Expert 2GB
What work have you done so far? How far have you gotten in this class?

Could you make a prime checker without a class (a.k.a. make a function that checks if a number is prime?)
Oct 30 '08 #2
What work have you done so far? How far have you gotten in this class?

Could you make a prime checker without a class (a.k.a. make a function that checks if a number is prime?)

I got

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. class PrimeChecker {
  4.  
  5. private:
  6. int primes[1000];                         // defining the array
  7. //const int MAX_PRIMES=1000;
  8.  
  9. int i;                                     // defining i , the position of the prime in the array
  10.                                     // defining x, the number input into the program
  11.  
  12.  
  13.  
  14. public:
  15. //int primes();
  16. PrimeChecker (); 
  17. ~PrimeChecker();
  18. void printAllKnownPrimes() ;
  19. bool IsPrime(int x);
  20. //bool IsPrimes (int i);
  21. };
  22.  
  23.  
  24. PrimeChecker::PrimeChecker(){
  25. primes[0] =2;
  26. primes[1] =3;
  27. i=1;
  28. }
  29.  
  30. PrimeChecker::~PrimeChecker(){};
  31. int x;
  32. bool PrimeChecker::IsPrime(x){
  33. int SQ = sqrt(x)
  34.  
  35. for ( n=primes[i] + 1; n<SQ; a++){
  36. if (n%primes[i]==0)
  37. primes[i+1] = n;
  38. i++;
  39. }
  40. }
  41. return IsPrime(x);
  42. }
  43.  
  44. bool PrimeChecker::IsPrimes(x) {
  45.   for (x=2;x<input;x++) {
  46.    if (input%x=0){
  47.          return false;
  48.   }
  49.  }
  50. return true;
  51. }
  52.  
  53.  
  54.  
  55. }
  56.  
  57.  
  58. //void printAllKnownPrimes() {
  59. //cout << "All primes are " ;
  60. //for (int i=0;i<n;i++){
  61. //cout << primes[i]; }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. int main(){
  69.     PrimeChecker p;
  70.     int x;
  71.     while(1){
  72.         while(x<2||x>62710561,i++) {
  73.             cout<<"Please input x between 2 and 1000 "<<endl;
  74.             cin>>x;
  75.             if(x==0) break;
  76.         }
  77.         if(x==0) break;
  78.  
  79.         if(p.IsPrime(x)) cout<<x<<" is a prime"<<endl;
  80.         else cout<<x<<" is not a prime"<<endl;
  81.         p.AllKnownPrimes();
  82.  
  83.         cout<<"Please input x in [2,62710561] or 0 for quit:"<<endl;
  84.         cin>>x;
  85.         if(x==0) break;
  86.     }
  87.     getchar();
  88.            return 1;
  89.     }
  90. }

DOesnt compile though
Oct 31 '08 #3
donbock
2,426 Expert 2GB
I got
<code snipped for brevity>
Doesn't compile though
Please provide any compiler errors/warnings, being careful to translate line numbers as needed so they refer to the source code as you posted it.

You're asking for people to volunteer their time to assist you. Make it easy for them to help you.

Please don't take my snippiness personally -- you just had the misfortune to be here when I boiled over.
Oct 31 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
0
by: ETM11871 | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
13
by: kolmogolov | last post by:
/* Hi, I have removed things irrelevant to reproducing the problem. What's wrong with my code? Thanks in advance for any hint! 1166425362 */ #include <stdio.h> #include <stdlib.h> #include...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
6
by: Neil | last post by:
Is there way to have control over the MS-Access spell checking (besides just launching it)? We want to tell it to check all records, but skip certain fields (or, alternatively, ONLY check certain...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.