Reverse Output (Stack)

Reply

Join Date: Jul 2004
Posts: 15
Reputation: silicon is an unknown quantity at this point 
Solved Threads: 0
silicon silicon is offline Offline
Newbie Poster

Reverse Output (Stack)

 
0
  #1
Jul 5th, 2004
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


  1. #include <iostream.h>
  2. const int STACK_MAX= 20;
  3. typedef int StackElement;
  4.  
  5. class Stack
  6. {
  7. private:
  8. StackElement StackArray[STACK_MAX];
  9. int StackTop;
  10.  
  11. public:
  12. bool empty();
  13. int primefactor(int number);
  14. bool full( );
  15. bool isprime(int number);
  16. void push(const StackElement & value);
  17. void pop();
  18. int top();
  19. void display();
  20.  
  21. Stack::Stack() //Constructor
  22. {
  23. StackTop=-1;
  24.  
  25. }
  26.  
  27. Stack::primefactor(int number)
  28. {
  29. bool isPrime = isprime();
  30. int prime = number;
  31. int i = 2, j;
  32. double squareroot = sqrt(static_cast<double>(number));
  33. int count = 0;
  34. cout << "The prime factorization of the number " << number << " is:" << endl;
  35. if(isPrime)
  36. cout << space << number << " is a prime number." << endl;
  37. else {
  38. while((prime > 0) && (i <= number)) {
  39. if((prime % i) == 0) {
  40. count++;
  41. for(j = 0; j < count; j++)
  42. cout << space;
  43. cout << i << " is a factor" << endl;
  44. prime /= i;
  45. } else
  46. i++;
  47. }
  48. return true;
  49. }
  50.  
  51.  
  52. void Stack::push(const StackElement & value) //Add value to the Stack
  53. {
  54. if(StackTop<STACK_MAX-1) //If Stack is not full add element
  55. {
  56. ++StackTop;
  57. StackArray[StackTop]=value;
  58. }
  59. else
  60. cout<<"Stack is full. \n";
  61. }
  62.  
  63.  
  64.  
  65. inline bool Stack::empty() const //Check for emptyness
  66. {
  67. return(StackTop==-1);
  68. }
  69.  
  70. }
  71.  
  72.  
  73. bool isprime(int number)
  74. {
  75. int i;
  76.  
  77. for(i = 2; i < number; i++) {
  78. if((number % i) == 0)
  79. return false;
  80. }
  81.  
  82.  
  83.  
  84. StackElement Stack::top()const //function to retireve value at top of Stack
  85. {
  86. if(StackTop>=0) //If Stack is not empty perform task
  87. return StackArray[StackTop];
  88. else
  89. cout<<"Stack is empty\n";
  90. }
  91.  
  92. void Stack::pop() //Function to discard value at top of Stack
  93. {
  94. if(StackTop>=0) //If Stack is not empty perform task
  95. StackTop--;
  96. else
  97. cout<<"Stack is empty\n";
  98. }
  99.  
  100. void Stack::display() //Function to write the entire stack
  101. {
  102. for(int i=StackTop;i>=0;i--)
  103. cout<<StackArray[i]<<endl;
  104. }
  105.  
  106. int main()
  107. {
  108. Stack factors;
  109. int number;
  110.  
  111. cout << "Enter a number > 1000 ";
  112. cin >> number;
  113.  
  114.  
  115.  
  116. factors.primefactor();
  117. factors.top();
  118. factors.pop();
  119. factors.display();
  120. };
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 15
Reputation: silicon is an unknown quantity at this point 
Solved Threads: 0
silicon silicon is offline Offline
Newbie Poster

Re: Reverse Output (Stack)

 
0
  #2
Jul 5th, 2004
This is a copy of my prime number code before implementing the stack

  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. const string SPACE_STR = " ";
  7.  
  8. void prime_factor(int number);
  9. bool is_prime(int number);
  10.  
  11. int main() {
  12. int number;
  13.  
  14. cout << "Enter a number > 1000 -> ";
  15. cin >> number;
  16.  
  17. prime_factor(number);
  18.  
  19. return 0;
  20. }
  21.  
  22. // Function I need help with.
  23.  
  24. void prime_factor(int number)
  25. {
  26. bool isPrime = is_prime(number);
  27. int prime = number;
  28. int i = 2, j;
  29. double squareRoot = sqrt(static_cast<double>(number));
  30. int count = 0;
  31.  
  32. cout << "The prime factorization of the number " << number << " is:" << endl;
  33.  
  34. if(isPrime)
  35. cout << SPACE_STR << number << " is a prime number." << endl;
  36. else {
  37. while((prime > 0) && (i <= number)) {
  38. if((prime % i) == 0) {
  39. count++;
  40. for(j = 0; j < count; j++)
  41. cout << SPACE_STR;
  42. cout << i << " is a factor" << endl;
  43. prime /= i;
  44. } else
  45. i++;
  46. }
  47. }
  48. }
  49.  
  50. bool is_prime(int number)
  51. {
  52. int i;
  53.  
  54. for(i = 2; i < number; i++) {
  55. if((number % i) == 0)
  56. return false;
  57. }
  58.  
  59. return true;
  60. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Reverse Output (Stack)

 
0
  #3
Jul 5th, 2004
Why not just run the original program's loop down from sqrt(number) down to 2, rather than go up? Yes, you'd have to check i with is_prime(), but the numbers would come out in the right order.

If you really want to use a stack, there are standard stack classes, but you could do it without a class very simply too. If it were me, I'd probably do a simple simple simple class:

  1. static const int MAX_STACK = 1000;
  2. class AnIntStack
  3. {
  4. public:
  5. AnIntStack() { m_top = 0; }
  6. void Push(int n) { if (m_top < MAX_STACK) m_stack[m_top++] = n; }
  7. int Pop(int n) { if (m_top > 0) return m_stack[--m_top]; else return 0; }
  8. bool IsEmpty() const { return (m_top <= 0); }
  9. protected:
  10. int m_stack[MAX_STACK];
  11. int m_top;
  12. };

and then in your *original* code where you did the output you would say something like mystack.Push(i); and in the main you would have a final loop saying something like "while (!mystack.IsEmpty()) <print element mystack.Pop()>

Is that what you were after?
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 15
Reputation: silicon is an unknown quantity at this point 
Solved Threads: 0
silicon silicon is offline Offline
Newbie Poster

Re: Reverse Output (Stack)

 
0
  #4
Jul 6th, 2004
Hello thanks for the tips. I've rewritten most of my code and came up with what's below. I am receiving 3 errors so I am unable to troubleshoot or see if there will be errors in the output. I believe that it is definitely close now. I know that the errors are with my function calls at the end but I thought that this was the method used for calling them.

Example: in the code (w/o stack) if I enter 36 It should return the prime factors
2 is a factor
2 ""
3 ""
3 ""

With the stack implemented now it is supposed to now return
3 is a factor
3 ""
2 ""
2 ""


  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. const string SPACE_STR = " ";
  7.  
  8. static const int MAX_CAPACITY = 1000;
  9. class Stack
  10. {
  11. public:
  12. Stack() { top = 0; }
  13. void primefactor (int number);
  14. bool is_prime(int number);
  15. void Push(int number) ;
  16. int Pop(int number) ;
  17.  
  18.  
  19. private:
  20. int arraystack[MAX_CAPACITY];
  21. int top;
  22. };
  23.  
  24. // Function I need help with.
  25.  
  26. void Stack::primefactor(int number)
  27. {
  28. bool isPrime = is_prime(number);
  29. int prime = number;
  30. int i = 2, j;
  31. double squareRoot = sqrt(static_cast<double>(number));
  32. int count = 0;
  33.  
  34. cout << "The prime factorization of the number " << number << " is:" << endl;
  35.  
  36. if(isPrime)
  37. cout << SPACE_STR << number << " is a prime number." << endl;
  38. else {
  39. while((prime > 0) && (i <= number)) {
  40. if((prime % i) == 0) {
  41. count++;
  42. for(j = 0; j < count; j++)
  43. cout << SPACE_STR;
  44. cout << i << " is a factor" << endl;
  45. prime /= i;
  46. } else
  47. i++;
  48. }
  49. }
  50. }
  51.  
  52. bool Stack::is_prime (int number)
  53. {
  54. int i;
  55.  
  56. for(i = 2; i < number; i++) {
  57. if((number % i) == 0)
  58. return false;
  59. }
  60.  
  61. return true;
  62. }
  63.  
  64.  
  65. void Stack::Push(int number)
  66. { if (top < MAX_CAPACITY)
  67. arraystack[top++] = number;
  68.  
  69. }
  70. int Stack::Pop(int number)
  71. { if (top > 0) return arraystack[--top]; else return 0;
  72. }
  73.  
  74.  
  75. int main() {
  76. int number;
  77.  
  78. cout << "Enter a number > 1000 -> ";
  79. cin >> number;
  80.  
  81. Stack::primefactor();
  82. Stack::Push();
  83. Stack::Pop();
  84. return 0;
  85. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 15
Reputation: silicon is an unknown quantity at this point 
Solved Threads: 0
silicon silicon is offline Offline
Newbie Poster

Re: Reverse Output (Stack)

 
0
  #5
Jul 6th, 2004
Hi, I have completed the code so and am now able to compile but for some reason it will not take the output and reverse it

example prime factors of 100
2
2
5
5

I am still trying to get it to print
5
5
2
2

It seems as if everything is written correctly but the output has not changed
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. const string SPACE_STR = " ";
  7.  
  8. static const int MAX_CAPACITY = 1000;
  9. class Stack
  10. {
  11. public:
  12. Stack() { top = 0; }
  13. void primefactor (int number);
  14. bool is_prime(int number);
  15. void Push(int number) ;
  16. int Pop(int number) ;
  17.  
  18.  
  19. private:
  20. int arraystack[MAX_CAPACITY];
  21. int top;
  22. };
  23.  
  24. // Function I need help with.
  25.  
  26. void Stack::primefactor(int number)
  27. {
  28. bool isPrime = is_prime(number);
  29. int prime = number;
  30. int i = 2, j;
  31. double squareRoot = sqrt(static_cast<double>(number));
  32. int count = 0;
  33.  
  34. cout << "The prime factorization of the number " << number << " is:" << endl;
  35.  
  36. if(isPrime)
  37. cout << SPACE_STR << number << " is a prime number." << endl;
  38. else {
  39. while((prime > 0) && (i <= number)) {
  40. if((prime % i) == 0) {
  41. count++;
  42. for(j = 0; j < count; j++)
  43. cout << SPACE_STR;
  44. cout << i << " is a factor" << endl;
  45. prime /= i;
  46. } else
  47. i++;
  48. }
  49. }
  50. }
  51.  
  52. bool Stack::is_prime (int number)
  53. {
  54. int i;
  55.  
  56. for(i = 2; i < number; i++) {
  57. if((number % i) == 0)
  58. return false;
  59. }
  60.  
  61. return true;
  62. }
  63.  
  64.  
  65. void Stack::Push(int number)
  66. { if (top < MAX_CAPACITY)
  67. arraystack[top++] = number;
  68.  
  69. }
  70. int Stack::Pop(int number)
  71. { if (top > 0) return arraystack[--top]; else return 0;
  72. }
  73.  
  74.  
  75. int main() {
  76. Stack mystack;
  77. int number;
  78.  
  79. cout << "Enter a number > 1000 -> ";
  80. cin >> number;
  81.  
  82. mystack.primefactor(number);
  83. mystack.Push(number);
  84. mystack.Pop(number);
  85.  
  86.  
  87. return 0;
  88. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Reverse Output (Stack)

 
0
  #6
Jul 6th, 2004
In your code, where you have:

cout << i << " is a factor" << endl;

That's where you should INSTEAD say:

mystack.Push(i);

So, now it puts things onto the stack instead of printing them. In your main routine, where you now do Push and Pop, you instead need a loop that pops off each pushed number and prints that number out. something like:
  1. int i;
  2. while (i = mystack.Pop())
  3. cout << i << " is a factor" << endl;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC