0

I am currently a student, trying to get factorials to print out as prime numbers multiplied to certain exponents like so:

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

However, I keep getting an unusual error, which occurs right after using scanf to retrieve my input (By the way, I would really appreciate someone showing me how to retrieve multiple inputs from an exterior file to do this using input redirection, since that's how we were supposed to retrieve our inputs for this).

Anyway, I'm assuming this error is somewhere in the specification for my while loop. I would greatly appreciate any help/tips/pointers. Thank you!

#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; 
}
Crow
  • 1
  • 2
    Suggest you use a debugger to trace the execution of your program line by line. It's worth your while to learn to debug effectively on your own and only seek help as a last resort. At a minimum the debugger will tell you immediately which line of code triggers the error. – kaylum Jan 29 '17 at 04:58

1 Answers1

2

Using gdb, I can tell that it is a divide by zero error in prim % num.

Hints:

  1. Compile with the -g flag
  2. Run using gdb
  3. Set a breakpoint ...
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216