Using classes and arrays

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>

using namespace 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.
int myarray[];

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

class MyArray
{
private:
    int * array ;
    unsigned arraySize ;

public:
    explicit MyArray( unsigned size ) 
        : array( new int[size] ), arraySize(size) {}

    ~MyArray() { delete [] array ; }

};

int main()
{
    unsigned size ;
    std::cout << "Enter the size: " ;
    std::cin >> size ;

    MyArray a(size) ;
}


Last edited on
is there a way to do it without using pointers?

This assignment wants me to create an array of n number of prime numbers (n being determined by the user in main with the stipulation 1<n<=40000)
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.    
}


http://www.cplusplus.com/reference/vector/vector/
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

If your assignment calls for you to roll up an array class, however, it's probably intended you use pointers and dynamic memory.
cant use vectors or pointers

I guess we are supposed to create an array of the max numbers(4000) then fill it with a user defined amount of primes.

I had to erase all the vector stuff because of this and not sure how I can do this

This is my header so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Prime number tester class specs
#ifndef PRIMENUMBERTESTER_H
#define PRIMENUMBERTESTER_H

#include <iostream>

//Tester class declarations

class PrimeNumberTester{
private:
	int const 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++;
		}


}

assignment is here if anyway feels like reading a wall of text http://pastebin.com/xAcExC9m
Last edited on
Just fyi, in PrimeNumberTester::PrimeNumberTester(int primeNumberList[], int max) primeNumberList is a pointer.

I would suggest something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class PrimeNumberTester
{
    const unsigned MAX_PRIMES = 4000 ;
    unsigned primes[MAX_PRIMES] ;
    unsigned primesFound ;

public:

    enum Primality { NotPrime, Prime, Undetermined } ;

    PrimeNumberTester( unsigned primesToFind ) ;

    Primality isPrime(unsigned value) ;
};


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.
Last edited on
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.
Last edited on
So I am not understanding using isPrime to test an already filled array of primes.

Really?

But I need to fill an array with only primes,

Hello, constructor.


Then use that filled array to test a user imputed number to see if it is prime.

Hello, isPrime.
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.
Thank you cire and LowestOne for your help.
Topic archived. No new replies allowed.