abotr trap 6 mac os x

I am tackling an exercise that finds prime numbers under 1000. It compiles ok but terminates with abort trap 6 and I do not know why?
Can anyone please help!!

I was thinking off changing from a bool array to int vector andn setting all elements to 1...?

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
#include<iostream>
#include<vector>


int main()
	{
		bool primeNumbers[1000];
		
		
		//set all elements to false in array
		for (  unsigned int i = 2; i <= 1000; i++ )
			{
				primeNumbers[i] = true;
			}
			
		
		//step through array
		for ( unsigned int i = 2; i <= 1000; i++ )
			{
				if ( primeNumbers[i] )
					{
						for ( int j = i + 1; j < 1000; j++ )
							{
								if ( j % i == 0 )
									{
										primeNumbers[j] = false;
									}
							}
					}
			}
			
		for ( unsigned int i = 2; i <= 1000; i++ )
			{
				if ( primeNumbers[i] )
					{
						std::cout << i << " ";
					}
			}
			
		std::cout << std::endl;
	
	
	return 0;
	
	}
Line 11 (and others) i <= 1000
this will access primeNumbers[i] with an out of range subscript. Remember array elements are numbered starting from 0, so valid subscripts would be 0 to 999.

Change the <= to < i <= 1000 to i < 1000

Also, rather than having the number 1000 appearing multiple times throughout the program, use a defined constant.

1
2
const int size = 1000;
bool primeNumbers[size];


for ( unsigned int i = 2; i < size; i++ )

etc.

Thanks very much!! Another school boy error :(

Thanks again!!
Topic archived. No new replies allowed.