Solved

Code implementing

Posted on 2006-11-20
1
230 Views
Last Modified: 2010-03-31
Hi,

i have the following piece of code but cannot find out what
Pos
Ret
Temp

are asking for me to return can anyone give me any hints?

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
}


Thanks
max
0
Comment
Question by:Max_AJApplegarth
1 Comment
 
LVL 10

Accepted Solution

by:
ADSLMark earned 500 total points
ID: 17981925
It's not Java, but anyway.. it looks like the code takes 4 steps in a stack, comparing the num value with each value on the top of the stack each iteration.
Let's assume we denote a stack with the following notation:
[5, 3, 7, 1] where 5 is the top element.
Let's assume we look at the state at 3 different points namely:

function LASTAT(s,num)
{
    var pos in Int
    var ret in Int
    var temp in Stack of Int
    temp <-- s
>1>>>>>>>>>>>>>>>>>>>>>>>>
    for (pos <-- 0 to 4)
    {
        if (PEEK(temp) == num) then {  ret <-- pos  }
        temp <-- POP(temp)
>2>>>>>>>>>>>>>>>>>>>>>>>>
    }
>3>>>>>>>>>>>>>>>>>>>>>>>>
    return ret
}


Then in an example:
LASTAT([5, 3, 7, 1], 7);

[1] pos=0, ret=0, temp=[5,3,7,1], num=7
[2] pos=0, ret=0, temp=[3,7,1], num=7 (if statement compares PEEK(temp)=5 with num=7)
[2] pos=1, ret=0, temp=[7,1], num=7 (if statement compares PEEK(temp)=3 with num=7)
[2] pos=2, ret=2, temp=[1], num=7 (if statement compares PEEK(temp)=7 with num=7)
[2] pos=3, ret=2, temp=[], num=7 (if statement compares PEEK(temp)=1 with num=7)
[3] pos=3, ret=2, temp=[], num=7 (if statement compares PEEK(temp)=1 with num=7)

I just noticed that the name is LASTAT (=> LastAt). That's what it does, it finds the last index of num in a stack of 4 elements.

pos is used as counter through the stack
ret is used as return value
temp is used as stack copy (since there is a POP, the stack changes so a temporary value is necessary)

I made several assumptions about the language, but i guess this will be sufficient.

Mark
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 67
java 8 lambda expresssions exception handling 3 75
thymeleaf natural templating vs JSP 2 67
varialbe initialization 11 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now