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.
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 :)
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--
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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.