相关疑难解决方法(0)

浮点异常

我成功编写了这段代码:

#include <stdio.h>
#include <math.h>
int q;

int main()
{
    srand( time(NULL) );
    int n=3;
    q=ceil(sqrt(n));
    printf("%d\n %d\n", n,q);

    if(n == 2)
        printf("%d\n is prime", n);
    else if(n % 2 == 0.0 || n < 2)
        printf("%d\n is not prime", n);
    else
    {
        int x;
        for(x = 0; x < q; x++){
            if(n % x == 0)
            {
                printf("%d\n is not prime", n);
                return;
            }
            else
                printf("%d\n is prime", n);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行我的代码时,我收到以下错误:

浮点异常

这个错误意味着什么,我该如何解决?

c floating-point

12
推荐指数
1
解决办法
13万
查看次数

正确使用 argv 条目作为另一个函数中的变量

我正在编写一个程序,其中传递第一个参数 (argv[1]),该参数确定我想要用来运行函数的线程数量。我创建了一个名为 THREAD_COUNT 的全局变量,它发生在 argv[1] 中,并使用 atoi() 将其转换为整数。

然后,我想使用 thread_count 变量作为 for 循环中的除数,表示我想要使用的线程数量。

我似乎无法做到这一点,因为我的逻辑不起作用并且我得到了浮点异常。我必须用一些东西来做一个指针吗?

别介意我,我是一名初级程序员,以前从未使用过 C。

下面的代码适用于 2 和 4 线程,只需忽略该FUNCTION变量。

#include <"transact.h">
#include <pthread.h>


int balance = 100000000;
int THREAD_COUNT;
int FUNCTION;

void * process(void * arg) {

for(int i = 0; i < 100000000/THREAD_COUNT; i++) {

int transaction = getTransaction(i);

balance = balance - 1;

printf("%d : %d\n", i, transaction);

      } 

}



int main(int argc, char * argv[]) {

int THREAD_COUNT = atoi(argv[1]);

int FUNCTION = …
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
45
查看次数

标签 统计

c ×2

floating-point ×1