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

1. 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. Have you read the homework policy?

3. 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.

4. Originally Posted by BillyTKid
Have you read the homework policy?
Yes I have, I submitted my example. I can read. Can you?

5. Originally Posted by stahta01
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. It may also help if you mentioned how does it not work, e.g., what is the expected output and actual output.

7. Originally Posted by laserlight
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. 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. 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. 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):
Originally Posted by laserlight
It may also help if you mentioned how does it not work, e.g., what is the expected output and actual output.

10. 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.

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.

11. 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. 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.

13. Originally Posted by laserlight
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