Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Leanna

Stacking Help!

This topic is 5282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have an assignment to create a stacking program, in which the client creates an empty stack of doubles, pushes and pops them, and generates errors if too much is tried to be pushed or there is nothing to pop. I have had some help getting started with my main and class Stack, however I am unsure of where to go from here. I would greatly appreciate any help on what to do next. Thank you! Leanna #include <stack> #include <cstdlib> #include <iostream> using namespace std; #define SIZE 8 class Stack { private: double data[SIZE]; int top_of_stack; public: void Init(); void Push(double item_to_push); double Pop(); }; int main() { Stack stk; stk.Init(); stk.Push(); stk.Pop(); Stack s1, s2; int k; s1.Init(); s2.Init(); cout << s1.Pop(); s1.Push(60); s1.Push(50); s1.Push(40); s1.Push(30); s1.Push(20); s1.Push(10); s2.Push(85); s2.Push(75); s2.Push(65); s2.Push(55); s2.Push(45); s2.Push(35); cout <<"\n--------------------First Stack----------------\n"; for (k=0; k<8; k++) cout << s1.Pop() << endl; cout <<"\n--------------------Second Stack----------------\n"; for (k=0; k<8; k++) cout << s2.Pop() << endl; return 0; }

Share this post


Link to post
Share on other sites
Advertisement
Well, it appears that you are allowed to use the stack class from STL, as you have it included. Assuming that...

I'm not sure what you think Stk is doing, as you don't seem to be adding anything to it. Also, I'm not sure why you have two more stacks besides this one. Just one should do, for your stated purpose.

Since you already have a stack class, you might want to define a member of class stack (the STL stack, not yours) for use in the actual stack functionality. From there, you can call pop and push (the STL versions) to your heart's content. empty might also interest you, as it will tell you when your stack is empty.

I would suggest googling for an STL users' guide to help out in the usage of the STL stack class.

-fel

[edited by - felisandria on June 3, 2004 4:00:36 PM]

Share this post


Link to post
Share on other sites
Thank you for your help. I am not sure if I am allowed to use the stack class from STL, I just learned about it from other web sites. My sample program from my instructor does not include it. Is there a way to make the program run without using the stack class from STL? We haven''t learned about that yet, so I am not sure I should be using it.
Also, I need to make two stacks run, that is why there is a s1 and an s2. As far as the stk, my instructor put that in the sample program- so I am really not sure what it is for!
I would appreciate any more help!

Thank you!
Leanna

Share this post


Link to post
Share on other sites
Hmm. Does your book have an article on stacks? I''m not sure what method your teacher would be using so I would hate to go tromping off on the "feli way" and have you get in trouble because you''re not doing it the way they expected. Otherwise, why don''t you list the sample code your instructor gave you, and maybe we can give some pointers after knowing what the instructor expects.

-fel

Share this post


Link to post
Share on other sites
Thanks for your help. The book we''re using is Sam''s teach yourself C++ in 21 days. I''ll paste below exactly what my instructor gave us. There really isn''t much in our book on stacks, only a small portion about memory. My instrutor explained the LIFO stack method at the beginning of our assignment, and that was the first time he''s mentioned it.
Anyway, if you could give me some pointers on where to get started, I would appreciate it.
Thank you again!
Leanna

Create a class Stack to implement a stack
and use Push() and Pop() as its primary
methods adding and removing items from it.

Here is the prototype for the class Stack:

#define SIZE 10

class Stack
{
private:
double data[SIZE];
int top_of_stack; // keeps track of recent pushes
public:
void Init(); // or we could use a constructor
void Push(double item_to_push);
double Pop();
};

The client creates an empty stack of
doubles with:

Stack stk;

stk.Init();

Then, whenever the client wants to "push" something
onto the stack, it calls

stk.Push( some_number );

and when the client wants to remove an item
and look at it, it calls:

cout << stk.Pop();

If the client tries to Pop() from a stack with nothing
on it, then an error should be generated. If it tries
to Push() so that more than SIZE (or whatever the size
of data is) items would end up on the stack, it also
generates an error.

Design a test main that creates at least two separate
stack objects and pushes and pops items from each,
printing as necessary. Here is an example of an
acceptable main():

int main()
{
Stack s1, s2;
int k;

// Initialize stacks ------------
s1.Init();
s2.Init();

// Test the Stack -----
cout << s1.Pop();

s1.Push( 2.34 );
s1.Push( 3.56 );
s1.Push( 7.89 ); // just for fun
s2.Push( -1.003 );
s1.Push( 5000700 );
s1.Push( 123456789.123456789 );

s2.Push( 1 );
s2.Push( 2 );
s1.Push( 3 );
s2.Push( 4 );
s1.Push( 5 );
s2.Push( 6 );

cout << "\n------------ First Stack ------------\n";
for (k=0; k<8; k++)
cout << s1.Pop() << endl;

cout << "\n------------ Second Stack ------------\n";
for (k=0; k<8; k++)
cout << s2.Pop() << endl;

return 0;
}


Share this post


Link to post
Share on other sites
Ooh. Serious cringing. Okay.

First off, I'm going to assume you understand arrays. Now, he wants the array to be the allocated memory that your stack goes in, from what I can tell from the code. This is kind of a bad thing in the Real World (TM), but it works for teaching you because you don't have to understand pointers all that well I suppose.

So, basically, what you need to do is to keep track of where in the stack you are, using top_of_stack. The actual data goes in the data array. What you need to do, in words (you figure the code out), is as follows:

Init should just set all the elements to SIZE to zero, and set top_of_stack to zero also.

When Push is called, put the data provided in the data array at the top_of_stack index. Then increment top_of_stack.

When Pop is called, return the data that is at the top_of_stack element in the data array, then decrement top_of_stack.

If you want to be fancy, clear out the array elements you popped from when you pop. Also, be sure that when you're using Push to top_of_stack, top_of_stack is not currently SIZE-1, or you'll get a memory violation.

Please don't do what his code says on the end there, it should be more:


cout <<"\n--------------------First Stack----------------\n";
for (k=0; k<s1.top_of_stack; k++)
cout << s1.Pop() << endl;


Think you can take it from here?

-fel

edit: darn HTML tags.

[edited by - felisandria on June 3, 2004 6:11:13 PM]

Share this post


Link to post
Share on other sites
I''m trying! I''ve been following your directions, but I''m not sure I understand what you mean by putting the data array in the top_of_stack index. I have a few example programs that show arrays and classes. Could you look at what I have so far and let me know what I need to change?

Thank you very much!
Leanna

#include <cstdlib>
#include <iostream>
using namespace std;

#define SIZE 6

class Stack
{
private:
double data[SIZE];
int top_of_stack;
public:
void Init();
void Push(double item_to_push);
double Pop();
};

void Stack::Init()
{
for (int k = 0; k < SIZE; k++)
data[k] = 0.0;

top_of_stack= 0;
}

void Stack:ush(double item_to_push)
{
for (int k = 0; k < SIZE; k++)
data[k] = item_to_push;
top_of_stack++;
}

Share this post


Link to post
Share on other sites
You're getting there.

Push should push one and only one item on there each time it's called. So, you don't need to iterate. It should push in the item at data[top_of_stack] and then increment top_of_stack.

Think of it as a stack of plates in a hole (to keep you from doing anything screwy like pulling a plate out of the middle). You "push" plates on by putting them on top of the stack of the plates in the hole, you "pop" plates out by pulling them off of the stack of plates in the hole and setting them to the side so you can read the double written on them, then get the next plate.

data[SIZE] is a hole that is SIZE plates deep and a plate width in diameter.
top_of_stack keeps track of where the top plate is in the hole right now.

Given top_of_stack, you can reach your robotic arm in there and grab the plate that is top_of_stack - 1 above the bottom of the hole without smashing them or missing.

Given top_of_stack, you can reach your robotic arm in there and gently set the plate at top_of_stack above the bottom of the hole without smashing the rest of the plates or dropping that plate and having it smash.

EDIT: you do need to make a determination of what top_of_stack means, btw. I'm modelling it as meaning "the place the next plate would be popped to", so it's the empty space above the top plate, more or less. You can also model it as "the actual top plate's position", in which case you would need to initialize top_of_stack to -1, and so forth. It's up to you.

-fel



[edited by - felisandria on June 3, 2004 6:15:09 PM]

Share this post


Link to post
Share on other sites
That''s abit of a ridiculous assignement for your class when there only just learning the language and maybe even there first language. This is something given in a data structures & algorithm class after you''ve learnt to program and a learnt the language well.

Share this post


Link to post
Share on other sites
I am not sure I get what you''re saying, but I changed a few things around. I also tried to start writing the Pop function. I am supposed to put error messages in as well if too much is pushed or popped, so I added those. Please let me know what you think.

Thank you!
Leanna

void Stack:ush(double item_to_push)
{
data[top_of_stack] = item_to_push;
top_of_stack++;
if (top_of_stack > SIZE)
cout << "Error \n";
}

double Stack:op()
{
if (top_of_stack >= 0)
top_of_stack--;

else cout<< "Error \n";

}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!