• 0

Algorithm Trace


Question

Hi, i really need some help on tracing an algorithm, and aswering a few questions.

Any support you could give me would be greatly appreciated, the algorithm and questions i an stuck on are as follows:

--------------------------------------------------------------------------------------------------

This is an algorithm trace task. No implementation is required.

Read the following algorithm for the recursive function called Mystery. X( ) is a one-dimensional array.

Function Mystery(X( ), N)

IF N = 1 THEN

Mystery = 1

ELSE

Temp = Mystery(X( ), N - 1)

IF X(N) >= X(Temp) THEN

Mystery = N

ELSE

Mystery = Temp

ENDIF

ENDIF

End Function Mystery

(a) Write down the exact output when the function is called with

(i) Output ?The answer is: ?, Mystery(A( ), 5)

if A(1) = 6, A(2) = 10, A(3) = 6, A(4) = 18, A(5) = 15.

(ii) Output ?The answer is: ?, Mystery(A( ), 5)

if A(1) = 16, A(2) = 18, A(3) = 12, A(4) = 15, A(5) = 18.

(b) The function is called with

Mystery(A( ), 3)

where A(1) = 28, A(2) = 20 and A(3) = 28.

Produce a diagram showing each step of the algorithm and the function calls.

© Describe the purpose of the algorithm.

(d) Write an iterative algorithm that does the same task as the recursive algorithm.

--------------------------------------------------------------------------------------------------

ps i already know how to set out a diagram showing each step of the algorithm and the function calls.

Thank:)again :)

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

OK, after much work i think i have cracked it

a) i) the answer is: 4

ii) the answer is: 5

c)I think the algorithm finds the highest number/equal highest number in the array.

Is this correct?

Thanks again

Link to comment
Share on other sites

  • 0

This is so very clearly a homework question so I'm not going to help you with it, but I will say this...

Don't sweat these kinds of problems - they are contrived and in one sense, pointless. I had a prof who loved to put these things on exams. The worst one he ever gave us was one that recursively modified and recombined 8 variables. We had to trace all variables, show all output produced by the code and describe the solution being solved. He also had a habit of marking these things out of 4... one point for each mistake... so 4 small errors in 100 or so variable values and you got zero on the question. Most people clued in and left these ones to the end of the exam after getting caught once or twice.

The thing is that in the real world, any developer who produced code like that would be fired and his work scrapped and re-written. This is why I question the validity of such questions - it would be much better to give a piece of real-world code... the kind of code you'll actually encounter in the real world.

Link to comment
Share on other sites

  • 0

Well I think the point of the question here is to be able to understand how recusion works.

Now Kestrel, that prof was a complete ass to give you that question and be strict like that on the grading.

I'm not going to be a preacher, but if someone should be fired from a job because they write recursive code, then you're wrong IMO. However there is a time and place for it.

Link to comment
Share on other sites

  • 0

i always found that real-world recursive functions (such as functions that drilled down into a directory structure) made 100x as much sense as ones that were given on tests/exams in CS classes.

Link to comment
Share on other sites

  • 0

The idea behind hard questions, is that if you can handle these questions, you can handle the standard/easy questions. Hard questions should be left on assignments and not exams though, imo.

Link to comment
Share on other sites

  • 0
The idea behind hard questions, is that if you can handle these questions, you can handle the standard/easy questions. Hard questions should be left on assignments and not exams though, imo.

585351975[/snapback]

in general i do agree, except that you've gotta draw a line between hard and hard just to be hard. the type of assignment mentioned above, with all those variables to keep track of (as well as the grading scheme mentioned) is just ridiculous. cut that by 1/4th and you'd get more accurate results, because you'd eliminate all the people that missed the question because they just didn't give a **** once they were working on it for an hour.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.