有人可以告诉我哪里出错了? [英] Can someone please tell me where I am going wrong??
问题描述
我正在尝试编写一个程序,要求用户在区间[1,2]中输入一个数字
,程序然后给出
$ b $的自然对数b那个数字,使用log(x + 1)系列......
这是我到目前为止所做的并且无法弄清楚我做错了什么。
任何帮助将不胜感激,谢谢你们...
#include< stdio.h>
#包括< math.h>
int main(){
int i,n;
double x;
浮点数,期限;
printf("在区间[1,2]中输入一个数字);
scanf("%d",& x);
sum = 0;
i = 0;
做{
i = i + 1;
x =(double)i;
term = pow(x,i )/ i;
if(i%2 == 1)
sum = sum + term;
else
sum = sum - term;
} while(i< = n);
printf(&the; wer is%lf \ n",sum);
E-Dot写道:
我正在尝试编写一个程序,要求用户在区间[1,2]中输入一个数字
,程序然后给出
这个数字,使用日志系列(x + 1)......
这是我到目前为止所不知道的是什么我我做错了。
任何帮助都会非常感谢,谢谢你们......
#include< stdio.h>
#include< math.h>
int main(){
如果主要没有去要接受命令行参数,请指定void。
int i,n;
double x;
float总和,期限;
为什么不将sum和term加倍?
printf("输入一个数字区间[1,2]);
使用换行符结束printf字符串或在printf之后调用fflush(stdout)
。否则,您的输出可能会延迟,因为
的缓冲。
scanf("%d",& x);
你告诉scanf寻找一个十进制整数值并将它存储到一个双重对象中。你确定这是你想要的吗?
sum = 0;
i = 0;
做{
i = i + 1;
x =(double)i;
你在x中覆盖你以前的值。
term = pow(x, I)/ I;
由于您之前的任务,x和i将具有相同的值。
if(i %2 == 1)
sum = sum + term;
else
sum = sum - term;
>
} while(i< = n);
您将i与未初始化的值进行比较,(n);未定义
行为。
printf("答案是%lf \ n",sum);
对于打印浮点对象,请使用%f格式说明符。 %lf
格式说明符添加了C99,作为替代,但许多
编译器还没有完全支持它。
E-Dot写道:
我正在尝试编写一个程序,要求用户输入一个数字
在区间[1,2]中,程序然后给出该数字的自然对数
,使用log(x + 1)系列...
如果你发布了所有作业会更好。
这就是我所拥有的远远不能弄清楚我做错了什么。
也许你说的是症状是什么......出了什么问题?
#include< stdio.h>
#include< math.h>
int main(){
>
int i,n;
double x;
浮动金额,期限;
printf("在区间[1,2]")中输入一个数字;
scanf("%d",& x);
sum = 0;
i = 0;
做{
i = i + 1;
可以使用(结果相同,但输入更少)
i ++;
x =(double)i;
term = pow(x,i)/ i;
你可以避免在循环中调用pow()。在pow(x,i)和pow(x,i + 1)之间有什么区别
,是否有模式?
if(i%2 == 1)
sum = sum + term;
或
sum + = term;
else
sum = sum - term;
} while(i< = n);
这不会给你六位小数吗?
并且n没有初始化。
< blockquote class =post_quotes>
>
printf("答案是%lf \ n",sum);
如果你做了作业所说的并打印了x,sum和log(x)
你可能已经能够自己调试这个(x会错的)
-
Nick Keighley
Nick Keighley写道:
E-Dot写道:
我正在写一个程序要求用户在区间[1,2]中输入一个数字
,程序然后给出该数字的自然对数
,使用该系列进行日志(x + 1)...
请注意''s log(x + 1),而不是log(x)...
< snip>
-
Nick Keighley
I am trying to write a program which asks the user to enter a number
in the interval [1,2], the program then gives the natural logarithm of
that number, using the series for log(x+1)...
Here is what I have so far and can''t figure out what i''m doing wrong.
any help would be greatly appreciated, thanks guys...
#include <stdio.h>
#include <math.h>
int main() {
int i,n;
double x;
float sum, term;
printf("Enter a number in the interval [1,2]");
scanf("%d", &x);
sum = 0;
i = 0;
do {
i = i+1;
x = (double) i;
term = pow(x, i)/i;
if(i % 2 == 1)
sum = sum + term;
else
sum = sum - term;
} while(i <= n);
printf("The answer is %lf\n",sum);
E-Dot wrote:I am trying to write a program which asks the user to enter a number
in the interval [1,2], the program then gives the natural logarithm of
that number, using the series for log(x+1)...
Here is what I have so far and can''t figure out what i''m doing wrong.
any help would be greatly appreciated, thanks guys...
#include <stdio.h>
#include <math.h>
int main() {If main is not going to accept command line parameters, specify void.
int i,n;
double x;
float sum, term;Why not make sum and term double as well?
printf("Enter a number in the interval [1,2]");Either end the printf string with a newline or call fflush(stdout)
just after printf. Otherwise, your output may appear delayed because
of buffering.
scanf("%d", &x);You''re telling scanf to look for a decimal integer value and store it
into a double object. Are you sure that this is what you want?
sum = 0;
i = 0;
do {
i = i+1;
x = (double) i;And you''re overwriting your previous value in x.
term = pow(x, i)/i;Here x and i will have the same value due to your previous assignment.
if(i % 2 == 1)
sum = sum + term;
else
sum = sum - term;
} while(i <= n);You''re comparing i against an uninitialised value, (n); Undefined
behaviour.
printf("The answer is %lf\n",sum);For printing a float-point object use the %f format specifier. The %lf
format specifier was added with C99, as an alternative, but many
compilers don''t yet fully support it.
E-Dot wrote:
I am trying to write a program which asks the user to enter a number
in the interval [1,2], the program then gives the natural logarithm of
that number, using the series for log(x+1)...it would be better if you posted all of the assignment.
Here is what I have so far and can''t figure out what i''m doing wrong.perhaps if you said what the symptom was... What is going wrong?
#include <stdio.h>
#include <math.h>
int main() {
int i,n;
double x;
float sum, term;
printf("Enter a number in the interval [1,2]");
scanf("%d", &x);
sum = 0;
i = 0;
do {
i = i+1;could use (same result, but less to type)
i++;
x = (double) i;
term = pow(x, i)/i;you can avoid calling pow() in the loop. What is the difference
between pow(x,i) and pow(x,i+1), is there a pattern?
if(i % 2 == 1)
sum = sum + term;or
sum += term;
else
sum = sum - term;
} while(i <= n);this isn''t going to give you six decimal places is it?
And n is not initialised.
>
printf("The answer is %lf\n",sum);if you did what the assignment said and printed x, sum and log(x)
you might have been able to debug this yourself (x would be wrong)
--
Nick Keighley
Nick Keighley wrote:E-Dot wrote:
I am trying to write a program which asks the user to enter a number
in the interval [1,2], the program then gives the natural logarithm of
that number, using the series for log(x+1)...
note that''s log(x + 1), not log(x)...
<snip>
--
Nick Keighley
这篇关于有人可以告诉我哪里出错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!