TLEJ Posted March 10, 2012 Report Share Posted March 10, 2012 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.. #include "stdafx.h" #define size 1000 int main() { int arr[size], i, j=1, remainder, primecount=0, max=size; for(i = 0;i < size; ++i) { arr[i] = j++; } for(j = 2; j <= size; j++){ for(i = j; i < size; i++){ remainder = arr[i] % j; if(remainder == 0) arr[i] = 0; } } for(i=0;i<size;i++){ if (arr[i]!=0) primecount++; } for(i = 0; i < size; i++) { printf("\n %d ",arr[i]); } printf("There are %d prime numbers up to %d.\n",primecount,max); return 0; } Quote Link to comment Share on other sites More sharing options...
rusty959 Posted March 20, 2012 Report Share Posted March 20, 2012 If for some reason you are still working on this 11 days later... Why remove them? You could just add a single if statement and only print it if the value in the array isn't 0. At least this is the easiest way. You can't simply "remove" them from the array, you would have to make a new array the size of the number of primes you have and copy the primes over. Quote 2013 Subaru BRZ12" Orion HCCA on AQ 22001967 mustang restoration project: http://www.stevemeadedesigns.com/board/topic/110765-mustang-restoration-build-2011-edition/ Youtube: https://www.youtube.com/user/therusty959 Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.