Solved
Help on Question
Posted on 2006-11-23
Hi,
I have the following question to answer but my text books are not breaking it down enough for me to understand, could anyone please let me know in more simple terms how to work it all out. I will supply the whole question so that you know what is being asked.
________________________________________________________________________
a specification of a function LASTAT is given below. In this, we number positions in the stack s from the top, so that the top item is at position 1, the item below this is at position 2, and so on. So for example, if s is the stack [5,4,3,4,33,22,11] then the item 11 is at position 1, the item 22 is at position 2 and so on.
In the specification below, the term 'last' is used to refer to the occurence of num in s whose position os largest when numbered in this was
function LASTAT (s in Stack of Int, num in Int) return in Int
pre true
post The returned value gives the position of the last occurence of num in s. If s is empty, or contains no copies of num, then the returned value is 0.
the questions are as follows
code in XImp is intended to implement LASTAT
function LASTAT(s,num)
{ /* XImp*/
var pos in Int
Var ret in Int
Var temp in Stack of Int
temp <-- s
for (pos <-- 0 to 4)
{
if (PEEK(temp) == num) then
{ ret <-- pos}
temp <-- POP(temp)
}
return ret
}
Q1. If the stack s is [3,1,3,4,5] and num is 3, give a trace table giving the values of the variables pos, int, temp before and after the loop is entered, and after each execution of thebody of the loop
Q2. The trace in Q1 should show that XImp is not a correct implementation of LASTAT. Explain why the trace shows the implementation to be incorrect.
Q3. Explain how XIMP might be modified to make it correct. show the changes that need to be made
____________________________________________________________________
As i said before I need some idiot notes as to how to answer this question, and i am not asking anyone to give me the answers unless they want to, but any help or information you can give will be grately recieved as I am desperate to understand this.