遇到调用函数的问题 [英] having a problem with calling a function

查看:69
本文介绍了遇到调用函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自学编程,我正在通过Ira Pohl的解剖书C进行练习。

我被困在其中一个上并且无法弄清楚如何修复它。


练习是为你编写一个程序,询问用户输入的数字,然后打印出许多素数。提供了确定数字是否为素数的函数。

我的问题是我无法弄清楚如何重复调用函数并只打印从中返回true的数字素数函数。到目前为止我的是这个。


#include" primes.h"


main(无效)

{

int x,y,n;


printf(PRIME NUMBERS将打印出来);

printf(你想看多少?);

scanf("%d"& x);

is_prime( n);

{

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

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

}

}


这里是is_prime.c


#include" primes.h"


int is_prime(int n)

{

int k,limit;


if(n == 2)

返回1;

if(n%2 = = 0)

返回0;

limit = n / 2;

for(k = 3; k <= limit; k + = 2)

if(n%k == 0)

返回0;

返回1;

}

这里是primes.h


#include< stdio.h>

#include< stdlib.h>


int is_prime(int n);

请帮帮我....这件事真的让我烦恼。

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);
Please help me.... this thing is really bugging the crap out of me.

推荐答案

interpim写道:
interpim wrote:
我正在尝试自学编程,而我正在练习本书的练习由Ira Pohl解剖。我被困在其中一个上面并且无法弄清楚如何解决它。

这个练习是为你编写一个程序,要求用户输入
编号,然后打印出许多素数。提供了
确定数字是否为素数的函数。


该函数返回一个值。用它。 (见下文。)

我的问题是我无法弄清楚如何重复调用函数
并且只打印从素数中返回true的数字
数字功能。到目前为止我的是这个。

#include" primes.h"


你忘了:


#include< stdio.h>


这是必需的,因为你正在使用printf。

main(无效)
{x / y,n; n;

printf(" PRIME NUMBERS WILL打印出来的打印(你想看多少?);
scanf("%d"& x);
is_prime (n);
{
(y = 0; y< = x; ++ y)
printf("%5d:%5d \ n",y,n)
}


如果输入值为1,则is_prime函数无法正常工作,因此

将其更改为:


for(y = 2; y< = x; ++ y)

{

if(is_prime(n))

{

printf("%d是prime \ n",y);

}

else

{

printf("%d是composite.\ n,y);

}

}


现在,main()返回int,所以从main()返回一个int:


返回0;

}
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: bi **** @ eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


--
Richard Heathfield : bi****@eton.powernet.co.uk
"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


这有意义吗?


main(无效)

{

int x,y,n;


printf(PRIME NUMBERS将被打印);

printf("你想要多少个)看?");

scanf("%d"& x);

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

{

n = is_prime(y);

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

}

}


David

星期二,2003年12月16日04:34: 20 UTC,interpim <乐*** @ atrc.navy.mil>写道:
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" <le***@atrc.navy.mil> wrote:
我正在尝试自学编程,我正在通过Ira Pohl的解剖书进行练习。
我被困在其中一个并且可以弄清楚如何修复它。

这个练习是为你编写一个程序,询问用户输入的数字,然后打印出许多素数。提供了确定数字是否为素数的函数。
我的问题是我无法弄清楚如何重复调用函数,只打印从素数函数返回true的数字。到目前为止我的是这个。

#include" primes.h"

main(无效)
{
int x,y, n;

printf(PRIME NUMBERS将打印出来);
printf(你想看多少?);
scanf ("%d"& x);
is_prime(n);
{
for(y = 0; y< = x; ++ y)
printf (%5d:%5d \ n,y,n)
}
}

这里是is_prime.c

#include " primes.h"
int is_prime(int n)
{
int k,limit;

if(n == 2)
返回1;
if(n%2 == 0)
返回0;
limit = n / 2;
for(k = 3; k< =限制; k + = 2)
if(n%k == 0)
返回0;
返回1;
}

这里是素数.h

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

int is_prime(int n);

请帮助我....这件事是真的从我这里窃听垃圾。
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);
Please help me.... this thing is really bugging the crap out of me.



interpim写道:
interpim wrote:

#include" primes.h"

main(无效)


制作这个


int main(无效)

" Implicit int"已从C中移除。

{
int x,y,n;

printf(PRIME NUMBERS将被打印\\n);
printf(你想看多少?);


如果你想确定用户会看到你的提示,你需要

输出换行符或用fflush(stdout)刷新流。

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


在这里你可以调用一个函数。该函数的目的是获取一个参数

并根据该参数返回一个结果。问自己2个问题:1)

你给出了什么论点? 2)你对结果做了什么?


答案是你已经给它一个垃圾(不确定)值,

并扔掉了结果。这显然不是一件有用的事情。

{


这是一个虚假的支架。似乎没有理由。

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


这个循环多少次被执行?你想要多少次

它被执行?

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


此时n仍然没有有用的值,所以你可能会打印垃圾(虽然技术上行为是未定义的,所以有

对可能发生的事情没有限制。)

}
}


你真正想做的是:


1.读入一个数字,n。

2.创建一个计数器,初始化为0.

3.迭代整数,测试每个是一个素数。

4.找到素数后,打印并递增计数器。

5.一旦计数器达到n,终止。


你显然需要在循环中调用is_prime(),并决定

是否打印&根据

is_prime的结果递增计数器。你的循环可能如下所示:


for(count = 0,x = 0; count< n; ++ x)

{

if(is_prime(x))

{

/ *打印数量,增量数* /

}

}


我使用的变量名与您的代码不同 - n是要打印的素数和
数迭代整数。


这里是primes.h

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


这里似乎没有理由将这些包括在内。

int is_prime(int n);

#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

-

我的电子邮件地址有效,但会定期更改。

请联系我使用最近发布的地址。



-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于遇到调用函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