Prime number using classes

Hello. I'm trying to write a program that finds the prime numbers from 3 to 100 while using a class--and having the array of numbers in the private member of the class. I figured out how to find the prime numbers without using a class but I can't figure out how to move it to make it work in a class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

class Prime
{
	int primeNumbers[100];
public:

void Prime::getPrime()
{
    for (int x = 3; x <= 100; x++)
    {
        bool prime = true;
        for (int y = 2; y <= (x - 1); y++)
        {
            if ((x % y) == 0)
            {
                prime = false;
                break;
            }
        }

        if (prime == true)
		{
		Prime::primeNumbers[x];
		}
		cout << Prime::primeNumbers[x] <<endl;
		
	}
}

};
int main()
{

	void getPrime();
	system("pause");

    return 0;
}


This is basically what I came up with, but it's obviously wrong because I get no output (which I'm also not sure why). Any help would be appreciated!
You aren't calling anything.

You've told the compiler that a Prime object will have a method called getPrime() which is fair enough (but you dont need 'Prime::' when your implementation is inside the class).

however, line 36...
remove the void and create a prime object it's like you're telling the compiler about another getPrime() method.

call getPrime() from your prime object

1
2
3
4
5
6
	Prime myPrimeObject;

	myPrimeObject.getPrime();
	system("pause");

	return 0;


your logic and syntax in getPrime() is messed up (again, why the 'Prime::' before your array?), but that should do you for now :)
Last edited on
Ah... thanks for pointing that out, mutexe. I was constantly changing things so I didn't notice those mistakes. I fixed it up and this is what I have now--

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

class Prime
{
	int primeNumbers[100];
public:
	void getPrime();
};

void Prime::getPrime()
{
    for (int x = 3; x <= 100; x++)
    {
        bool prime = true;
        for (int y = 2; y <= (x - 1); y++)
        {
            if ((x % y) == 0)
            {
                prime = false;
                break;
            }
        }

        if (prime == true)
		{
		Prime::primeNumbers[x];
		}
		cout << Prime::primeNumbers[x] << endl; 
	}
}


int main()
{
	Prime prime1;
	prime1.getPrime();
	system("pause");

    return 0;
}


However.. now I get a strange output of -858993460 ?? And it repeatedly prints itself.
indeed.

think about why the 'if' on line 25 never evaluates to true.

again, remove 'Prime::' from lines 27 and 29.

and line 27, that's not how to add a value to your array. if that's what you're trying to do.



Last edited on
and another thing to think about (sorry)

I'm trying to write a program that finds the prime numbers from 3 to 100


I know there are 25 prime numbers between 1 and 100, so why create an array with 100 elements?

Thanks for helping me mutexe!
I think I almost got it.. but I have a question.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

class Prime
{
	int primeNumbers[25];
public:
	void getPrime();
};

void Prime::getPrime()
{
	int i = 0;
    for (int x = 3; x <= 100; x++)
    {
        bool prime = true;
        for (int y = 2; y <= (x - 1); y++)
        {
            if ((x % y) == 0)
            {
                prime = false;
                break;
            }   

        
		}
	if (prime == true)
	{
		primeNumbers[i] = x;
	}
	cout << primeNumbers[i] << endl;
	}
}


int main()
{
	Prime prime1;
	prime1.getPrime();
	system("pause");

    return 0;
}


This is what I have now. When I run it, it outputs the prime numbers. However, it repeated each prime number multiple times. Is there something I did wrong? Sorry for asking so many questions. And I'm not sure if I added the values to the array properly.. but the output is better than the previous code!
no worries dude. glad to help. i'm getting bored doing documentation anyway :)

firstly delete line 31.

when your outer for loop has finished you now have a fully populated array with your primes in (as your starting from 3 there will be 24 values sorry, not 25).

What you could do then is iterate over you array print each value.
i've done it with a vector rather than an array, because i think arrays smell.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include <iostream>
#include <vector>

class Prime
{
	//int primeNumbers[100];
	std::vector<int> m_primesCol;

public:

	void getPrime()
	{		

		// build up the collection
		for(int i = 3; i <= 100; i++)
		{
			bool prime(true);

			for(int n = 2; n <= i - 1 ; n++)
			{
				if(i % n == 0)
				{
					prime = false;
				}

			}

			if(prime)
			{
				m_primesCol.push_back(i);
			}		
		}	

		// display AFTER your outer for
		DisplayPrimes();

	}

	void DisplayPrimes()
	{
		std::vector<int>::iterator it;

		for(it = m_primesCol.begin();it!=m_primesCol.end();++it)
		{
			std::cout << *it << " ,";
		}

		std::cout << "\n";
	}

};

int main()
{
	Prime myPrimeObject;
	myPrimeObject.getPrime();

	system("pause");

	return 0;
}

Last edited on
I would prefer to use vectors, but I think the array is a requirement for the assignment.
So far I have :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

class Prime
{
	int primeNumbers[25];
public:
	void Prime::getPrime()
	{
		int i = 0;
	    for (int x = 3; x <= 100; x++)
	    {
		    bool prime = true;
			for (int y = 2; y <= (x - 1); y++)
	       {
	           if ((x % y) == 0)
	           {
		            prime = false;
			        break;
			    }           
			}
			if (prime == true)
			{
				primeNumbers[i] = x;
			}
		}
		displayPrime();
	}	

	void displayPrime()
	{
		
		for (int i = 0; i <= 25; i++)
			{
				cout << primeNumbers[i] << endl;
			}
	}	
};

int main()
{
	Prime prime1;
	prime1.getPrime();
	system("pause");

    return 0;
}


It returns the value 97 for the first line, then returns a bunch of -858993460.
I am lost as to why it's doing this.
in that case do exactly what i've done but use an array rather than a vector :)


your issue now is that you are not incrementing the variable i when you add the prime to your array. in other words you are correcting calculating the primes but you are writing them ALL to the first element in the array.

if you want to do it like that, add an '++i' after line 24.

and in line 33 change the 25 to a 23, although you shouldnt be hard-coding numbers like that. What you could do is initialise all values in your array to 0 in your class constructor, then in your for loop in displayPrime iterate over the elements until you hit a zero. Then in theory it doesnt matter how large your array is.
Last edited on
Here's your working code Take A Look

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

class Prime
{
//	int primeNumbers[25];			//Old Code
public:
	void getPrime();
};

void Prime::getPrime()
{
//	int i = 0;						//Old Code
    for (int x = 3; x <= 100; x++)
    {
        bool prime = true;
        for (int y = 2; y <= (x - 1); y++)
        {
            if ((x % y) == 0)
            {
                if (x!=100)				//New Code
				{						//New Code
				x++; 	y=1;			//New Code
				}						//New Code
				else					//New Code
				{						//New Code
				prime = false;			//New Code
                break;					//New Code
				}						//New Code
				//prime = false;	//Old Code
                //break;			//Old Code
            }   

        
		}
	if (prime == true)
		std::cout << x << endl; 		//New Code
//	{								//Old Code
//		primeNumbers[i] = x;		//Old Code
//	}								//Old Code
//	cout << primeNumbers[i] << endl;//Old Code
	}
}


int main()
{
	Prime prime1;
	prime1.getPrime();
	system("pause");

    return 0;
}
Last edited on
@sandy
but I think the array is a requirement for the assignment


where is the array in your code?
Topic archived. No new replies allowed.