Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

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

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M9526572

Have any Question?


Related Questions in Programming Language

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Task - hand execution of arraysoverviewin this task you

Task - Hand Execution of Arrays Overview In this task you will demonstrate how arrays work by hand executing a number of small code snippets. Instructions Watch the Hand Execution with Arrays video, this shows how to ste ...

Question 1 what is hadoop explaining hadoop 2 what is

Question: 1. What is Hadoop (Explaining Hadoop) ? 2. What is HDFS? 3. What is YARN (Yet Another Resource Negotiator)? The response must be typed, single spaced, must be in times new roman font (size 12) and must follow t ...

Task arrays and structsoverviewin this task you will

Task: Arrays and Structs Overview In this task you will continue to work on the knight database to help Camelot keep track of all of their knights. We can now add a kingdom struct to help work with and manage all of the ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

Background informationthis assignment tests your

Background Information This assignment tests your understanding of and ability to apply the programming concepts we have covered throughout the unit. The concepts covered in the second half of the unit build upon the fun ...

Overviewthis tasks provides you an opportunity to get

Overview This tasks provides you an opportunity to get feedback on your Learning Summary Report. The Learning Summary Report outlines how the work you have completed demonstrates that you have met all of the unit's learn ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As