-
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.
int prime_answer; // the answer.
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
printf("%d, \n", &prime_answer)
return (1);
}
return (0);
-
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.
int prime_answer; // the answer.
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);
printf("%d, \n", &prime_answer);
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
-
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'
-
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.
int prime_answer; // the answer.
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);
printf("%d, \n", &prime_answer);
return (1);
}
return (0);
}
-
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 */
}
Further some comments on your code:
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.
int prime_answer; // the answer.
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);
printf("%d, \n", &prime_answer);
/* Shiro: usually 0 is used as "everything went OK" value */
return 0;
}
-
Problem :
Quote:
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
-------------------------------------------------------
-
You should check your last printf statement
printf("%d,\n", &prime_answer);
You should omit the & otherwise you might get some funny answer.
-
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.
-
The braces {} are used to establish a block of code and they establish the scope of the variables and objects defined inside them.
-
hey if i omit the and in the printf statement i get a warning about print_answer not being initailized.
-
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_answer; // the answer.
// 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
{
printf("%d, ", prime_answer);
}
while (prime_count++ < prime_amount);
prime_answer = prime_count++;
return (0);
}
-
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.