1. ## beginner

i am brand new to coding, but i can nver get brackets right {}

Will some one please explaing this to me?

/************************************************** ************************************************** *
Filename: pgen.c
Author: Mike Hartwig
Purpouse: Generate given amount of prime numbers.
Input: Keyboard
Output: Screen
************************************************** ************************************************** */
#include <stdio.h>

int main() {

int prime_amount; //the amount of primes to generate.
int prime_start = 2; //the number to start generating primes at.
int prime_count; // the count of numbers we already have.

printf("Welcome to the Prime Number Generator.\n");
printf("How many prime numbers would you like to generate: ");
scanf("%d", &prime_amount);
printf("Generating %d prime numbers...\n", prime_amount);

do (prime_count < 2) {

prime_answer = prime_start + prime_count * 2

} while

return (1);

}

return (0);

2. ## Re: Beginner

Ok, just a small mix up in your do while loop.

It should look like this:

#include <stdio.h>

int main() {

int prime_amount; //the amount of primes to generate.
int prime_start = 2; //the number to start generating primes at.
int prime_count; // the count of numbers we already have.

printf("Welcome to the Prime Number Generator.\n");
printf("How many prime numbers would you like to generate: ");
scanf("%d", &prime_amount);
printf("Generating %d prime numbers...\n", prime_amount);

do {

prime_answer = prime_start + prime_count * 2

}
while(prime_count < 2);

return (1);

}

return (0);

PS, I'm a bit rusty so if anyone sees this code and notices anything wrong, correct me, but it looks like it should be right.

-AJ

3. i get this. There is something wrong with my squiggles {}. If somebody could explain to me exactly where and when to put them. It would be great

C:\Program Files\Microsoft Visual Studio\MyProjects\pgen\pgen.c(34) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\MyProjects\pgen\pgen.c(37) : error C2061: syntax error : identifier 'printf'
C:\Program Files\Microsoft Visual Studio\MyProjects\pgen\pgen.c(46) : error C2059: syntax error : 'return'

4. ## Re: Beginner

Ok, try this:

#include <stdio.h>
#include <stdlib.h>

int main() {

int prime_amount; //the amount of primes to generate.
int prime_start = 2; //the number to start generating primes at.
int prime_count; // the count of numbers we already have.

printf("Welcome to the Prime Number Generator.\n");
printf("How many prime numbers would you like to generate: ");
scanf("%d", &prime_amount);
printf("Generating %d prime numbers...\n", prime_amount);

do {

prime_answer = prime_start + prime_count * 2

}
while(prime_count < 2);

return (1);

}

return (0);

}

5. What I'm usually doing with { and } is placing them on the same colum. So

Code:
```while (condition)
{
/* some code */
}```
In this way, it is much easier to find {}-pairs. A lot of programmers write:

Code:
```while (condition)
/* a statement */```
When more code is needed, you can easily forget the brackets. So it is a good style to do this:

Code:
```while (condition)
{
/* a statement */
}```

Code:
```#include <stdio.h>
#include <stdlib.h>

int main ()
{
int prime_amount; //the amount of primes to generate.
int prime_start = 2; //the number to start generating primes  at.
int prime_count; // the count of numbers we already have.

printf("Welcome to the Prime Number Generator.\n");
printf("How many prime numbers would you like to generate: ");
scanf("%d", &prime_amount);
printf("Generating %d prime numbers...\n", prime_amount);

/* Shiro: you need to initialise prime_count before using it */
prime_count = INITIAL_VALUE;

do
{
/* Shiro: you forgot a ; at the end */
prime_answer = prime_start + prime_count * 2;
}
while (prime_count < 2);

/* Shiro: usually 0 is used as "everything went OK" value */
return 0;
}```

6. Problem :

Code:
```    do
{
/* Shiro: you forgot a ; at the end */
prime_answer = prime_start + prime_count * 2;
}
while (prime_count < 2);```

Looks like an infinite loop since your testing
prime_count without ever changing it within
the do while loop
-------------------------------------------------------

7. You should check your last printf statement
You should omit the & otherwise you might get some funny answer.

8. Thanks ginoitalo. There can be an infinite loop. The variable prime_count doesn't change so prime_count < 2 will always be TRUE or FALSE, depending on the INITIAL_VALUE.

Since prime_count is the actual number of primes and prime_amount the requested number of primes it should be:

Code:
```do
{
/* Shiro: you forgot a ; at the end */
prime_answer = prime_start + prime_count * 2;
}
while (prime_count++ < prime_amount);```
Therefor I would initialise prime_count to 0.

By the way, this program doesn't generate primes. If that was the purpose of the program.

9. The braces {} are used to establish a block of code and they establish the scope of the variables and objects defined inside them.

10. hey if i omit the and in the printf statement i get a warning about print_answer not being initailized.

11. i got this now.

/************************************************** ************************************************** *
Filename: pgen.c
Author: Mike Hartwig
Purpouse: Generate given amount of prime numbers.
Input: Keyboard
Output: Screen
************************************************** ************************************************** */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int prime_amount; //the amount of primes to generate.
int prime_count = 1; // the count of numbers we already have.
// int prime_start = 2;

printf("Welcome to the Prime Number Generator.\n");
printf("How many prime numbers would you like to generate: ");
scanf("%d", &prime_amount);
printf("Generating %d prime numbers...\n", prime_amount);

do
{

}
while (prime_count++ < prime_amount);

return (0);
}

12. Still one thing. I would initialise prime_count to 0, because of (prime_count++ < prime_amount).

Assume prime_amount is N. Then you run from 0 to N-1. Which is in fact N prime numbers. You are initialising prime_count as 1, so you are running from 1 to N-1, which results in N-1 primenumbers, but you want N prime numbers.

Or do (prime_count++ <= prime_amount). This would lead to running from 1 to N.

Popular pages Recent additions