I already made a working code for this problem (Problem 10), but it took about an hour before it was done calculating.
So I did a little research on the internet and found out about the sieve of Eratosthenes. So I made a program that would calculate the answer to this problem in this manner. It works fine on smaller numbers, but it crashes when I input 2 million, and it gives a negative result at 1 million, so I'm guessing it has something to do with the variablesize being too small, though I have no idea how to fix it...
constint n = 2000000; //highest number tested
char prime[n];
The stack is a scarce resource, so you should consider allocating large blocks of memory like that from the heap.
Part of the problem with the stack is that the size is determined by the linker, and not the compiler so you just don't know how large it is. In your case, you know the context in which your function is called, but in general you don't know. So even if you're code's run in an environment where there's plenty of stack space, you could be called when a lot of it is already used. So it is really important to use as little stack as is reasonable, and avoid the use of alloca().
Also, as the number of primes in a fixed range decreases as the numbers get larger, it may be more efficient to store the actual prime numbers rather than storing a flag against a huge range of numbers. Of course, this particular algorithm doesn't lend itself to that naturally.
I had never heard of the heap and the stack, until now. After changing the code a little bit, the program doesn't crash anymore.
I changed line 11 to: char *prime = newchar[n];
and I also changed nTotal to a long long, instead of an integer. (since I noticed 4 bytes wouldn't be enough)
this gave me the correct result. Thank you for your help everyone :)