![]() |
| ||
| Reverse Output (Stack) 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> |
| ||
| Re: Reverse Output (Stack) This is a copy of my prime number code before implementing the stack #include <iostream> |
| ||
| Re: Reverse Output (Stack) 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: static const int MAX_STACK = 1000; 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? |
| ||
| Re: Reverse Output (Stack) 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 "" #include <iostream> |
| ||
| Re: Reverse Output (Stack) 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 :sad: #include <iostream> |
| ||
| Re: Reverse Output (Stack) 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: int i; |
| All times are GMT -4. The time now is 8:38 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC