Board index » delphi » need help with recursive function

need help with recursive function

I need to write a recursive function that will take a number entered by the
user, process it and then display the number of characters that number has.

Re:need help with recursive function

Go right ahead!  When you've finished, you can post it here for us to admire.
[I'm assuming you are NOT asking us to do your homework for you, as you know
that not only violates the spirit of this newsgroup, but will be of absolutely
no benefit to you, since you won't learn anything by handing in someone else's
work ...].

Bob Schor
Pascal Enthusiast

Quote
Heero Yuy wrote:
> I need to write a recursive function that will take a number entered by the
> user, process it and then display the number of characters that number has.

Re:need help with recursive function

Quote
>Heero Yuy wrote:

>> I need to write a recursive function that will take a number entered by the
>> user, process it and then display the number of characters that number has.

I can offer some valuable help - don't use recursion to do that!
Jud McCranie

Re:need help with recursive function

This is probably a school assignment, and is probably designed to teach "recursive
thinking" (or what mathematicians call "induction").  Now, once one knows how to
solve a (recursive) problem recursively, one can also learn when to use iteration,
or other techniques.

Bob Schor

Quote
Jud McCranie wrote:
> >Heero Yuy wrote:

> >> I need to write a recursive function that will take a number entered by the
> >> user, process it and then display the number of characters that number has.

> I can offer some valuable help - don't use recursion to do that!
> Jud McCranie

Re:need help with recursive function

Quote
Bob Schor <bsc...@pitt.edu> wrote:
>This is probably a school assignment, and is probably designed to teach "recursive
>thinking" (or what mathematicians call "induction").  Now, once one knows how to
>solve a (recursive) problem recursively, one can also learn when to use iteration,
>or other techniques.

But it is bad uses of recursion like this that give people the
wrong idea, because it is much better done iteratively.  And
iteration should come before recursion.
Jud McCranie

Re:need help with recursive function

Sorry.. I'm an Italian boy so my English couldn't be very perfect.
This problem can be solved by "recursive thinking" because it can be showed
as:

function chars_of_number(n: word): byte;
begin
if n<10 then chars_of_number:=1 else
chars_of_number:=1+chars_of_number(n div 10);
end;

so the function becomes a recursive-one. The teacher cauld have assigned it
to force the student to think in recursive mode and teach him to apply this
method every time he likes...

I'm sorry for showing an example in pascal code, but it was the only method
to tell my idea about teacher's assignment.

Jud McCranie ha scritto nel messaggio ...

Quote
>Bob Schor <bsc...@pitt.edu> wrote:

>>This is probably a school assignment, and is probably designed to teach
"recursive
>>thinking" (or what mathematicians call "induction").  Now, once one knows
how to
>>solve a (recursive) problem recursively, one can also learn when to use
iteration,
>>or other techniques.

>But it is bad uses of recursion like this that give people the
>wrong idea, because it is much better done iteratively.  And
>iteration should come before recursion.
>Jud McCranie