-4

I have made sure that in the statement i%a[i] == 0, that i and a[i] are between 1 and 100. But still, I get this error message!

#include <stdio.h>

int main()
{
    int n, m, x = 0, count = 0;
    scanf("%d %d", &n, &m);

    int a[n], b[m];

    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }

    for(int i = 0; i < m; i++)
    {
        scanf("%d", &b[i]);
    }

    for(int i = 1; i <= 100; i++)
    {
        if(i % a[i] == 0)
        {
            x++;
        }
        else if(b[i] % i == 0)
        {
            x++;
        }

        if(x == 2)
        {
            count++;
        }
        else
        {
            continue;
        }
        x = 0;
    }

    printf("%d", count);

    return 0;
}

Error Message:

Program terminated with signal SIGFPE, Arithmetic exception.
#0  0x0000000000401113 in main () at Solution.c:23
23                  if(i%a[i]==0){
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Yuv
  • 61
  • 6

2 Answers2

1

Using the magic number 100 in this for loop

for(int i = 1; i <= 100; i++)

has a sense only in the case when n and m are not less than 101.

Pay attention to that the program does not guarantee that a[i] is not equal to 0.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    https://www.hackerrank.com/challenges/between-two-sets/problem Please refer to this question statement. There are clear constraints placed for the input values and there is nowhere a mention of a[i] being less than 1. – Yuv Sep 12 '21 at 17:51
  • @YuvrajSakshith It does not matter what is written there. I said that your program does not guarantee this requirement. The user can enter any number. – Vlad from Moscow Sep 12 '21 at 18:07
  • 1
    The input is generated by the website is what I am saying. When such constraints are already placed, there is no question of dividing by zero. – Yuv Sep 13 '21 at 10:56
0

Vlad from Moscow correctly stated that the program does not guarantee that a[i] is not equal to 0. But he didn't make clear to you that the problem you ask about is not that "The user can enter any number" for a[i] - the problem rather are the a[i] numbers which aren't entered at all. The input is generated by the website, and only a[i] for i from 0 to n−1 are defined, while your loop tries to access a[i] for i from 1 to 100; an undefined a[i] can be 0; when such an a[i] is 0, the operation i modulo a[i] gets you the Arithmetic exception.

So heed Vlad from Moscow's warning and change your program so that it does not attempt to use undefined values.

is there a way I can fix this? Or an alternative approach?

You can basically keep your approach if you only use valid indexes into the arrays a[] and b[]; your main loop body could then be:

    {   x = 0;
        // use only valid indexes for a[] and b[]
        for (int j = 0; j < n; ++j) if (i % a[j] == 0) ++x;
        for (int j = 0; j < m; ++j) if (b[j] % i == 0) ++x;
        if (x == n+m) ++count;
    }
Armali
  • 18,255
  • 14
  • 57
  • 171