2017-01-29 3 views
0

В настоящее время я студент, пытаясь получить факториалы печатать, как простые числа, умноженных на определенные показатели, как так:Необычного исключения плавающей запятой (ядро сбрасывало) Ошибка с C

5! = (2^3) (3^1) (5^1)

Однако, я продолжаю получать необычную ошибку, которая возникает сразу после использования scanf для извлечения моего ввода (кстати, я был бы очень признателен кто-то показывает мне, как извлекать несколько входов из внешнего файла, чтобы сделать это, используя перенаправление ввода, так как мы должны были получить наши входы для этого).

В любом случае, я предполагаю, что эта ошибка находится где-то в спецификации моего цикла while. Я был бы очень признателен за любую помощь/советы/указатели. Спасибо!

#include <stdio.h> //headers 
#include <stdbool.h> 

//function prototypes - I will be using functions inside of each other 

int find_prime_count (int prime, int num); 
int find_next_prime (int prime); 
bool is_prime (int num); 

int main(void) //main function 
{ 
    int primeCount[100] = {0}, prime = 2, fact, i = 2, temp = 2, currentPrimeCount, printCount = 0; 

    printf ("Enter number: "); 
    scanf ("%d", &fact); 


    while (i <= fact) 
    { 
     printf ("i is less than factorial"); 
     while (temp != 1) 
     { 
      printf ("Temp is not equal to one"); 
      currentPrimeCount = find_prime_count (prime, temp); 
      printf ("currentPrimeCount calculated"); 
      temp = temp/(currentPrimeCount * prime); 
      printf ("Temp updated"); 
      primeCount[prime + 1] += currentPrimeCount; 
      printf ("primeCount[prime + 1] updated"); 
      prime = find_next_prime (prime); 
      printf ("Next prime found"); 
     } 

     i += 1; 
     temp = i; 
    } 

    printf ("%3d! = ", fact); 
    i = 0; 
    while (i < 100) 
    { 
     if (primeCount[i] != 0) 
     { 
      if (printCount == 0) 
      { 
       printf ("(%d^%d)", i, primeCount[i]); 
      } 

      else if (printCount != 0) 
      { 
       printf (" * (%d^%d)", i, primeCount[i]); 
      } 

      printCount += 1; 

      if ((printCount % 9) == 0) 
      { 
       printf ("/n"); 
      } 

      if ((printCount > 9) && ((printCount % 9) == 0)) 
      { 
       printf ("  "); 
      } 

     } 

    } 

    return 0; 
} 



bool is_prime (int num) 
{ 
    bool check = true; //sets check variable to true 
    int i = 2; //starts counter variable at 2 (will test all numbers >=2 && <num) 

    while (i < num && check == true) 
    { 
     if ((num % i) == 0) //if it is divisible by any number other than 1 and itself 
     { 
      check = false; //it is not a prime number and the check becomes false 
     } 

     i += 1; //increasing counter 
    } 

    return check; //returns boolean value 
} 

int find_next_prime (int prime) 
{ 
    int i = prime; 
    bool check = false; 
    printf ("find_next_prime starts."); 

    while (check == false) 
    { 
     i += 1; 
     check = is_prime (i); 
    } 

    printf ("find_next_prime ends."); 

    return i; 
} 

int find_prime_count (int prime, int num) 
{ 
    int count = 0; 
    printf ("find_prime_count starts."); 

     while ((prime % num) == 0) 
     { 
      count += 1; 
      num = num/prime; 
     } 

    printf ("find_prime_count ends."); 

    return count; 
} 
+2

Предлагает Вам использовать отладчик, чтобы проследить выполнение вашей программы линии по линии. Это стоит того, чтобы научиться самостоятельно отлаживать свои усилия и искать помощь только в крайнем случае. Как минимум, отладчик сразу сообщит вам, какая строка кода вызывает ошибку. – kaylum

ответ

2

Используя gdb, я могу сказать, что это деление на нулевую ошибку в prim % num.

Подсказка:

  1. компилировать с флагом -g
  2. Выполнить с помощью gdb
  3. Установить точку останова ...
Смежные вопросы