Thread: Program that stores first 47 prime numbers in an array and prints them

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

    Program that stores first 47 prime numbers in an array and prints them

    What have I done wrong here? Again syntax or something else?

    Code:
    #include <stdio.h>
    
    int main()
    {   
    	int array[47]; /*Sets array to 47*/
    	int primeNumber = 1; /* declare and initialise the starting point of the 
    	search for primes */
    	int count=0;
    	while(primeNumber < 230)
    	{
    		int loopGuard = 2; /* initialise the loop guard */
    		int numDivisors = 0; /* variable representing the number of divisors 
    								a number has */
    		while(loopGuard < primeNumber) /* continue to perform the statements 
    		while the loopGuard is less than or equal to the current number 
    		(loopGuard) */
    		{
    			
    			if(primeNumber % loopGuard == 0) /* if the given number is divisable
    			by the current number (loopGuard) */
    			{
    				numDivisors =numDivisors + 1; /* increase our count of the 
    				number of divisors */
    			}
    			/* always increment the loopGuard variable */
    			loopGuard = loopGuard + 1; 
    		}
    		/* if there are no divisors for the number it must be prime */ 
    		if(numDivisors == 0){
    		array[count]=primeNumber;
    		}
    		count++; /*Count adds one*/
    		primeNumber = primeNumber + 1; /* increment the loop guard i.e.
    			test if the next number is prime */
    	}
    	count=0;
    	while (count < 47) /*While count less than 10*/
    
    
    		{
    			
    		printf("%d ", array[count]); /*Prints numbers according to the position list[1], [2], [3] etc*/
    		
    		count++; /*Count adds one*/
    		
    		}
    	return 0;	 
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Have you read the homework policy?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Very poor variable names; primeNumber is not a prime number very often.

    Starting at 1 is normally considered bad design. 2 or sometimes 3 is used more often.

    Last while loop is better as a for loop.

    Using 47 as a literal number is considered poor style by most instructors.

    Tim S.
    Last edited by stahta01; 10-27-2011 at 10:04 AM.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by BillyTKid View Post
    Have you read the homework policy?
    Yes I have, I submitted my example. I can read. Can you?

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by stahta01 View Post
    Very poor variable names; primeNumber is not a prime number very often.

    Starting at 1 is normally considered bad design. 2 or sometimes 3 is used more often.

    Last while loop is better as a for loop.

    Using 47 as a literal number is considered poor style by most instructors.

    Tim S.
    LOL, my lecturer actually wrote it!!! I was trying to adapt it slightly. The errors you pointed to are his, not mine, confirms what I thought about him...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It may also help if you mentioned how does it not work, e.g., what is the expected output and actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by laserlight View Post
    It may also help if you mentioned how does it not work, e.g., what is the expected output and actual output.
    That it stores the first 47 numbers in an array and prints them. Not being a d**k, but I put that in the subject title. It compiles but does nothing.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Next time be more specific in describing your problem. Something like "I get some prime numbers, but mostly garbage when I print out the list". Read this: How To Ask Questions The Smart Way.

    I know they teach you to comment your code thoroughly, but you've gone overboard. Comments should only explain what isn't obvious from reading the code, or why you made the choices you did in writing code. You should get rid of all the comments for variable declarations. Proper variable names are a better way to indicate what a variable is for. Ditch the comments that explain your loop condition, since they don't say anything the code doesn't say, and the second one is just plain wrong. Ditch the comments about incrementing the variables and printing things out, that's more than obvious from the code.

    As for your problem of garbage numbers, you only want to increment count when you found a prime number, not every time through your loop.

    @BillyTKid: How does this violate the homework policy? Perhaps you mean to tell the OP that they need to ask a better question.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Interista View Post
    That it stores the first 47 numbers in an array and prints them. Not being a d**k, but I put that in the subject title. It compiles but does nothing.
    Your subject line told us what it's supposed to do. What you should have focused on in laserlight's response is (emphasis mine):
    Quote Originally Posted by laserlight View Post
    It may also help if you mentioned how does it not work, e.g., what is the expected output and actual output.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Interista
    That it stores the first 47 numbers in an array and prints them. Not being a d**k, but I put that in the subject title.
    Sure, but since you did not state how it does not work, it could well be that the problem is that the numbers are printed, but in a wrong format.

    Quote Originally Posted by Interista
    It compiles but does nothing.
    What do you mean by "does nothing"? For example, a quick test for me shows that the program crashes, and this is explained by anduril462's observation that you are incrementing count wrongly: you end up writing to the array named array out of bounds.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Thanks. I'll work away at that for a while and see where I get.

    Incidentally, does this mean my suspicions about my lecturer not exactly being the best are confirmed?

    I mean what you've all criticised is what he wrote...

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Interista
    Incidentally, does this mean my suspicions about my lecturer not exactly being the best are confirmed?
    Well, I don't know about his teaching ability, but if the code given is really what he wrote, then he's pretty sloppy, both in programming and failing to test example programs before handing them out.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by laserlight View Post
    Well, I don't know about his teaching ability, but if the code given is really what he wrote, then he's pretty sloppy, both in programming and failing to test example programs before handing them out.
    Being fair, his program did work, I was modifying it slightly to store numbers into the array, but many/most of the problems you (and other posters) highlighted were his.

    Oddly that convinces me more to battle on despite him and do the best I can, then when I hopefully pass I can move on to a better teacher.

    Trust me, I know his teaching style is poor, given my background I should know what a good teacher is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-30-2010, 10:08 PM
  2. Program the prints prime numbers
    By cloudstrife910 in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2010, 03:03 PM
  3. Replies: 12
    Last Post: 02-28-2008, 06:19 PM
  4. Prime Numbers and Array...
    By Deux in forum C Programming
    Replies: 12
    Last Post: 12-20-2004, 04:12 PM
  5. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM