[英]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?
[英]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;
}
提前致謝!
這里沒有技術問題,實際上這只是我區分i和j的眼睛問題
int temp = j % newArr[j][2];
應該變成int temp = j % newArr[i][2];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.