Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Here Is My Code ..
I'm trapped in here .. Please Help .. .
It Some Times Gives The True Value But Not For Every Scenario .. :(

C++
#include <iostream>
using namespace std;



template <class T>
class Stack
{
private:
    int MaxSize;
    int stkptr;
    T *stackArray;
    Stack(const Stack &other); // disallow copy
    Stack& operator =(const Stack& other); // disallow assignment
public:
    Stack(int size);
    ~Stack();
    void push (const T &);
    T pop( );
    bool isFull();
    bool isEmpty();
    T top();
};
template<class T>
T Stack<T>::top()
{
    if(stkptr != -1)
        return stackArray[stkptr];
//	stkptr++;
}
template <class T>
Stack<T>::Stack(int size)
{
    if(size > 0)
    {
    MaxSize = size;
    stackArray = new T[MaxSize];
    stkptr = 0;
    }
    else
        cout<<"Size Should Be +_VE. " << endl;
}
template <class T>
Stack<T>::~Stack()
{
    delete []stackArray;
}
template <class T>
void Stack<T>::push(const T &elem)
{
    if (!isFull())
    {
    stackArray[stkptr] = elem;
    stkptr++;
    }
    else
    cout <<"Stack Is Full. " << endl;
}
template <class T>
T Stack<T>::pop()
{
    if (!isEmpty())
    {
    stkptr--;
    return stackArray[stkptr];
    }
    else
    cout<<"Stack Is Empty. "<< endl;
}
template <class T>
bool Stack<T>::isEmpty()
{
    return stkptr == 0;
}
template <class T>
bool Stack<T>::isFull()
{
    return stkptr == MaxSize;
}

int main()
{
    const int n = 5;
    Stack<int> stk(n);
    stk.push(1);
    stk.push(31);
    stk.push(14);
    stk.push(11);
    stk.push(9);


    cout << endl << "Search Max .!!! " << endl;

    int Max = stk.pop();
	cout << Max;
    while(!stk.isEmpty())
    {
		if(stk.pop() > Max)
        {
			Max = stk.pop(); // This statement looks Wierd.. 
        }
    }
    cout << Max << "\n\n";

    return 0;
}

Any Suggestions Would Be Appreciated ..
I just Got The DUMBEST Teacher Ever .. . :(
Posted
Comments
Usman Hunjra 12-May-15 8:56am    
i think there is some game of 'top' function with this .. Please check it


1 solution

You may skip one value here and assign the next to Max:
C++
if(stk.pop() > Max)
{
    Max = stk.pop(); // This statement looks Wierd..
}

Instead assign the popped value to a variable and check that for maximum:
C++
int cur = stk.pop();
if (cur > Max)
{
    Max = cur;
}
 
Share this answer
 
Comments
Shmuel Zang 12-May-15 9:13am    
5'ed.
Usman Hunjra 12-May-15 9:20am    
BingO ..
Thank You Sir .. Respect ++
Jochen Arndt 12-May-15 9:28am    
You are welcome.

If this solved your problem, you may accept the answer to mark it as solved.
Frankie-C 12-May-15 9:38am    
My 5.
Anyway maybe worth a more detailed explanation.
pop() get and remove the value from the stack that is no more available.
I.e in the case:
if(stk.pop() > Max)
{
Max = stk.pop(); // This statement looks Wierd..
}
you get the value from stack, compare it to Max, and, if it is bigger you assign to Max the *next stack value* (not the one you compared that is no more available on the stack).
With the proposed solution the correct value is assigned to Max.
Jochen Arndt 12-May-15 9:46am    
Thank you for upvoting and the more detailed explanantion.

I hoped that the poster realised the effect himself after reading my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900