Velocity Reviews > having a problem with calling a function

# having a problem with calling a function

interpim
Guest
Posts: n/a

 12-16-2003
Im trying to teach myself programming, and I am doing exercises out of the book C by Dissection by Ira Pohl.
I am stuck on one of them and can't figure out how to fix it.

The exercise is for you to write a program that asks for a user inputted number and then print out that many prime numbers. The function that determines if a number is prime or not is provided.
My problem is that I can't figure out how to call the function repetitively and only print the numbers that return a true from the prime number function. What I have so far is this.

#include "primes.h"

main(void)
{
int x,y,n;

printf("PRIME NUMBERS WILL BE PRINTED\n");
printf("How many do you want to see? ");
scanf("%d",&x);
is_prime(n);
{
for(y=0;y<=x;++y)
printf("%5d: %5d\n",y,n)
}
}

here is is_prime.c

#include "primes.h"

int is_prime(int n)
{
int k, limit;

if (n == 2)
return 1;
if (n % 2 == 0)
return 0;
limit = n / 2;
for (k = 3; k <= limit; k += 2)
if (n % k == 0)
return 0;
return 1;
}

here is primes.h

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

int is_prime(int n);

Richard Heathfield
Guest
Posts: n/a

 12-16-2003
interpim wrote:

> Im trying to teach myself programming, and I am doing exercises out of the
> book C by Dissection by Ira Pohl. I am stuck on one of them and can't
> figure out how to fix it.
>
> The exercise is for you to write a program that asks for a user inputted
> number and then print out that many prime numbers. The function that
> determines if a number is prime or not is provided.

That function returns a value. Use it. (See below.)

> My problem is that I can't figure out how to call the function
> repetitively and only print the numbers that return a true from the prime
> number function. What I have so far is this.
>
> #include "primes.h"

You forgot:

#include <stdio.h>

This is required, because you are using printf.

>
> main(void)
> {
> int x,y,n;
>
> printf("PRIME NUMBERS WILL BE PRINTED\n");
> printf("How many do you want to see? ");
> scanf("%d",&x);
> is_prime(n);
> {
> for(y=0;y<=x;++y)
> printf("%5d: %5d\n",y,n)
> }

Your is_prime function doesn't work correctly for an input value of 1, so
change this to:

for(y = 2; y <= x; ++y)
{
if(is_prime(n))
{
printf("%d is prime\n", y);
}
else
{
printf("%d is composite.\n", y);
}
}

Now, main() returns int, so return an int from main():

return 0;

> }

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

David
Guest
Posts: n/a

 12-16-2003
Does this make sense?

main(void)
{
int x,y,n;

printf("PRIME NUMBERS WILL BE PRINTED\n");
printf("How many do you want to see? ");
scanf("%d",&x);
for(y=0;y<=x;++y)
{
n=is_prime(y);
printf("%5d: %5d\n",y,n);
}
}

David

On Tue, 16 Dec 2003 04:34:20 UTC, "interpim" <(E-Mail Removed)> wrote:

> Im trying to teach myself programming, and I am doing exercises out of the book C by Dissection by Ira Pohl.
> I am stuck on one of them and can't figure out how to fix it.
>
> The exercise is for you to write a program that asks for a user inputted number and then print out that many prime numbers. The function that determines if a number is prime or not is provided.
> My problem is that I can't figure out how to call the function repetitively and only print the numbers that return a true from the prime number function. What I have so far is this.
>
> #include "primes.h"
>
> main(void)
> {
> int x,y,n;
>
> printf("PRIME NUMBERS WILL BE PRINTED\n");
> printf("How many do you want to see? ");
> scanf("%d",&x);
> is_prime(n);
> {
> for(y=0;y<=x;++y)
> printf("%5d: %5d\n",y,n)
> }
> }
>
>
>
> here is is_prime.c
>
> #include "primes.h"
>
> int is_prime(int n)
> {
> int k, limit;
>
> if (n == 2)
> return 1;
> if (n % 2 == 0)
> return 0;
> limit = n / 2;
> for (k = 3; k <= limit; k += 2)
> if (n % k == 0)
> return 0;
> return 1;
> }
>
>
> here is primes.h
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int is_prime(int n);
>
>
>

Kevin Goodsell
Guest
Posts: n/a

 12-16-2003
interpim wrote:
>
> #include "primes.h"
>
> main(void)

Make this

int main(void)

"Implicit int" has been removed from C.

