这个程序怎么会给出浮点错误?

how can this program give a floating point error?

这是一个 "sort of" 逻辑问题,我试图用 c 来解决。 我基本上在一个数组中输入一个数字的所有除数,并将数字添加到一个数字中并存储在一个变量 'best' 中。 但是我得到的输出是 "Floating point exception (core dumped)" 以为其中一个循环有问题,但我找不到任何问题。 我可以知道这个错误的含义是什么吗?

#include <stdio.h>

void main()
{
int n,a[50],b[50],k=0,l1=0,l2=0,temp,big,bigi,best=0,i,l3;

printf("Enter an integer (less than 10^5):- ");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
    if(n%i==0)
    { 
        a[k]=i;
        k++;
    }
}

for(i=0;i<k;i++)
{
    b[i]=a[i];
}  

for(i=0;i<k;i++)
{
    if(a[i]<10) 
    {
        continue;
    }   
    else
    {
        l3=a[i];
        while(l3>0)
        {
            temp=l3%10;
            l1=l1+temp;
            l3=l3/10;
        }
        if(l1>=10)
        {
            while(l1>0)
            {
                temp=l1%10;
                l2=l2+temp;
                l1=l1/10;
            }
        }
        a[i]=l2;
    }
}

big=a[0];
for(i=0;i<k;i++)
{
    if(a[i]>big) 
    {
        big=a[i];
        bigi=i;
    }
}

for(i=0;i<k;i++)
{
    while(i!=bigi)
    {
        if(a[bigi]==a[i])
        {
            if(b[bigi]>b[i])
            {
                best=a[i];
            }
            else
            {
                best=a[bigi];
            }
        }
    }
    if(best=0)
    {
        best=a[bigi];
    }
}
printf("The best number is :- %d",best);
}

哦,天哪,史蒂夫,你的代码格式!

我没有答案,但我敢打赌它与数组索引有关。您正在创建一个包含 50 个元素的数组。

然后您将使用 k 的索引访问它,该索引取决于您的输入!你怎么保证k不会大于49?

将一些调试 printfs 放在那里。到处。监控您的变量 k。看看你的循环在做什么,你在哪里卡住了。

为了我自己以及阅读本文的任何其他人的理智,在这里:

#include <stdio.h>

void main()
{
    int n,a[50],b[50],k=0,l1=0,l2=0,temp,big,bigi,best=0,i,l3;

    printf("Enter an integer (less than 10^5):- ");
    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
        if(n%i==0)
        { 
            a[k]=i;
            k++;
        }
    }

    for(i=0;i<k;i++)
    {
        b[i]=a[i];
    }  

    for(i=0;i<k;i++)
    {
        if(a[i]<10) 
        {
            continue;
        }   
        else
        {
            l3=a[i];
            while(l3>0)
            {
                temp=l3%10;
                l1=l1+temp;
                l3=l3/10;
            }
            if(l1>=10)
            {
                while(l1>0)
                {
                    temp=l1%10;
                    l2=l2+temp;
                    l1=l1/10;
                }
            }
            a[i]=l2;
        }
    }

    big=a[0];
    for(i=0;i<k;i++)
    {
        if(a[i]>big) 
        {
            big=a[i];
            bigi=i;
        }
    }

    for(i=0;i<k;i++)
    {
        while(i!=bigi)
        {
            if(a[bigi]==a[i])
            {
                if(b[bigi]>b[i])
                {
                    best=a[i];
                }
                else
                {
                    best=a[bigi];
                }
            }
        }
        if(best=0)
        {
            best=a[bigi];
        }
    }
    printf("The best number is :- %d",best);
}