Return value in an array, Programming Languages

Assignment Help:

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.)


Related Discussions:- Return value in an array

Create an xslt stylesheet-attributes to elements , Attributes to Elements ...

Attributes to Elements Create an XSLT stylesheet that converts all attributes found in a given XML document to elements.Use the sample instance from question 2 to test the tra

Applications of oop, Applications of OOP, The promising areas for applicati...

Applications of OOP, The promising areas for application of OOP include: Real-time systems Simulation and modelling Object-oriented databases Hypertext, hype

DOM - Document Object Model, What is DOM? The Document Object Model is a pl...

What is DOM? The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure an

Find the dual - duality theory and complementary slackness, Linear Programm...

Linear Programming Consider the following optimization problem: min x s.t. x ≥ max{a1, a2, . . . , an} Rewrite this problem as a Linear Programming Problem. What is the

What is url describe their protocol, What is URL describe theri protocol? ...

What is URL describe theri protocol? URL basicallt stands for uniform resource locator. A URL is a pointer to a particular resource on the Internet at a particular location.

Write a program that draws the initials j, Write a program that draws the i...

Write a program that draws the initials J G P on the form similar to that shown in Fig J 1 (using straight lines and curve semicircles). The figure can not have corners. All ends a

Python, Software and sales assingment I keep get a syntax error

Software and sales assingment I keep get a syntax error

Opening and closing the document object on web page, Although the Document ...

Although the Document object's write()and writeln() methods are part of the DOM, they cannot be used to change content after a Web page has been rendered. You can write code that e

Pros and cons of Assembly language, What are the main pros and cons of asse...

What are the main pros and cons of assembly programming language?

Write a perl program to design a computer game, Write a Perl program ghici....

Write a Perl program ghici.pl, one of the simplest computer games. The program should generate a random integer between 1 and 1000 and asks the user to guess it. If the user ?nds t

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd