简体   繁体   中英

Floating point exception C. Why?

I wrote a simple code in C about the traffic of cash register, but I always get "Floating point exception". I know, it's happen at overflow and dividing by zero, but I think at this case contain none of these. Here's the code:

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    int kassza[32] = {0};
    int a;
    int ossz = 0;

    scanf("%d", &a);
    printf("meg ok");

    while (a != 0) 
    {
        kassza[a - 1]++;
        ossz++;
        scanf("%d", &a);
    }

    int max = 0;
    for (int a = 1; a < 32; a++)
    {
        if (kassza[a] > kassza[max]) 
        {
            max = a;
        }
    }
    printf("%d. kassza: %d %%-a az osszes vevonek\n", max + 1, kassza[max], kassza[max] / (ossz / 100));
    return 0;
}

so idk why this,pls help me!

Code incurs division by zero due to kassza[max] / (ossz / 100) and ossz < 100 .

In that case, ossz/100 --> 0 due to integer division and then kassza[max] / 0 is division by zero.

Integer division by 0 is often (mis-)reported as a Floating point exception .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM