Link to home
Create AccountLog in
Avatar of ibax
ibax

asked on

C++ Fill the missing gaps...

Hello.

I have to fill in the missing gaps for this program...


#include <iostream>

using namespace std;

template<class T>

class Stack

{

public:

      Stack(int s = 100) : size(s), top(-1) { data = new T[size]; };

      ~Stack() {------; };

      void push(const T& x) { data[----] = x; };

      T pop() { return data[-----]; };

      int isEmpty() const { return --------; };

      int isFull() const { return ----------; };

private:

      int size;

      int top;

      T* data;

};


int main()

      {

            Stack<int> intStackl(5);

            Stack<int> intStack2(10);

            Stack<char> charStack(8);

            intStackl.push(77);

            charStack.push('A');

intStack2.push(22);

            charStack.push('E');

  charStack.push('K');

intStack2.push(44);

            cout << intStack2.pop() << endl;

            cout << intStack2.pop() << endl;

            if (intStack2.isEmpty()) cout << "intStack2 is empty.\n";

            system("pause");

            return 0;
      }

Open in new window


I have done this will the code bellow

#include <iostream>

using namespace std;

template<class T>

class Stack

{

public:

      Stack(int s = 100) : size(s), top(-1) { data = new T[size]; };
      ~Stack() {};

      void push(const T& x) { data[size] = x; };

      T pop() { return *data;};

      int isEmpty() const { return data[size]; };
      int isFull() const { return data[size]; };

private:

      int size;
      int top;
      T* data;

};

      int main()

      {

            Stack<int> intStackl(5);
            Stack<int> intStack2(10);
         
            intStackl.push(77);        
			intStack2.push(22);
			intStack2.push(44);


            cout << intStack2.pop() << endl;
            cout << intStack2.pop() << endl;

            if (intStack2.isEmpty()) cout << "intStack2 is empty.\n";

            system("pause");

            return 0;

      }

Open in new window


The answer I get is...

-842150451
-842150451
intStack2 is empty.
Press any key to continue . . .

I am not sure that this is correct and that it is looking for the address...

Can anyone see where I went wrong if I did?

- Thanks
Avatar of phoffric
phoffric

A quick look, and I see that you are not using top, and that for isEmpty(), you are returning data rather than something that indicates whether the stack is empty.
For the stack, you may wish to consider using vector rather than an array. I see that you are using new [], but I don't see you using delete [].
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Converting -842150451 to hex, you get 0xCDCDCDCD.When you build your program in debug mode, this means that the data is not initialized. Take a close look at how you are setting the array (stack) value, and you should see the error.    
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of ibax

ASKER

Thanks for the continued comments guys, Just trying to figure it out now.
In general, if you use a debugger, it will save you many hours of debugging. I use Visual Studio 2008 C++ debugger for Windows and ddd for Cygwin. Here's an article to get you started with VS debugger:    http://w.e-e.com/0XEBxvIf you have any questions on using it, don't hesitate to ask.Here's debugger information for the ddd GUI debugger (which you may need to install on Linux in addition to gdb and Xming, if you haven't done so already).    http://heather.cs.ucdavis.edu/~matloff/Debug/Debug.pdf    http://www.gnu.org/manual/ddd/html_mono/ddd.html        
Avatar of ibax

ASKER

Thankyou.

Should the end product product all the values that were pushed in order?

intStackl.push(77);

charStack.push('A');

intStack2.push(22);

charStack.push('E');

charStack.push('K');

ntStack2.push(44);
BTW - I see that isEmpty() is returning an int. If you were handed the class and told to fill in the gaps, then that is fine, and useful for your own knowledge since many programmers in industry do it that way. What you may already know is that in the if-expression    if( isEmpty() )if isEmpty returns 0, then that is treated as false, and if it returns any integer != 0, then that is true.But, for the purposes of more robust coding, some of my projects required us to make our own enum boolean types having different patterns, say 0xFBAD for false and 0xDEED for true (or anything you prefer). When using this style, we were required to do:
   if( prjTRUE == isEmpty() ) {
      // ...
   }
   else if ( prjFALSE == isEmpty() ) {
      // ...
   }
   else {
       // system failure recovery code goes here!
   }

Open in new window

>> Should the end product product all the values that were pushed in order?A stack is a LIFO (last in - first out) container.If you push 1, 2, 3then the stack looks like:3 <- top21Then when you pop the stack the first time, the last item you pushed (i.e., 3) get popped off the stack, and now the stack should look like:21
Avatar of ibax

ASKER

     void push(const T& x)
        {
              data[---------] = x;
        };


How do I use push to add the elements to the top in this function.

If anything the only thing that is printed is the last value that is 44 and it's not adding to the stack by the looks of it.
In your stack class, you have size which represents the amount of allocated memory for the stack. Every time you push an element onto the stack, it would be useful to have a count of the number of items on the stack.

Initially count is 0, of course. Since you are returning an int for isEmpty(), then returning "count" would correspond to 0 for false, and not 0 for true.

Given that you are using an array (and I assume that you may need this array to grow if more items are added than the initial allocation, then the first item is at index [0]. Using the count attribute, you can push the new element into the correct spot (just be careful with indexing).
I forgot my initial comment:
>> A quick look, and I see that you are not using top
This "top" corresponds to the "count". So, if you use "top", then you should be OK, and the "count" is redundant. But you will have to be careful in what isEmpty() returns, and with the indexing.
In previous post, I said:

If you push 1, 2, 3
then the stack looks like:
3 <- top
2
1

What you have to do is ask, what are the indexes into my stack array for these three elements? You can let me know what you think.
Avatar of ibax

ASKER

Thanks for the help.
Were none of the things I pointed out even remotely useful? I pretty much identified the issues you were having plus some additional ones too.
If you wish, you can Request Attention, reopen, and select multiple posts and re-assign points for all posts which you found useful.
   Cheers,
       phoffric
Oops, I didn't mean to post the last post as an admin comment - it seems this weeks release has changed my default comment type to admin :(

I've removed the admin'ness!
Recommendation for closure is as follows.

http:#34113477 - identified the main issues (and a couple others)
http:#34113573 - explained the main issue in detail

Other posts by Paul were also extremely helpful but these two together identify the issues observed in the question.