Return value in an array, Programming Languages

A specification of a function TOARR is given below.


function TOARR(n in Int, s in Stack of Int) return in Array of Int pre n ≥ 0.

post The returned value is an array of size n. It contains items from the stack s, copied in the order in which they appear in s, but with the top item from s in the position with index 1 in the returned array, and so on.

If the stack s contains more than n items, then only the top n items are copied to the returned array.

If the stack s contains fewer than n items, then the returned array is filled up with 0s.

For example, TOARR(6, [1,2,3]) = [3,2,1,0,0,0];

TOARR(4, [1,2,3,4,5]) = [5,4,3,2].

(a) What value is returned by TOARR(n,s) when s is [0,1,2,3,4] and n is 3? [1 mark] (b) The code in XImp below is intended to implement TOARR.

function TOARR(n,s)

{ //XImp

var theArr in Array (of size 4) of Int var index in Int

var temp in Stack of Int temp <-- s

for (index < 1 to n)

{
if (SIZE(s) > 0) then
{
PUT(index,theArr,PEEK(temp))
temp <-- POP(temp)
}
}

return theArr

}

(i) Suppose that the stack s is [0, 1, 2, 3, 4] and n is 3. With these inputs, trace the execution of XImp. Give a trace table showing the values of the variables theArr, index and temp after each execution of the body of the loop.

What value is returned by XImp with these inputs?

(ii) Your trace in part (i) should show that XImp is not a correct implementa- tion of TOARR. By referring to the specification of TOARR, explain why the trace shows the implementation to be incorrect. (Your explanation here does not need to refer to the details of the code in XImp.)

(c) Explain how XImp can be modified to make it correct. ( It is sufficient to give the changes which are needed in order to make the code correct. You do not need to repeat parts of the given code that do not need changing). [6 marks]

(d) Use the WorkPad to test your corrected implementation.

Save the WorkPad file which you use to test your implementation. Include the contents of this file as part of your Solution Document. ( It is sufficient to show t e s t s when s is [0, 1, 2, 3, 4] and n is 3, and for two other suitably chosen input cases.)

Posted Date: 3/7/2013 1:49:25 AM | Location : United States







Related Discussions:- Return value in an array, Assignment Help, Ask Question on Return value in an array, Get Answer, Expert's Help, Return value in an array Discussions

Write discussion on Return value in an array
Your posts are moderated
Related Questions
These are the specifications for your Perl program: 1. The file containing your program must be named cookiemgr.pl 2. It must be invoked as: cookiemgr.pl cookies_file [options]

Let us say we have a list of 10 animal objects called "animals" in Alice. Write the code to move them forward synchronously by 1 meter.

The following function calls supposedly write a single new-line character, but some are incorrect. Identify which calls don't work and explain why.     printf("%c", '\n');

Task .Task 1 Database design This task will allow you to demonstrate the following Learning Outcomes (LOs): LO 2. Justify the design and development of the application and critica

I need help getting started with a class project of creating a project in the Perl programming language. I was thinking of maybe doing an employee directory

Please remember to read and follow all of the assignment guidelines. This assignment will expand upon the simulation algorithm given in class to produce a program which may be u

i neeed an CGI Script that send mass emails i need this input fields Sender Name: Sender Email: Subject: Message: choose (HTML Or text ) , by default HTML Emails and send bu

Write a program to handle a book collection. The data on each book might be title, author, publication date, book number and  cataloging details. Allow for adding or deleting books

Extend the SpaceRocks game and provide a warp feature which allows the ship to jump to a random location to escape a rock. The warp feature is only activated once the shields have

i''ve a problem with rendering a page with django templates