Adding and comparing with a vector also whole numbers

What i want the program to do is..

3/2 = Add 3 to a vector since dividing it by 2 gives an uneven number.
4/2 = Discard 4 since 2 is an whole number.
5/2 = Uneven number, check vector for additional numbers. Find 3, check if 5/3 will produce an whole number. It does not, add 5 to vector
6/2 = Whole number found, discard.. And so on.


what i'm wondering is how do you access the vector once a new number has been added and how to check if a variable stores a whole number?
... how do you access the vector ...

http://www.cplusplus.com/reference/stl/vector/

... how to check if a variable stores a whole number?

http://www.cplusplus.com/doc/tutorial/operators/

(look for the "modulo" operator - %)
Last edited on
Sorry for my english. what i meant with whole numbers where numbers that when divided does not split like 5/2 = 2.5... did later realise that was not what it meant at all sorry :)

Anyways got a part of the program working having some trouble with the vector.


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
	vector<int> PrimNumbers(20);
	PrimNumbers[0] = 2;

	I = PrimNumbers[b];
	

	// cout << I << endl;
	

	


	for (double PrimSearch=3; PrimSearch < 50; PrimSearch++)
	{
		
		
		Divide = PrimSearch / I;

		Num = Divide;
		Check = Divide;



	
    if ( Num / Check == 1)
    {
      

    }
	else{
		cout << PrimNumbers[b] << endl;
		b++;
	     PrimNumbers[b] = PrimSearch;
		
	}


	}


what i need is for PrimSearch to be divided with all the numbers stored in PrimNumbers in the start it's just 2 but later it will become 3 5 7 and so on

so it would look like this:

PrimSearch / PrimNumber = Divide

3/2 = 1.5 store 3 in PrimNumber[1]
4/2 = 2
5/2 = 2.5 .. 5/3 = 1.666 store 5 in PrimNumber[2]
6/2 = 3
7/2 = 3.5 .. 7/3 = 2.333 .. 7/5 = 1.4 store 7 in PrimNumber[3]

So none of the variables in PrimNumbers can be divided with 7 so we also store 7 in PrimNumbers and on the next number that PrimSearch generate that can't be divided by anything already stored in PrimNumbers will from now on also be divided by 7

so in short when PrimSearch is divided with PrimNumber. all stored values in PrimNumber needs to be checked if any of them will give a prime number
Last edited on
Did you check the web pages I sent you the links for?

If so, you'd know that % (the module operator) gives you the remainder for integer division. So testing for 0 == value % 3 is the way to test that value is exactly divisible by 3.

3 % 2 evaluates to 1, as 3/2 = 1 R 1
6 % 2 evaluates to 0, as 6/2 = 3 R 0

Using % allows you to use ints everywhere. Including the loop.

You have to be careful when testing doubles for equality as they are not totally accurate. I can't see the types you are using for Num and Check, but if they're double the the test Num / Check == 1 might not always work.

As far as the vector goes, the code you posted is incomplete so I can't really comment. But based on your explanation, I suggest you stop pre-sizing the array and use push_back() -- see link I sent before, about vector.

1
2
	vector<int> PrimNumbers;
	PrimNumbers.push_back(2);


then use the vector's size() and operator[] -- see same link for more info

Andy

P.S. You are more likely to get a response if your code is clearly and neatly formatted. (Note that you can go back and tidy up code!)

Last edited on
Yes i did read the pages you linked.. Tried to change the code abit to look more like what you're saying allthought i'm very new to this so sorry if i'm having abit of trouble in understanding just exactly what you're saying :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	
vector<int>PrimeNumbers(Storeage);
	PrimeNumbers[0] = 2;

for(int PrimeSearch = 2; PrimeSearch < 50; PrimeSearch++)
{
			
      if(PrimeSearch % PrimeNumbers[0] == 1)
{
		cout << "test" << endl;
		PrimeNumbers[a] = PrimeSearch;
		a++;
}
else{
cout << "test" << endl;
}

cout << PrimeNumbers.size() << endl;
		
}



This part of the code i'm having some trouble with now with modulo in place it eliminates about 25 of the non prime numbers which is great but i need to compare if any of the numbers stored in PrimeNumbers can be divided with those 25 and generate an whole number and if so to discard them not just the number 2 and not just the latest value found either needs to be ALL numbers stored. could you perhaps post an exampel of how to divide every value of PrimeNumbers with PrimeSearch and check if any of them generates a whole number?

1
2
3
4
5
6
if(PrimeSearch % PrimeNumbers[0] == 1)
{
	cout << "IFtest" << endl;
	PrimeNumbers[a] = PrimeSearch;
	a++;
}






could look something like this

 
if(PrimeSearch % PrimeNumbers[0] == 1 && PrimeSearch % PrimeNumbers[1] == 1 && PrimeSearch % PrimeNumbers[2] == 1)


allthought this would not work since from start there is no values stored in PrimeNumbers execpt for PrimeNumbers[0] so the program would be dividing by 0

Last edited on
You need to use another loop rather than a long line of tests.

Before continuing with your original problem, I suggest you get a bit of practice with loops, vectors, and %:

1. Look at the vector calls I mentioned and have a go at populating a vector (with push_back) and then listing its contents (with size, and [], and a for loop).

2. Look at operator%

3. And read up about the break statement
http://www.cplusplus.com/doc/tutorial/control/

Then go back to your original problem!
Last edited on
Topic archived. No new replies allowed.