Question: For a given integer n>1, the smallest integer d>1 that divides n is a prime factor. We can find the prime factorization of n if
For a given integer n>1, the smallest integer d>1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeathing this until n becomes 1. Write a program that queries the user for a value for n, determines the prime factorization of n in this manner, but displays the prime factors in descending oder. For example for n = 3960 your program should produce:
11 * 5 * 3 * 3 * 2 * 2 * 2
You must use Nyhoff's Stack class (attached); you can not use the STL stack class.
This is what I have so far;
/*-- Stack.cpp------------------------------------------------------------- This file implements Stack member functions. --------------------------------------------------------------------------*/ #includeusing namespace std; #include "Stack.h" //--- Definition of Stack constructor Stack::Stack() : myTop(-1) {} //--- Definition of empty() bool Stack::empty() const { return (myTop == -1); } //--- Definition of push() void Stack::push(const StackElement & value) { if (myTop < STACK_CAPACITY - 1) //Preserve stack invariant { ++myTop; myArray[myTop] = value; } else { cerr << "*** Stack full -- can't add new value *** " "Must increase value of STACK_CAPACITY in Stack.h "; //exit(1); } } //--- Definition of display() void Stack::display(ostream & out) const { for (int i = myTop; i >= 0; i--) out << myArray[i] << endl; } //--- Definition of top() StackElement Stack::top() const { if ( !empty() ) return (myArray[myTop]); else { cerr << "*** Stack is empty -- returning garbage value *** "; StackElement garbage; return garbage; } } //--- Definition of pop() void Stack::pop() { if ( !empty() ) myTop--; else cerr << "*** Stack is empty -- can't remove a value *** "; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
