Hi this is a questing from some cs home work. Our task was to take a ada spec file and write the code for the functions and the main program, I have already done this in c++ but am having problems doing it in ada.

here is my code:

package stackPkg is
MAX : constant integer := 100;  -- Maximum capacity
type stack is private;
overflow, underflow : exception;  -- Stack overflow/underflow conditions
function empty(S : in stack) return boolean;
-- Returns true if stack S is empty.
function topItem(S : in stack) return integer;
-- Returns the top element of stack S.
-- Raises the underflow exception if S is empty.
procedure push(S : in out stack; item : in integer);
-- Pushes the item on top of stack S.
-- Raises the overflow exception if S is full.
procedure pop(S : in out stack; item : out integer);
-- Pops the top element of stack S and stores it in item.
-- Raises the underflow exception if S is empty.
private
  type stack is array(positive range 1..MAX) of integer;
  top : natural := 0;    -- array index of the top element
end stackPkg;

This is what was already supplied to me and here is what I wrote to go with the above spec file.

package body stackPkg is
procedure push (S : in out stack; item : in integer) is
begin
S (top):= item;
top:= top + 1;
--pushes the input on to the stack.
end push;
--------------------------------------------------------------------------------
procedure pop(S : in out stack; item : out integer) is
begin
 item := top;
 top:= top -1;
end pop;
--------------------------------------------------------------------------------
function topItem(S : in stack) return integer is
begin
  return top;
  --wanted to do stack(top) but that didn't work.
end topItem;
--------------------------------------------------------------------------------
function empty(S : in stack) return boolean is
begin
if(top=0) then
 return true;
else
 return false;
end if;
end empty;
--------------------------------------------------------------------------------
end stackPkg;

and the main is:

with ada.Text_IO, stackpkg, ada.integer_text_io;
use stackPkg, Ada.Text_IO ,ada.integer_text_io;

procedure main is
s: stack;
temp: integer;
begin
 for Base in 1..20 loop
   push(s, Base);
 end loop;
push(s, 5);
put_line("The stack will now be printed.");
new_line;
while empty(s)=false loop
   pop(s, temp);
   new_line;
   put(temp);
end loop;

--Pop the stack one more time to generate an error.
pop(s, temp);

end main;

After much dificulty I got this to complie but now when I run it I get the error:

raised CONSTRAINT_ERROR : stackpkg.adb:5 index check failed

How can I fix this and any other errors that are in my code? Any help would be appreciated since this is my first program in this language.

Whew! I haven't coded any ADA for about 7 years, so it will take me a bit of refresher time to help you on this. Be patient! :-)

Ok. I think your logic for push() and pop() is somewhat inverted. In push(), you set the top of the stack to the new element (S(top) = item), and then you set top to the previous entry (top = top + 1). Why line 11 in main() is generating the error (push(s, 5), I am not sure. Let me think on all of this some more, and review my old ADA texts... :-)

For me looks like your stack exposes the implementation detail of stacks array index, which does not make any kind of sense. I would suggest to check first of all to check your logic, can you show proof that your c++ version works?

Thanks for the help so far guys.

here is the c++ code that I based this ada code segment on.

template <class T>
class mystack
{
public:
    mystack();
    bool push(T item);
    bool pop(T &item);
    bool pop();
    int stack_Size();
    void clean_stack();
    bool is_full();
    bool is_empty();
    void print_stack();
    T getTop();
private:
    T array[20];
    int counter;
    int top;
};

and my push and pop functions are:

template <class T>
bool mystack<T>::push(T item)
{
    if(!is_full())
    {
        array[++top]=item;
        counter++;
        return true;
    }
    return false;
}

template <class T>
bool mystack<T>::pop(T &item)
{
    if(!is_empty())
    {
        item=array[top];
        top--;
        counter--;
        return true;
    }
    return false;
}

Ok, I tried to change the code of the push and pop function as suggested and still got the same error but I think that it should stay as in the orginal code because top is basically just a variable to store the index in. If I push a value onto the stack I need to store the next value pushed into the next index in the array, so top = top + 1.

This pseudocode is supposed to print 5, show that it does in your C++ implementation:

push(3)
push(2)
print pop() + pop()

My code dosn't if I use it like your psudeo code, because the pop function return true or false on rather the pop was successful, so it ouputs 2 not 5, but I tried the stl stack and it dosn't work like your psudo code either.

However I cand do it with my code with the following:

mystack<int> s;
s.push(3);
s.push(2);
int var1, var2, sum;
s.pop(var1);
s.pop(var2);
sum = var1 + var2;
cout << sum << endl;

This will output 5.

I just figure out how to fix my problem with the ada implimentation.
I had tried switching the

top:= top + 1;

and the

S (top):= item;

lines before and it didn't work but I deleted all the object code and tried again with just the source files and it now works correctly!

Thanks for the help guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.