2010-11-08 107 views
2
[email protected]:~/Programming/C.progs/Personal$ cat prime4.c 
/* 
* File: main.c 
* Author: matthewmpp 
* 
* Created on November 7, 2010, 2:16 PM 
*/ 

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <math.h> 

/* 
prime numbers. 
version4 
should tell whether a number is prime or not prime. 
by using other prime numbers. 
*/ 

int input_func() 
{ 
    char line[100]; 
    int n_input; 

    while (1) { 
     printf("Please enter a whole number.\n"); 
     fgets(line, sizeof (line), stdin); 
     sscanf(line, "%d", &n_input); 

     if (n_input >= 0) 
      break; 

     return (n_input); 
    } 
} 

int ifstatements_func (n_ifstate) 
int n_ifstate; 
{ 
    if (n_ifstate == 0) { 
     printf("The number, %d, is not prime and has no factors.\n", n_ifstate); 
     exit(1); 
    } 

    if (n_ifstate == 1) { 
     printf("The number, %d, is not prime.\n", n_ifstate); 
     printf("The factors of %d, is %d.\n", n_ifstate, n_ifstate); 
     exit(1); 
    } 

    if (n_ifstate == 2) { 
     printf("The number, %d, is a prime.\n", n_ifstate); 
     printf("The factors of %d, are 1 and %d.\n", n_ifstate, n_ifstate); 
     exit(1); 
    } 
    if (n_ifstate == 3) { 
     printf("The number, %d, is a prime.\n", n_ifstate); 
     printf("The factors of %d, are 1 and %d.\n", n_ifstate, n_ifstate); 
     exit(1); 
    } 
    return (n_ifstate); 
} 

int square_root_func (n_prmfnc) 
int n_prmfnc; 
{ 
    int i; //counter 

    float sq_root_f; 
    int sq_root_i; 

    int primes[100]; 
    int length_primes; 

    primes[0] = 2; /*first prime is 2.*/ 
    primes[1] = 3; /*second prime is 3.*/ 
    length_primes = sizeof (primes); 

    //printf ("before.sq_root.value of n_prmfnc=%d\n", n_prmfnc); 
    sq_root_f = sqrt(n_prmfnc); 
    sq_root_i = sq_root_f; 
    //printf ("prmfnc.after.sq_root\n"); 
    //printf ("value of sq_root=%.3f\n", sq_root_f); 
    //printf ("value of sq_root=%d\n", sq_root_i); 

    return (sq_root_i); 
} 

int prime_func (sq_root_pf, n_pf) 
int sq_root_pf, n_pf; 
{ 
    int prime_counter; 
    int prime_temp; 
    int prime_flag=0; 

    int primes_pf[100]; 
    int i;     //counter 

    primes_pf[0]=2; 
    primes_pf[1]=3; 
    primes_pf[2]=5; 

    printf ("before.for.in.pf"); 
    for (i = 0; i <= 100; ++i) { 
     printf ("after.for.in.pf"); 
     if (primes_pf[i] <= sq_root_pf) { 
      printf ("before.modulus.in.pf"); 
      prime_temp = n_pf % primes_pf[i]; 
      printf ("after.modulus.in.pf"); 
      if (prime_temp == 0) { 
       ++prime_counter; 
       if (prime_counter == 0) 
        prime_flag = 1; /*yes, number is prime.*/ 
      } 
     } 
    } 
    return (prime_flag); 
} 

int main() { 
    int n_main1; //number from input 
    int n_main2; //number after if statements 
    int sq_root_main; //square root of number from function 
    int prime_flag_main; //value of 1 if it is a prime 

    n_main1 = input_func(); 
    printf("main.after.input.function=%d.\n", n_main1); 

    n_main2 = ifstatements_func (n_main1); 
    printf ("main.after.ifstatments.function=%d\n", n_main2); 

    sq_root_main = square_root_func (n_main2); 
    printf ("main.after.square_root_func_func=%d\n", sq_root_main); 

    prime_flag_main = prime_func (sq_root_main, n_main2); 
    printf ("main.after.prime_func=%d\n", prime_flag_main); 

    return (EXIT_SUCCESS); 
} 


OUTPUT: 
[email protected]:~/Programming/C.progs/Personal$ cc -c prime4.c 
[email protected]:~/Programming/C.progs/Personal$ cc -o prime4 prime4.c -lm 
[email protected]:~/Programming/C.progs/Personal$ ./prime4 
Please enter a whole number. 
44 
main.after.input.function=44. 
main.after.ifstatments.function=44 
main.after.square_root_func_func=6 
Floating point exception 
[email protected]:~/Programming/C.progs/Personal$ 

聲明:錯誤是在prime_func。我相信原因是模數(%符號)。C編程 - 浮點異常

問題:爲什麼我得到浮點異常以及如何解決?

+0

http://stackoverflow.com/questions/3615476/floating-point-exception – TGar 2017-01-20 12:29:30

回答

4

「浮點異常」是一個用詞不當。它只發生在整數除以零和其他幾個部門相關的操作。

+0

我正在尋找... – JohnyTex 2018-01-02 15:33:17

10

會發生什麼是一個零除。您只初始化primes_pf的前三個條目,但重複遍歷所有這些條目(實際上,您的循環即使在最後一個條目之後也運行一次;使用i < 100而不是i <= 100來解決此問題)。對於除前三項之外的所有項目,除以某個單位數量,其中一項顯然爲零。不要使用單位值。

0

不知道我相信上面的答案!

X = 5.0; Y = 0.0; Z = X/Y;

這會給浮點異常....

的問題似乎是prime_pf只初始化3元。所以模數試圖除以零。 順便說一句,如果您將\ n添加到您的printf語句中,並添加額外的語句 fflush(stdout); 在程序錯誤之前,您更有可能看到調試輸出。

0

該問題存在於您的primes_pf變量中。您似乎已初始化此整數數組的前三個元素,但當迭代器i超過2時,primes_pf[i]正在從未初始化的內存中讀取數據,並將其與sq_root_pf進行比較;那是不對的。

我沒有花時間去完全理解你的算法,但我最好的猜測是你忘了給for循環中的某個地方分配一個新值給primes_pf

-1
sqrt(x) needs x to be of type double, you have used int. 

演員翻一番(雙)n_prmfnc

非常不清楚了!