Array data insertion issues

So the goal is to set up prime factorization, i'm using 36 as my default for debugging, except when I run it, it prints out all 3's instead of 2 2 3 3 like it should. In addition it prints out 10 3's instead of just 4, I would think once it exits the while loop, it would stop filling in values? Also that initial include of stdafx.h is a default that isn't helping/hurting so I left it from visual studio express


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
#include "stdafx.h"
#include <math.h>
#include <iostream>

using namespace std;


int main()
{
	int x;					// user defined number to find prime factorization of
	double root;			// the sqrt of x, which is mathmatically the
							// largest possible factor of a number
							// this eliminates excess time trying to generate a solution
	int divisor = 2;		// used to test % of numbers less than h to see if they
							// can evenly divide into x
	int i = 0;				// array pointer location for outputting prime factors
	int primearray[10];

	cout << "Welcome to the prime factorization solver!" << endl <<
		"Please insert a number to solve for the prime factor: " << endl;
	cin >> x;

	root = sqrt (x);

	
	while(divisor <= root)
	{
		if (x%divisor == 0)
		{
			x = x/divisor;
			for (i = 0; i < 10;)
				primearray[i++] = divisor;
			divisor = 2;			// resets to smallest prime number
		}
		else
		{
			divisor++;
		}
	}


	cout << "The array is: " << endl;
	for(i = 0; i < 10; i++)
		cout << primearray[i] << "\t";


	system("PAUSE");

	return 0;
}

Your code wouldn't even compile for me. according to the information I got on this website the sqrt function does not take an integer argument.
http://www.cplusplus.com/reference/cmath/sqrt/

You might wan't to read this it's an article about system.
http://www.cplusplus.com/articles/j3wTURfi/

When you use visual studios the #include "stdafx.h" is the precompiled header file. I would recommend creating an empty project, to avoid having that in your program.

I couldn't get your logic to work so I did this instead
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
#include <math.h>
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
	int primeArray[20];
	int count = 0;
	int x;	
	int i = 2;
	int j = 0;
	
	cout << "Welcome to the prime factorization solver!" << endl;
	cout <<	"Please insert a number to solve for the prime factor: " << endl;
	cin >> x;
	
	if (x < 2)
	{
		cout << "No prime factors less than 2" << endl;
		cin.ignore();
		exit(0);
	}
	
	while(i <= x)
	{
		if (x % i == 0)
		{
			primeArray[count] = i;
			x = x / i;
			count++;
		}
		else
		{
			i++;
		}
	}/*end of while loop*/

	cout << "The array is: " << endl;
	for(j = 0; j < count; j++)
		cout << primeArray[j] << "\t";
	
	system("PAUSE");

	return 0;
}


@bilger Remove line 31.
fg109 wrote:
Remove line 31.

that dosn't work
Yes, there are other things that need to be changed in the code, but that's the reason why he got 10 3's instead of 2 2 3 3.
Some food for thought.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <vector>
#include <iostream>

class PrimeSequence
{
public:
    PrimeSequence() : _index(0) {}
    unsigned next() ;

private:
    unsigned _index ;

    static bool isPrimeCandidate(unsigned val) ;
    static void calcNext() ;
    static std::vector<unsigned> primes ;
};

struct factor
{
    unsigned value ;
    unsigned multiplicity ;

    factor(unsigned v) : value(v), multiplicity(0) {}
};

std::vector<factor> factorize( unsigned value )
{
    PrimeSequence seq ;
    std::vector<factor> factors ;

    do
    {
        unsigned prime = seq.next() ;

        if ( value % prime == 0 )
        {
            factors.push_back(prime) ;
            auto & f = factors.back() ;
            while ( value % prime == 0 )
            {
                ++f.multiplicity ;
                value /= prime ;
            }
        }

    } while ( value != 1 ) ;

    return factors ;
}


int main()
{
    unsigned value ;

    std::cout << "Enter the number to factor (0 or 1 to quit.)\n" ;
    while ( std::cin >> value && value > 1 )
    {
        std::vector<factor> primeFactors = factorize(value) ;

        for ( auto& pFac : primeFactors )
            std::cout << pFac.value << " x " << pFac.multiplicity << '\n' ;

        std::cout << "\nEnter the number to factor (0 or 1 to quit.)\n" ;
    }
}


unsigned primesPrimer[] = { 2, 3, 5, 7, 11 } ;
unsigned primerSize = sizeof(primesPrimer) / sizeof(primesPrimer[0]) ;

std::vector<unsigned> PrimeSequence::primes(primesPrimer, primesPrimer+primerSize) ;

bool PrimeSequence::isPrimeCandidate(unsigned candidate)
{
    bool primeCandidate = true ;
    auto prime = primes.begin() ;

    while ( primeCandidate && prime != primes.end() )
        if ( candidate % *prime++ == 0 )
            primeCandidate = false ;

    return primeCandidate ;
}

void PrimeSequence::calcNext()
{
    unsigned val = primes.back() ;

    do
    {
        val += 2 ;
    } while ( !isPrimeCandidate(val) ) ;

    primes.push_back(val) ;
}

unsigned PrimeSequence::next()
{
    if ( _index == primes.size() )
        calcNext() ;

    return primes[_index++] ;
}
Topic archived. No new replies allowed.