繁体   English   中英

程序收到信号SIGFPE,在C中使用模数时出现算术异常

[英]program received signal SIGFPE, Arithmetic exception when using modulo in C

我正在尝试使用 C lang 编写一个程序来计算给定的两个数字 A 和 B,从 A 到 B 有多少个数字可以被另一个数字 K 整除。例如,如果 A 是 1,B 是 10 ,K 为 3,则有 3 个数满足此条件:3、6 和 9。

当尝试在在线编译器中调试它时,我收到此错误:

Program received signal SIGFPE, Arithmetic exception.
0x00005555555553fd in main () at main.c:346
346             int temp = j % newArr[j][2];

模数有什么问题吗?

这是我目前的尝试。 我已经在js尝试过这个并且它有效,但在C无效

int main(void)
{
  int arr[] = {100,16,9905,8,7346,...,301n]
  //the input is array 0f 301 integers, which the 
     first element is just the number of test case

  int newArr[100][3];
  int no = 1;
  int result = 0;
  int x, y, i, j;
  int start, end;

  for (x = 0; x < 100; x++)
  {
    for (y = 0; y < 3; y++)
    {
      newArr[x][y] = arr[no];
      no += 1;
    }
  }

  for (i = 0; i < 100; i++)
  {
    if (newArr[i][0] < newArr[i][1])
    {
      start = newArr[i][0];
      end = newArr[i][1];
    }
    else
    {
      start = newArr[i][1];
      end = newArr[i][0];
    }

    for (j = start; j <= end; j++)
    {
      int temp = j % newArr[j][2];

      if ( temp == 0)
      {
        result += 1;
      }

      printf(" %d result %d\n",i, result);
    }

    printf("case %d : %d \n", i, result);
    result = 0;
  }

  return 0;
}

提前致谢!

这里没有技术问题,实际上这只是我区分ij的眼睛问题

int temp = j % newArr[j][2]; 应该变成int temp = j % newArr[i][2];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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