> {
> int x,y,n;
>
> printf("PRIME NUMBERS WILL BE PRINTED\n");
> printf("How many do you want to see? ");

If you want to be sure the user will see your prompt, you need to either
output a newline or flush the stream with fflush(stdout).

> scanf("%d",&x);
> is_prime(n);

Here you call a function. That function's purpose is to take an argument
and return a result based on that argument. Ask yourself 2 questions: 1)
What argument have you given it? 2) What have you done with the result?

The answers are that you have given it a garbage (indeterminate) value,
and thrown away the result. This is clearly not a useful thing to do.

> {

This is a spurious bracket. There seems to be no reason for it.

> for(y=0;y<=x;++y)

How many times will this loop be executed? How many times did you want
it to be executed?

> printf("%5d: %5d\n",y,n)

At this point n still does not have a useful value, so you will probably
print garbage (though technically the behavior is undefined, so there
are no restrictions on what may happen).

> }
> }
>

What you really want to do is:

1. Read in a number, n.
2. Create a counter, initialize to 0.
3. Iterate through integers, testing if each is a prime.
4. When a prime is found, print it and increment the counter.
5. Once the counter reaches n, terminate.

You obviously need to call is_prime() inside the loop, and decide
whether or not to print & increment the counter based on the result of
is_prime. Your loop might look something like this:

for (count=0, x=0; count<n; ++x)
{
if (is_prime(x))
{
/* print number, increment count */
}
}

I'm using the variable names differently than in your code - n is the
number of primes to print and x iterates through the integers.

>
> here is primes.h
>
> #include <stdio.h>
> #include <stdlib.h>

There doesn't seem to be a reason to include these here.

>
> int is_prime(int n);
>

-Kevin
--
My email address is valid, but changes periodically.

Dan Pop
Guest
Posts: n/a

 12-16-2003
In <brm855\$n96\$(E-Mail Removed)> Richard Heathfield <(E-Mail Removed)> writes:

>interpim wrote:
>
>> #include "primes.h"

>
>You forgot:
>
>#include <stdio.h>

Nope, he hasn't. It's in "primes.h", which you have snipped without

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)

interpim
Guest
Posts: n/a

 12-16-2003
I appreciate you guys helping me out.... I haven't tested it yet but the way Kevin layed it out, makes it easier for me to understand... Thanks again.

Christopher Benson-Manica
Guest
Posts: n/a

 12-16-2003
interpim <(E-Mail Removed)> spoke thus:

> Im trying to teach myself programming, and I am doing exercises out of the book C by Dissection by Ira Pohl.

> main(void)

No one's mentioned it yet (that I noticed), but if this is how your
book declares main(), you might be well advised to choose a different
book. "The C Programming Language" (second edition, by Kernighan and
Ritchie) would be an excellent choice.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Alex
Guest
Posts: n/a

 12-16-2003
Christopher Benson-Manica <(E-Mail Removed)> wrote:
> interpim <(E-Mail Removed)> spoke thus:

>> Im trying to teach myself programming, and I am doing exercises out of the book C by Dissection by Ira Pohl.

>> main(void)

> No one's mentioned it yet (that I noticed), but if this is how your
> book declares main(), you might be well advised to choose a different
> book. "The C Programming Language" (second edition, by Kernighan and
> Ritchie) would be an excellent choice.

There is nothing wrong with that declaration of main with respect
to C89. In fact, I recall the following being used throughout K&R2:

main()
{

}

.... which is worse than the OP's example when 'main' may be invoked
recursively.

Alex

Default User
Guest
Posts: n/a

 12-16-2003
Christopher Benson-Manica wrote:

> > main(void)

>
> No one's mentioned it yet (that I noticed), but if this is how your
> book declares main(), you might be well advised to choose a different
> book. "The C Programming Language" (second edition, by Kernighan and
> Ritchie) would be an excellent choice.

Really? Here's K&R's first program:

In C, the program to print "hello, world" is

#include <stdio.h>

main()
{
printf("hello, world\n");
}

Brian Rodenborn

Keith Thompson
Guest
Posts: n/a

 12-16-2003
(E-Mail Removed) (Dan Pop) writes:
> In <brm855\$n96\$(E-Mail Removed)> Richard Heathfield
> <(E-Mail Removed)> writes:
>
> >interpim wrote:
> >
> >> #include "primes.h"

> >
> >You forgot:
> >
> >#include <stdio.h>

>
> Nope, he hasn't. It's in "primes.h", which you have snipped without

Fair enough -- but as a matter of style, it's better to provide a
"#include <stdio.h>" directive in each source file that needs it
(especially since neither "primes.h" nor "primes.c" depends on
anything in <stdio.h>).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"