简体   繁体   English

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

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

I'm trying to make a program using C lang to calculate the given two numbers, A and B, how many numbers from A to B, inclusive, are divisible by another number K. For example, if A is 1, B is 10, and K is 3, then there are 3 numbers that satisfy this: 3, 6, and 9.我正在尝试使用 C lang 编写一个程序来计算给定的两个数字 A 和 B,从 A 到 B 有多少个数字可以被另一个数字 K 整除。例如,如果 A 是 1,B 是 10 ,K 为 3,则有 3 个数满足此条件:3、6 和 9。

when trying to debug it in online compiler i get this error:当尝试在在线编译器中调试它时,我收到此错误:

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

is there anything wrong in the modulo?模数有什么问题吗?

this is my current my attempt.这是我目前的尝试。 i've tried this in js and it works, but not in C我已经在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;
}

thanks in advance!提前致谢!

there is no technical issue here, actually this is only my eye problem of differentiating i and j这里没有技术问题,实际上这只是我区分ij的眼睛问题

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

暂无
暂无

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

相关问题 当我在我的代码中对通过不同 integer 的模获得的 int 使用模时,为什么会出现 SIGFPE、算术异常错误? - Why do I got a SIGFPE, Arithmetic exception error when I'm using a modulo on an int obtained by a modulo of a different integer in my code? 该程序的模算术步骤 - modulo arithmetic steps for this program C句柄信号SIGFPE并继续执行 - C handle signal SIGFPE and continue execution C:在计算中使用无符号操作数时如何最好地处理无符号整数模算术(“环绕”) - C: How to best handle unsigned integers modulo arithmetic (“wrap-around”) when using unsigned operands in calculations 带有SIGFPE异常的程序在gdb下的行为有所不同 - Program with SIGFPE exception behaves differently under gdb 由于SIGFPE,算术异常导致除法运算导致错误执行 - Operation with division resulting in error execution because of SIGFPE, Arithmetic exception “试图接收信号SIGSEGV,分段错误”尝试使用递归获取3个字符组合的所有关键字时 - "Program received signal SIGSEGV , Segmentation fault” When trying to get all keywords of 3-character combinations using recursion C:收到信号时出现分段错误 - C: Segmentation fault when signal received 带符号整数的模运算在C中未定义行为? - Modulo arithmetic with signed integers undefined behavior in c? ANSI C-程序接收到的信号SIGSEGV,分段错误 - ANSI C - Program received signal SIGSEGV, Segmentation fault
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM