473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prime Checker C++

2 New Member
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) printAllKnownPr imes() which prints all the primes stored in the ar-
ray using cout.


Thank you in advance =)
Oct 30 '08 #1
3 3030
Ganon11
3,652 Recognized Expert Specialist
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
ace2606
2 New Member
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 Recognized Expert Top Contributor
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
8396
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, but a prime number tester would also be nice. I'm dealing with numbers in the 10^6-10^8 range so it would have to fairly efficient Dag
9
2716
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 (testPrimes.py) with two algorithms that both check for primes by testing only odd numbers using factors up to the square root of the value, where Primes1 is based on all of the existing primes so far, and Primes2 is based on all odd numbers, I would...
11
7157
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 the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is prime....
0
1725
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 complicated. we are on a functions chapter. i am usung the square root to find the prime. so far i have accomplished this but i cant fgure how to divide this by ten so i can find the right prime. i am using c. #include <stdio.h> #include <math.h> ...
25
3884
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 complicated. we are on a functions chapter. i am usung the square root to find the prime. so far i have accomplished this but i cant fgure how to divide this by ten so i can find the right prime. i am using c. #include <stdio.h>
13
2686
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 <string.h>
60
1950
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 ending in 5 ..which obviously cannot be prime numbers) can anyone please help me out with the reason?? /*Generate Prime Numbers*/ #include<stdio.h>
7
4901
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
6
10829
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 fields). Is that possible? Alternatively, if that's not, we noticed that the spell checker skips fields that are disabled. So one could disable the fields to be skipped; run the spell checker; and then re-enable those fields when done. But how...
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.