Homework help, comp...
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] Homework help, computer science algorithms.

24 Posts
9 Users
0 Reactions
70 Views
Posts: 3149
Free Member
Topic starter
 

Hi Hive mind. My daughter has stumped me, I have no idea what is going on with this Algorithms worksheet. Please explain it to me in Wally plain language.
[url= https://s14.postimg.org/xb0m5atq9/algorithms.jp g" target="_blank">https://s14.postimg.org/xb0m5atq9/algorithms.jp g"/> [/img][/url]

thanks.


 
Posted : 08/01/2018 7:48 pm
Posts: 0
Free Member
 

Well P1(1) is easy 🙂

Just a "*" on a line on the blackboard.

For values other than 1 then the function recursively calls itself. You need to follow the call path through.


 
Posted : 08/01/2018 7:59 pm
Posts: 0
Free Member
 

recursion ftw 8)

a)
*

b)
*
**
*

c)
*
**
*
***
*
**
*

d)
*
**
*
***
*
**
*
****
*
**
*
***
*
**
*


 
Posted : 08/01/2018 8:00 pm
Posts: 3149
Free Member
Topic starter
 

Oddly my daughter has the correct answers, but neither of us can work out how we got there. Please explain "For values other than 1 then the function recursively calls itself. You need to follow the call path through."

I am looking forward to the enlightenment.

Many thanks.


 
Posted : 08/01/2018 8:09 pm
Posts: 0
Free Member
 

Sorry, that wasn't very helpful - If you are still struggling, just try following the code as explicit instructions line by line, writing the output as you go.

Remember that once you've done (a), you already know the output for P1(1), and so on, so you don't necessarily need to work it all out again.

Hopefully my answer will help you to trace it through.


 
Posted : 08/01/2018 8:09 pm
Posts: 3171
Free Member
 

In the else bit, the algorithm is calling itself (recursion as mentioned above) but subtracting 1 from N. Don't forget that the code continues to run after the recursive call is complete.


 
Posted : 08/01/2018 8:12 pm
Posts: 0
Free Member
 

"For values other than 1 then the function recursively calls itself. You need to follow the call path through."

'recursion' is the function calling itself, so 'recursively calls itself' is just confusing you.

What that is telling you is that when you call it with (1), it just does something then stops. When you call it with something other than (1), it will go to the other half of the 'if' statement and work out the answer by calling itself.

Trace it through until you get to a recursive call - like P1(n-1); write down your value of 'n', go back to the start and run through with the new value of n. When you have finished, you will return to where you were before, and use your previous value of n.

This is called pushing and popping the call stack, it is how most languages manage control flow.


 
Posted : 08/01/2018 8:15 pm
Posts: 0
Free Member
 

You need to understand recursion.

Here are some examples: https://stackoverflow.com/questions/25676961/understanding-how-recursive-functions-work


 
Posted : 08/01/2018 8:20 pm
Posts: 7076
Full Member
 

Who writes code like that? I mean seriously?

Have we gone back to APL? Or perhaps [url= https://en.wikipedia.org/wiki/Befunge ]Befunge[/url] ?

EDIT: Anyhow, the question is "could you write how the blackboard would look after the following statments".

To which the answer is clearly "No" unless you have the ability to recreate the look of a blackboard, complete with chalk powder, scratched out stuff from yesterday's class, and bits of snot from the grotty kid at the back of the class.


 
Posted : 08/01/2018 8:33 pm
Posts: 3149
Free Member
Topic starter
 

Ok, we are still struggling after everyone's very kind efforts.
Why does P1(3) start with a * ?


 
Posted : 08/01/2018 8:37 pm
Posts: 10315
Full Member
 

Why does P1(3) start with a * ?

Because the first thing that P1(3) does is call P1(2)
and the first thing P1(2) does is call P1(1)
and P1(1) is just *


 
Posted : 08/01/2018 8:45 pm
Posts: 10315
Full Member
 

I think you get that P1(1) is just '*'

P1(2) is then:

P1(1)
**
P1(1)

and because we already know P1(1) is * then the whole thing becomes

* <-This * is P1(1)
**
* <- and this * is P1(1)

Then do the same for P1(3). First time through it is

P1(2)
***
P1(2)

But we just calculated P1(2). So replacing that answer where we have P1(2) above you get

* <-The next three lines are just P1(2)
**
*
<-This is the part of P1(3)
* <- and the next three lines are just P1(2) again
**
*

Then do the same for P1(3)


 
Posted : 08/01/2018 8:47 pm
Posts: 0
Free Member
 

Look at the logic - what happens when n != 1? It calls itself again, ie.

P1(3)
Calls P1(2)
Calls P1(1)
write *
.
.
.


 
Posted : 08/01/2018 8:49 pm
Posts: 10315
Full Member
 

and of course for fun, just Google 'recursion' 🙂


 
Posted : 08/01/2018 8:51 pm
Posts: 3149
Free Member
Topic starter
 

holly moses we get it!!! WHooppy whoop whoop!
Great explanation Leffeboy. Enjoy a Wheatbeer on me.
Does your email in profile take paypal gift?


 
Posted : 08/01/2018 8:52 pm
Posts: 77347
Free Member
 

You need to understand recursion.

If you want to understand recursion, Google it. (Seriously, do it.)


 
Posted : 08/01/2018 8:53 pm
Posts: 10315
Full Member
 

Does your email in profile take paypal gift?
Unfortunately not but I'll crack one open in celebration anyway 🙂


 
Posted : 08/01/2018 8:53 pm
Posts: 77347
Free Member
 

and of course for fun, just Google 'recursion'

Gods damn you.


 
Posted : 08/01/2018 8:54 pm
Posts: 10315
Full Member
 

🙂

also, see [url= http://singletrackmag.com/forum/topic/homework-help-computer-science-algorithms ]this thread[/url]


 
Posted : 08/01/2018 8:55 pm
Posts: 31206
Full Member
 

Who writes code like that? I mean seriously?

You’re not a computer scientist unless you understand recursion.

And you’re not a software engineer unless you understand why using recursion is almost always a bad idea.


 
Posted : 08/01/2018 8:55 pm
Posts: 3171
Free Member
 

also, see this thread

🙂

And you’re not a software engineer unless you understand why using recursion is almost always a bad idea.

Welcome to the world of infinite loops...many hours spent trying to debug/fix awful code, oh the joy!


 
Posted : 08/01/2018 9:38 pm
Posts: 77347
Free Member
 

also, see this thread

I saw that coming, but well played nonetheless.

you’re not a software engineer unless you understand why using recursion is almost always a bad idea.

you’re not a software engineer unless you understand why using recursion is almost always a bad idea, and choose to do it anyway. (-:


 
Posted : 08/01/2018 9:44 pm
Posts: 77347
Free Member
 

Welcome to the world of infinite loops

Is it just me that thinks that sounds like it should be an Iron Maiden album?


 
Posted : 08/01/2018 9:44 pm
Posts: 0
Free Member
 

Recursion is great, in the right place.

For tree traversal it is hard to beat, for safety of life systems, not so much.


 
Posted : 08/01/2018 9:55 pm
Posts: 3171
Free Member
 

Is it just me that thinks that sounds like it should be an Iron Maiden album?

\m/

Ah Infinite Dreams - great track.


 
Posted : 08/01/2018 9:55 pm

6 DAYS LEFT
We are currently at 95% of our target!