C 计算递归序列 - 预期表达式错误

标签 c

我正在尝试为 n = 60 以内的值编写此递归序列。我知道 x0 = 1。

Xn+1 = 2^(n+1) * (sqrt( 1 + 2^-n*Xn) - 1)

到目前为止我已经

int main ()
{
    // first term x0 = 1
    double xn = 1;
    double x;
    double squareRoot = 1 + (pow(2, -x) * xn);

    for (x = 1; x <= 60; x++)
    {
        double xn = pow(2, x) * double sqrt(squareRoot) - 1);
        printf("%f\n", xn);
    }

}

但是我在有双 sqrt 的行上得到了预期的表达式错误。

最佳答案

1.这里有一个额外的括号。

double xn = pow(2, x) * double sqrt(squareRoot) - 1);

并且您不需要在此处提及类型 -double sqrt(squareRoot) 。你可以这样写 -

double xn = pow(2, x) * (sqrt(squareRoot) - 1);

2.此声明也会产生问题 -

double x;
double squareRoot = 1 + (pow(2, -x) * xn);    //this statement

for (x = 1; x <= 60; x++)

这里您在pow中使用了x,但它在循环之外,并且x直到循环才初始化。因此,您需要在此语句之前初始化x

关于C 计算递归序列 - 预期表达式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857364/

相关文章:

c - 递归 - 不确定为什么数字增加然后减少 1234554321

c - 使用二进制补码的按位减法溢出

c - 树递归编译,但在执行时崩溃

c - 无法理解Glib错误

python - 防止文件描述符在 POSIX 系统上关闭

c - 在 libev 中锁定 io watcher 和 timer watch

c - 如何使用fwrite写入数据0xf0f0f0f0

c++ - 在 select 上处理 FD_SETSIZE

c - 如何使用 C 中的整数对带有字符串的结构进行排序?

c - RSA_new 和 RSA_generate_key_ex 造成内存泄漏