Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中的复sigma表示法_C_Function_For Loop - Fatal编程技术网

c语言中的复sigma表示法

c语言中的复sigma表示法,c,function,for-loop,C,Function,For Loop,我试图写一个C程序来计算这个符号。我已经试了几个小时了,但我找不到我的错误。我必须使用三个函数进行组合、阶乘和功率计算。此外,我不能使用其他图书馆。 我检查了组合函数、幂函数和阶乘函数;他们没有问题。我认为问题出在for循环中。例如,当我输入5时,我必须得到100左右,但相反,我得到0.19。 这是我的密码。你能帮我找出我的错误吗?非常感谢 #include <stdio.h> int factorial(int n) { int result_fact = 1, cou

我试图写一个C程序来计算这个符号。我已经试了几个小时了,但我找不到我的错误。我必须使用三个函数进行组合、阶乘和功率计算。此外,我不能使用其他图书馆。 我检查了组合函数、幂函数和阶乘函数;他们没有问题。我认为问题出在for循环中。例如,当我输入5时,我必须得到100左右,但相反,我得到0.19。 这是我的密码。你能帮我找出我的错误吗?非常感谢

#include <stdio.h>

int factorial(int n)
{

    int result_fact = 1, count;

    for (count = 1; count <= n; count++)
    {
        result_fact  = result_fact * count;
    }

    return result_fact;
}

int power(int x, int y)
{

    int result_pow = 1, count;

    for (count = 1; count <= y; count++)
    {
        result_pow *= x;
    }

    return result_pow;

}

int combination(int n, int r)
{

    int fact_n, fact_r, fact_n_r, result_com;

    fact_n = factorial(n);
    fact_r = factorial(r);
    fact_n_r = factorial(n - r);
    result_com = fact_n / (fact_n_r * fact_r);

    return result_com;

}

int main()
{
    int n, i, j, z, powe = 0, fact = 0, com = 0;
    double result_top = 0, result_top1 = 0, result_bottom = 0, result_bottom1 = 0, result;

    printf("Enter a n value >> ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++)
    {
        for (z = 1; z <= n - i; z++)
        {
            result_top += power(i, z);
        }
        result_top1 += result_top;
        result_top = 0;
    }

    for (i = 1; i <= n; i++)
    {
        for (j = 1; (j <= i); j++)
        {
            powe = power(j, n);
            com = combination(n, j);
            fact = factorial(n);
            result_bottom += ((powe * com) / fact);

        }

        result_bottom1 += result_bottom;

        result_bottom = 0;


    }

    result = result_top1 / result_bottom1;
    printf("%lf", result);

    return 0;
}
#包括
整数阶乘(整数n)
{
int result_fact=1,计数;

对于(count=1;count,代码中有两个主要问题:

  • 即使在进行产生非整数结果的除法运算时,也可以使用整数类型
  • 你计算一个分子和一个分母的和,然后除以这两个和。数学表达式说的是计算分数的和,而不是分别计算分子和分母的和
由于除法需要浮点运算,而且随着
n
变大,各种函数可能会溢出
int
类型,因此让我们先将它们转换为
双精度

double factorial(int n)
{
    double result = 1;
    /*  Note:  When a loop variable like "count" is not needed outside the loop,
        declare it inside the loop, as is done below.
    */
    for (int count = 1; count <= n; ++count)
        result *= count;
    return result;
}


double power(int x, int y)
{
    double result = 1;
    for (int count = 1; count <= y; ++count)
        result *= x;
    return result;
}


double combination(int n, int r)
{
    //  Also declare other variables just where they are needed.
    double fact_n = factorial(n);
    double fact_r = factorial(r);
    double fact_n_r = factorial(n - r);
    return fact_n / (fact_n_r * fact_r);
}
main
中,我们唯一需要开始的就是
n
,所以我们先去掉其他声明:

int main(void)
{
    int n;
    printf("Enter a n value >> ");
    scanf("%d", &n);
然后我们确实想要开始一个和,所以我们将定义它并将其初始化为零。我们将在
i
上开始一个循环,而不是有两个:

    double sum = 0;
    for (int i = 1; i <= n; ++i)
    {
然后我们需要在
j
上用循环计算分母:

        double bottom = 0;
        for (int j = 1; j <= i; ++j)
        {
            double j_power = power(j, n);
            double comb    = combination(n, j);
            double n_fact  = factorial(n);
            bottom += j_power * comb / n_fact;
        }
然后您可以完成循环,打印总和,并完成
main

    printf("%lf", sum);
}

哪些输入值失败?幂和阶乘的整数函数将很容易溢出
int
。您声称的错误状态的症状,包括特定的相关输入值、预期输出值和实际输出值,应该是。我喜欢以C为生的C--I程序(在嵌入式设备上).但为手头的工作选择合适的工具是很重要的。在我30多年来编写的几十种语言中,C将是我解决这个问题的最后选择。除了最小的n之外,你的阶乘、幂和组合函数都会无声地溢出。正如前面提到的,整数除法迫使你做任何事情所有内容都是双精度的,尽管结果应该是整数。不要发布代码、数据、错误消息等的图像-复制或在问题中键入文本。公式很好,因为我不知道是否有其他方法发布它,但其余文本应作为文本发布。请不要通过故意破坏为其他人做更多工作您的帖子。通过在Stack Exchange(SE)网络上发布,您已经在a下授予SE分发内容的不可撤销的权利(即,无论您未来的选择如何)。根据SE策略,非故意破坏版本将被分发。因此,任何故意破坏行为都将被恢复。请参阅:。如果允许删除,则会出现“删除”按钮,但它只在浏览器中,而不是在移动应用程序中。@TUANAGÜLER:你的程序必须支持的最大n值是多少?你必须使用
int
,还是可以使用
long-long-int
uint64\u-t
?对于那些小n,你只需更改我在
中显示的
double
t
@TUANAGÜLER:在C中,一个孤立的
++i
i++
具有相同的效果;它们递增
i
(如果使用该值,则结果不同;一个是递增后的值,一个是递增前的值。)我习惯使用<代码> +i i>代码>窗体,因为在C++中,当操作数是一个定义为<代码> +++ >代码>的类时,它们可以有不同的效果。非常感谢你们的一切。
        double bottom = 0;
        for (int j = 1; j <= i; ++j)
        {
            double j_power = power(j, n);
            double comb    = combination(n, j);
            double n_fact  = factorial(n);
            bottom += j_power * comb / n_fact;
        }
        sum += top / bottom;
    printf("%lf", sum);
}