Hi. I am trying to get this into my head but having difficulty. I have searched and searched how to do the following but can't find anything.
This is what I have so far in my .h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef HEADER_H
#define HEADER_H
class myclass{
private:
int myarray[];
public:
myclass(int size){
myarray[size];
}
~myclass(){
}
This my main code so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "Header.h"
#include <iostream>
usingnamespace std;
int main(){
int size;
cout<<"Please inter size of array:";
cin >>size;
myclass.
return 0;
}
I haven't even started on the implementation file, I will eventually be doing an assignment that will build a class that will be used to test whether or not a number is prime it will also create and hold an array of prime numbers which will be used to determine if a number is prime
I am having trouble using a class constructor to generate an array any pointers would be greatly appreciated.
This is illegal. If you want to declare an array, you must declare it with a size that is also a compile time constant. If you want to allocate the memory in the constructor, you'll need to use dynamic memory.
You could use a vector. The class wouldn't be needed in that case.
Alternately, since you know the maximum size, you could go with an array of that size and just use what you needed of it (although that would be a hefty sized variable for automatic storage.) A vector would be my recommendation.
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <vector>
int main()
{
unsigned size ;
std::cout << "Enter the size: " ;
std::cin >> size ;
std::vector<unsigned> primes(size) ; // vector of size elements.
}
// Prime number tester class specs
#ifndef PRIMENUMBERTESTER_H
#define PRIMENUMBERTESTER_H
#include <iostream>
//Tester class declarations
class PrimeNumberTester{
private:
intconst MAX=4000;
int primeNumberList[MAX];
public:
PrimeNumberTester(int primeNumberList[], int max);
~PrimeNumberTester();
};
#endif
then my function implementation is, I know its wrong but I am not sure how to fix it
1 2 3 4 5 6 7 8 9 10 11
PrimeNumberTester::PrimeNumberTester(int primeNumberList[], int max){
int size=0;
//primeNumberList[0]=2;
for(int count=0; count<max; ++count){
primeNumberList[size]=count;
size++;
count++;
}
}
Where you calculate the number of primes specified in the constructor's parameter and store that value in primesFound for reference.
isPrime would handle the logic for testing a number for primality after the array is filled, returning NotPrime if the number isn't prime, Prime if the number is prime, and Undetermined if the square root of value is greater than the largest prime calculated and it is not evenly divisible by any of the primes calculated.
But I need to fill an array with only primes, the amount of primes is user defined in main. Then use that filled array to test a user imputed number to see if it is prime. So I am not understanding using isPrime to test an already filled array of primes.
The constructor only needs one parameter, the user defined max. Your class needs to remember that for later. The only time you need MAX is when you declare the array and perhaps to make sure the user amount isn't greater than 4000.
It's tough to post code without giving the answer.
You need one loop that increments the current index of the array so that it can store the next prime. The trick is to only increment the index when the current number you are figuring out is prime.