Hi everyone, I've been taking a class on cprogramming and have been successful up thus far. I am now learning about stacks and am having tremendous problems. I've written a program that will calculate the prime factors of numbers. The only problem is that I needed to reverse the output as show in the example below
e.g. Prime factors of #
1
2
3
4
need printout to read
4
3
2
1
I understand the concept in which the output will first be stored in an array [1,2,3,4] but am not entirely sure on how to retreive it without making the program really long. I know about pop functions but have only used this with push empty and full functions in class. I am just having problems on trying to figure out how to implement it here.
I've tested my prime factorization program by itself and I know it works but when I try to integrate it into the stack I get numerous errors and I am not sure if I am on the right track
Please let me know if you have any suggestions. I feel as if I am close but am started to get frustrated because it feels as if I am missing something small
Any help will be greatly appreciated
Thanks in advance
Sean
#include <iostream.h>
const int STACK_MAX= 20;
typedef int StackElement;
class Stack
{
private:
StackElement StackArray[STACK_MAX];
int StackTop;
public:
bool empty();
int primefactor(int number);
bool full( );
bool isprime(int number);
void push(const StackElement & value);
void pop();
int top();
void display();
Stack::Stack() //Constructor
{
StackTop=-1;
}
Stack::primefactor(int number)
{
bool isPrime = isprime();
int prime = number;
int i = 2, j;
double squareroot = sqrt(static_cast<double>(number));
int count = 0;
cout << "The prime factorization of the number " << number << " is:" << endl;
if(isPrime)
cout << space << number << " is a prime number." << endl;
else {
while((prime > 0) && (i <= number)) {
if((prime % i) == 0) {
count++;
for(j = 0; j < count; j++)
cout << space;
cout << i << " is a factor" << endl;
prime /= i;
} else
i++;
}
return true;
}
void Stack::push(const StackElement & value) //Add value to the Stack
{
if(StackTop<STACK_MAX-1) //If Stack is not full add element
{
++StackTop;
StackArray[StackTop]=value;
}
else
cout<<"Stack is full. \n";
}
inline bool Stack::empty() const //Check for emptyness
{
return(StackTop==-1);
}
}
bool isprime(int number)
{
int i;
for(i = 2; i < number; i++) {
if((number % i) == 0)
return false;
}
StackElement Stack::top()const //function to retireve value at top of Stack
{
if(StackTop>=0) //If Stack is not empty perform task
return StackArray[StackTop];
else
cout<<"Stack is empty\n";
}
void Stack::pop() //Function to discard value at top of Stack
{
if(StackTop>=0) //If Stack is not empty perform task
StackTop--;
else
cout<<"Stack is empty\n";
}
void Stack::display() //Function to write the entire stack
{
for(int i=StackTop;i>=0;i--)
cout<<StackArray[i]<<endl;
}
int main()
{
Stack factors;
int number;
cout << "Enter a number > 1000 ";
cin >> number;
factors.primefactor();
factors.top();
factors.pop();
factors.display();
};