Nothing Displayed

Feb 27, 2014 at 11:49am
I really don't get this. I made a (more-or-less) simple program that calculates whether a number between 2 to 2147483647 is prime or not, and if it's not prime, then it will display the prime factors. There must be something wrong with the void printPrimeFactors and I can't find anything logically incorrect, but it doesn't want to display the prime factors. What did I do wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cstdlib>
using namespace std;

char userInput;
bool isPrime(int testNumber);
void printPrimeFactors(int testNumber);
int testNumber = 0;

int main()
{
    do
    {
        system("cls");

        cout << "Prime Factorization" << endl;
        cout << "-------------------" << endl << endl;
        cout << "Enter an integer value [2 to 2147483647]: ";
        cin >> testNumber;
        cout << endl;

        if (testNumber < 2 || testNumber > 2147483647)
        {
            cout << "Please enter a positive integer between 2 to 2,147,483,647" << endl;
        }
        else
        {
        	 isPrime(testNumber);
        	 
        	 if (!isPrime(testNumber))
        	 {
        	 	printPrimeFactors(testNumber);
        	 }
        	 else
        	 {
        	 	cout << testNumber << " is a prime number." << endl << endl;
        	 }
        	 cout << endl;
        }
        
        cout << "Do you want to run the program again?[y/n]: ";
        cin >> userInput;
        cout << endl << endl;

        return 0;
    } while (userInput == 'y' || userInput == 'Y');
}

bool isPrime(int testNumber)
{
	for (int n = 2; n <= (testNumber^(1/2)); n++)
	{
		if (testNumber % n == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

void printPrimeFactors(int testNumber)
{
	int y = 0;
	int primeFactors[8];

	cout << "Prime Factors: ";
	
	for(int x = 1;x <= testNumber;x++)
	{
    	if(testNumber % x == 0 && isPrime(testNumber))
    	{
        	primeFactors[y] = x;
        	cout << primeFactors[y] << ' ';
        	y++;
    	}
	}
	
	return;
}
Feb 27, 2014 at 11:56am
isPrime(testNumber)
You probably want isPrime(x)
Feb 27, 2014 at 11:58am
On line 51, you are using the XOR operator, which isn't what you want. You probably want to use the sqrt function supplyed in the <cmath> header file instead. Also, another possibility is that simply too much is being calculated: Try doing some things to speed it up like a predefined prime number table for smallish values or other things like that. Finally, you should probably check for a segmentation fault in printPrimeFactors(), which can be achieved by simply doing a test for if y > 7. Why do you need to store the data in an array anyway? You aren't using it ever again...
Feb 27, 2014 at 12:07pm
(testNumber^(1/2)) does not return the squre root. It's actually xor with 0 (integer division 1/2 -> 0)

I made some corrections:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

char userInput;
bool isPrime(int testNumber);
void printPrimeFactors(int testNumber);
int testNumber = 0;

int main()
{
    do
    {
        system("cls");

        cout << "Prime Factorization" << endl;
        cout << "-------------------" << endl << endl;
        cout << "Enter an integer value [2 to 2147483647]: ";
        cin >> testNumber;
        cout << endl;

        if (testNumber < 2 || testNumber > 2147483647)
        {
            cout << "Please enter a positive integer between 2 to 2,147,483,647" << endl;
        }
        else
        {
        	 isPrime(testNumber);

        	 if (!isPrime(testNumber))
        	 {
        	 	printPrimeFactors(testNumber);
        	 }
        	 else
        	 {
        	 	cout << testNumber << " is a prime number." << endl << endl;
        	 }
        	 cout << endl;
        }

        cout << "Do you want to run the program again?[y/n]: ";
        cin >> userInput;
        cout << endl << endl;

    } while (userInput == 'y' || userInput == 'Y');
        return 0; // Moved here
}

bool isPrime(int testNumber)
{
	int v = int(sqrt(testNumber) + 0.5); // this is the square root
	for (int n = 2; n <= v; n++)
	{
		if (testNumber % n == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

void printPrimeFactors(int testNumber)
{
	int y = 0;
	int primeFactors[8];

	cout << "Prime Factors: ";

	for(int x = 1;x <= testNumber;x++)
	{
    	if(testNumber % x == 0 && isPrime(testNumber x))
    	{
        	primeFactors[y] = x;
        	cout << primeFactors[y] << ' ';
        	cout << x << ' ';
        	y++;
    	}
	}

	return;
}
Feb 27, 2014 at 12:16pm
Wow, that was fast. Anyways, MiiNiPaa, when I replace the (testNumber) with (x), it ends up displaying prime AND composite factors. Also, NT3, I'll try out the table thingy. Oh, and is it really more efficient to use y > 7 rather than using a limited array?
Feb 27, 2014 at 12:21pm
Its probably slightly more efficient not to use an array at all, which is what you probably want here (like @cire's example).
Feb 27, 2014 at 1:10pm
The program works! :D Thanks, you guys are awesome. Although I am curious, how does moving the return 0 change anything?
Feb 27, 2014 at 1:15pm
when you hit return, function will instantly stop executing and return value you provided to the caller. So in your code you had stopped your program before it got chance to check user input
Feb 27, 2014 at 1:24pm
Oh okay, thanks.
Topic archived. No new replies allowed.