steve
steve

Reputation: 127

how can this program give a floating point error?

This is a "sort of" logical question that i tried to solve using c. I basically input all the divisors of a number in an array and the added the digits to a single digit number and stored in a variable 'best'. But the output that i get is "Floating point exception (core dumped)" Thought it would be a problem with the one of the loops, but i cant find any. could i please know what is the meaning of this error and whats causing it ?

#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);
}

Upvotes: 0

Views: 28

Answers (1)

robgrimmer
robgrimmer

Reputation: 26

Oh gosh Steve, your code formatting!

I don't have an answer, but I'm willing to bet it has something to do with the array indices. You are creating an array with 50 elements.

Then you are accessing it with an index of k that is dependent on your input! How can you guarantee that k will not be greater than 49?

Put some debug printfs in there. Everywhere. Monitor your variable k. Look at what your loops are doing, and where you are getting stuck.

And for my own sanity, and that of anyone else reading this, here:

#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);
}

Upvotes: 1

Related Questions