为什么我的代码重复打印出“root是51重复?

时间:2017-10-23 19:05:28

标签: c

#include <stdio.h>
#include <math.h>

int main (void)
{
    int a, b, c;

    printf("************************************************\nWelcome to the cubic root estimator.\nThis estimates the value of one root of\nf(x)=ax^3+bx^2+cx+d.\n************************************************\nEnter the coefficients in the form “a b c”:\n");
    scanf("%d,%d,%d", &a, &b, &c);

    if (powf(b,2)<4*a*c)
    printf("Complex Roots – cannot estimate with this program\n");
    else{
        int x, y;
        int arr[100];

            for (x=-50; x<=50; x++)
                y=(a*x*x)+(b*x)+c;

                if (y==0)
                printf("There is a root at: x =%d", x);

                else {
                    int i;
                    for (i=0; i<100; i++)
                    arr[i]= x; /*array of the values of x that dont yield y=o*/


                    for (i=0; i<100; i++)
                        {if (arr[0]<arr[i])
                         arr[0]=arr[i];


                printf("There is a root at: x =%d", arr[i]);
                }

当我运行此代码时,它会一遍又一遍地打印出来“51中有一个根”

1 个答案:

答案 0 :(得分:0)

int i;
for (i=0; i<100; i++)
    arr[i]= x;     

for (i=0; i<100; i++)
    {
        if (arr[0]<arr[i])
            arr[0]=arr[i];
        printf("There is a root at: x =%d", arr[i]);
    }

因为您设置arr[i] = x;并退出第一个循环x是51,因为退出条件是x > 50