C 产生不同于预期输出的斯特林近似
所以我是C新手,慢慢地学习语法。不过我遇到了一个问题。所以我试图证明斯特林近似,在哪里 ln(N!)=N ln(N)-(N) 因此,当我在代码中生成print语句来测试数组的每个元素是否都在生成数组的输出时,数组的输出就是我想要的数字。远没有C 产生不同于预期输出的斯特林近似,c,C,所以我是C新手,慢慢地学习语法。不过我遇到了一个问题。所以我试图证明斯特林近似,在哪里 ln(N!)=N ln(N)-(N) 因此,当我在代码中生成print语句来测试数组的每个元素是否都在生成数组的输出时,数组的输出就是我想要的数字。远没有 #include <stdio.h> #include <stdlib.h> #include <math.h> double * natural_log (); /* Obtain the natural log o
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double * natural_log ();
/* Obtain the natural log of 0 to 100 and then store each value in an array */
double * approximation ();
/* Use the sterling approximation caluculate the numbers from 0 - 100 and then store it in an array */
double * difference ();
/* Calculate the difference between the arrays */
double * percentage ();
/* Calculate the percentage of the difference and return the array */
int main () {
natural_log ();
/* approximation (); */
return 0;
}
double * natural_log () {
static double natural_array[101]; /* set up the array */
int i; /* set up the integer to increase the array by a value */
natural_array[0] = 0.0; /* set up the first value in the array */
natural_array[1] = log(2);
double x;
x = natural_array [1];
for (i = 2; i <=100; i++) { /* set up the for loop to increment the i */
natural_array[i] = x + log(1 + i);
x = natural_array[i];
**printf ("Element[%d] = %d\n", i, x);**
}
return natural_array;
}
double * approximation () {
static double approximation_array[99]; /* set up the array */
int i; /* set up the integer to increase the array by a value */
for (i = 0; i <=100; i++) {
approximation_array[i] = (i) * log(i) - (i);
}
return approximation_array;
}
我确信这些数字是不应该在输出上吐出来的,所以有人能解释为什么吗?谢谢大家! 您没有使用打印正确的数据类型
printf ("Element[%d] = %d\n", i, x);
它想要打印int
类型。请试一试
printf ("Element[%d] = %e\n", i, x);
您还必须这样声明数组
static double natural_array[101];
要么这样,要么降低循环限制。最好是这样把两者联系起来
#define ELEMENTS 100
...
static double natural_array[ELEMENTS];
...
for (i = 2; i < ELEMENTS; i++) {
...
#定义元素100
...
静态双自然_数组[元素];
...
对于(i=2;i<元素;i++){
...
您没有使用打印正确的数据类型
printf ("Element[%d] = %d\n", i, x);
要打印int
类型的。请重试
printf ("Element[%d] = %e\n", i, x);
您还必须这样声明数组
static double natural_array[101];
要么这样,要么减少循环限制。最好这样将两者联系起来
#define ELEMENTS 100
...
static double natural_array[ELEMENTS];
...
for (i = 2; i < ELEMENTS; i++) {
...
#定义元素100
...
静态双自然_数组[元素];
...
对于(i=2;i<元素;i++){
...
您没有使用打印正确的数据类型
printf ("Element[%d] = %d\n", i, x);
要打印int
类型的。请重试
printf ("Element[%d] = %e\n", i, x);
您还必须这样声明数组
static double natural_array[101];
要么这样,要么减少循环限制。最好这样将两者联系起来
#define ELEMENTS 100
...
static double natural_array[ELEMENTS];
...
for (i = 2; i < ELEMENTS; i++) {
...
#定义元素100
...
静态双自然_数组[元素];
...
对于(i=2;i<元素;i++){
...
您没有使用打印正确的数据类型
printf ("Element[%d] = %d\n", i, x);
要打印int
类型的。请重试
printf ("Element[%d] = %e\n", i, x);
您还必须这样声明数组
static double natural_array[101];
要么这样,要么减少循环限制。最好这样将两者联系起来
#define ELEMENTS 100
...
static double natural_array[ELEMENTS];
...
for (i = 2; i < ELEMENTS; i++) {
...
#定义元素100
...
静态双自然_数组[元素];
...
对于(i=2;i<元素;i++){
...
natural_array[99];
必须有101
元素才能由100
索引,这是一个次要问题。数组有99个元素,索引为0到98。循环将上升到索引100,即元素101。索引越界将导致。natural_array[99]
必须有101
元素才能由100
索引,这是一个次要问题。数组有99个元素,索引为0到98。循环上升到索引100,即元素101。索引越界导致。natural_数组[99]
必须有101
元素才能由100
索引,这是一个次要问题。数组有99个元素,索引为0到98。循环上升到索引100,即元素101。索引越界导致。natural_数组[99]
必须有101
元素才能由100
索引,这是一个次要问题。数组有99个元素,索引为0到98。循环会上升到索引100,即元素101。索引越界会导致。这很有效,谢谢!我想这比我想象的要简单。minor:考虑到各种数值结果,推荐%e“
胜过%f”
。它很有效,谢谢!我想它比我想象的要简单。minor:鉴于各种各样的数值结果,推荐%e“
胜过%f”
。它很有效,谢谢!我想它比我想象的要简单。minor:鉴于各种各样的数值结果,推荐%e”
超过%f“
。这很有效,谢谢!我想这比我想象的要简单。或者:考虑到各种各样的数值结果,推荐%e”
超过%f“
。