Very simple question..

I am trying to write a program that stores all the prime numbers up to 1000 in an array.. I can do that.. but when it's doing the check to see if it's prime, if it isn't, it just sets it to 0. So when I try to print out all my prime numbers, it prints all the 0s out with it. How do I remove the 0s from the array? Or what's a better way to do this..
Last edited on
why not add a conditional that checks if arr[i] == 0 to decide whether to cout anything?

EDIT: ...to decide whether to printf() anything.
Last edited on
I just thought of that about 5 seconds ago.. Not sure how I couldn't come up with that earlier..

1
2
3
4
5
  for(i = 0; i < size; i++)
    {
	if (arr[i]!=0)
		printf("%d ",arr[i]);
    }


But how would I just not store it in the array to begin with?
Last edited on
Lej wrote:
But how would I just not store it in the array to begin with?


You have to store something in each element of the array, or else the program will store something for you (usually whatever information happens to be at the memory locations at compile time - and more often than not, that garbage is not condusive to a healthy program)



Topic archived. No new replies allowed.