Like Tree1Likes
  • 1 Post By stahta01

Issue storing Prime numbers in an array and then prinitng

This is a discussion on Issue storing Prime numbers in an array and then prinitng within the C Programming forums, part of the General Programming Boards category; Hi I am trying to write a program that generates the prime numbers under a 1000. The program itself works ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    16

    Issue storing Prime numbers in an array and then prinitng

    Hi I am trying to write a program that generates the prime numbers under a 1000. The program itself works fine until i edit it and try to store the values in an array and then print that array. I know im pretty close to fixing this but i cant seem to find where im going wrong. Any help would be great

    Code:
    #include <stdio.h> 
    int main ()
    {
    int array[1000];
    
    
    int j=0;
    
    
    int i = 0;		/*assigns value to i*/
    	
    printf("The following numbers are prime:\n\n");
    	
    	while (i < 1000){
    		
    		int	num = 2;
    		
    			int non = 0; 
    					
    	while ( num < i ){
    
    
    		if ( i%num == 0) { /*true statement means number is not prime */
    								/*adds 1 to value of i in each time*/
    		non++;
    }
    				
    		num++;
    	}	
    				
    		if (non ==0){
    				
    				
    			scanf("%d",&array[i]);/*prints the prime number for the user */
    }
    			i++;/*adds 1 to value of count*/
    }
    i=0;	
    
    	
    
    
    
    	while (i<=1000){
    	
    	printf("%d",array[i]);
    	
    	i++;
    	
    	}
    		
    return 0;	/*Terminates program*/
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Your scanf line is waiting for user input. Its not adding elements to the array, which I think is what you were wanting. But just fixing that isn't going to solve all your problems. I think you have faulty logic in your algorithm.
    Last edited by DaveH; 11-04-2011 at 08:10 AM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    63
    you are not at all storing prime numbers into array.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    I understand that im not scanning in the prime numbers and like you said its a week program overall i know that but i would prefer if i could fix it then make it sparkle lets say!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Once you know a number is prime, you need to put it into your array and increase the index for the next number...
    Code:
    array[index++] = prime_number;
    If you use more meaningful variable names the function of your code would be more apparent to you.

    Also... there are considerable optimizations you can do here... First of all we all know that 1, and 2 are prime numbers so you can start your search at 3. Second all prime numbers are odd numbers (even numbers are divisible by 2) so you can increment your counter by 2 instead of 1.
    Last edited by CommonTater; 11-04-2011 at 09:03 AM.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    @commontater thanks for your advice i see how it would make it much neater and have found similar code on these forums and have changed my variable names etc to that code as i deemed it to be better. however when i compile the program it prints out random numbers but it is printing from the array which is progress!thanks again

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Are you in the same class as Interista?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,506
    Quote Originally Posted by CommonTater View Post
    First of all we all know that 1, and 2 are prime numbers so you can start your search at 3.
    FYI: Most people consider 2 to be the first prime.

    Prime number - Wikipedia, the free encyclopedia
    A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
    The first 1000 prime numbers http://primes.utm.edu/lists/small/1000.txt

    Tim S.
    Last edited by stahta01; 11-04-2011 at 11:16 AM.
    laserlight likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  2. Storing using fgets issue
    By ccc123 in forum C Programming
    Replies: 3
    Last Post: 05-30-2011, 11:41 AM
  3. prinitng 2D array form pointer ????
    By fcommisso in forum C Programming
    Replies: 6
    Last Post: 11-02-2009, 08:58 PM
  4. Storing large numbers as an array
    By Rush152 in forum C Programming
    Replies: 9
    Last Post: 05-19-2006, 11:51 PM
  5. Prime Numbers and Array...
    By Deux in forum C Programming
    Replies: 12
    Last Post: 12-20-2004, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21