Recursively

K

karthikbalaguru

Hi,

Need some clarification regarding the following example w.r.t
recursive .


void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get '332' as output ?

Thx in advans,
Karthik Balaguru
 
I

Ian Collins

karthikbalaguru said:
Hi,

Need some clarification regarding the following example w.r.t
recursive .


void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get '332' as output ?
Why? What happens when f is called with "3"?
 
R

Richard Tobin

karthikbalaguru said:
void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

f("123") is called. It calls f("23") twice, then prints "2".
f("23") calls f("3") twice, then prints "3".
f("3") doesn't print anything.

So "3" gets printed twice, then "2".

Whether you actually see anything is system dependent, because no
linefeed is printed at the end.

-- Richard
 
C

Chris Dollin

karthikbalaguru said:
void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

That's the way it works.
It should actually return without printing anything.

How? If you consider only the outermost call, /that/
returns printing something [1] -- "123"[1] isn't '\0',
so it will do the final `printf`.
How can one get '332' as output ?

Let's look at the code.
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);

I'd actually rewrite it as

if (a[1] != '\0')
{
f( a + 1 );
f( a + 1 );
putchar( a[1] );
}

[Actually I'd write that first bit as `if (a[1])`; I have
no fears of C's conditional tests.]

The test is "is there are second character in this string?"
If so, print the rest of the string, again. Then print this
second character. No problem.

[Whoever wrote the code was either playing head-games with the
student's ideas about indexing or didn't really understand C's
indexing themselves. Or something.]

[1] Except there's no final newline, so perhaps it won't print
anything -- easily fixed by adding `putchar( '\n' );` [and
`return 0;`] at the end of `main`.
 
T

The Real Andy

Hi,

Need some clarification regarding the following example w.r.t
recursive .


void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get '332' as output ?

Thx in advans,
Karthik Balaguru

Have you actually tried to execute this program? Try step through it.
You SHOULD get 332.

Sounds like a homework question.
 
M

Malcolm McLean

karthikbalaguru said:
Hi,

Need some clarification regarding the following example w.r.t
recursive .


void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get '332' as output ?
If you don't understand the logic, put in some diagnostics.

void f(char a[])
{
printf("called with ***%s***\n", a);
if(a[1]=='\0')
{
printf("Returning(1)\n");
return;
}
f(a+1);
printf("finished call one\n");
f(a+1);
printf("finished call two\n"):
printf("%c", a[1]);
printf("Returning(2)\n");
}
 
C

CBFalconer

karthikbalaguru said:
Need some clarification regarding the following example w.r.t
recursive .

void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

You've gotten answers. If you really want a recursive means of
writing a string, try the following.

#include <stdio.h>

void putout(const char *s) {
if (*s) {
putchar(*s);
putout(s + 1);
}
}

int main(void) {
putout("123");
putchar('\n');
return 0;
}
 
T

Tim Wescott

Hi,

Need some clarification regarding the following example w.r.t
recursive .


void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get '332' as output ?

Thx in advans,
Karthik Balaguru

I suggest you code it up, compile it, link it, and try it out. Preferably
with a debugger running so you can step through it.

--
Tim Wescott
Control systems and communications consulting
http://www.wescottdesign.com

Need to learn how to apply control theory in your embedded system?
"Applied Control Theory for Embedded Systems" by Tim Wescott
Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html
 
R

runner

karthikbalaguru said:
Hi,

Need some clarification regarding the following example w.r.t
recursive .


void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get '332' as output ?

Thx in advans,
Karthik Balaguru

The first column shows the depth. The depth is also
shown by the indentation.

1 a[1] => 2 f(123)
2 a[1] => 3 f(23)
3 a[1] => f(3)
3 a[1] => f(3)
2 a[1] => 3 print 3
2 a[1] => 3 f(23)
3 a[1] => f(3)
3 a[1] => f(3)
2 a[1] => 3 print 3
1 a[1] => 2 print 2
 
M

Mark McDougall

karthikbalaguru said:
Need some clarification regarding the following example w.r.t
recursive . (snip)
It should actually return without printing anything. Bzzzttt!!!
How can one get '332' as output ?

Even if you can't execute this in your head, try single-stepping with a
source-level debugger.

Or if you couldn't be bothered with that, don't hand your assignment in at
all!

Regards,
 
I

IDDLife

Hi,

Need some clarification regarding the following example w.r.t
recursive .

void f(char *) ;
main()
{
f("123");}

void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);

}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get '332' as output ?

Thx in advans,
Karthik Balaguru

f("123") ------- a[1]=2
|_f("23") ----------a[1] = 3
|_f("3")
------------ a[1] = '\0' -------- return;
|_f("3")
-------------a[1] = '\0' --------- return;
|
_printf("%c", a[1]); ---------"3"
|_f("23") ----------a[1] = 3
|_f("3")
------------ a[1] = '\0' -------- return;
|_f("3")
-------------a[1] = '\0' --------- return;
|
_printf("%c", a[1]); ---------"3"
|_printf("%c", a[1]); ---------"2"

So the result is the "332".
 
I

IDDLife

Hi,

Need some clarification regarding the following example w.r.t
recursive .

void f(char *) ;
main()
{
f("123");}

void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);

}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get '332' as output ?

Thx in advans,
Karthik Balaguru

f("123") ------ a[1]='2'
|_ f("23") -------- a[1] ='3'
|_f("3")
---------- a[1]='\0' -----------return;
|_f("3")
---------- a[1]='\0' -----------return;
|_printf("%c",
a[1]) -------"3"
|_ f("23") -------- a[1] ='3'
|_f("3")
---------- a[1]='\0' -----------return;
|_f("3")
---------- a[1]='\0' -----------return;
|_printf("%c",
a[1]) -------"3"
|_ printf("%c", a[1]) -------"2"

So the result is the "332".
 
E

elmerpdc

just step through the code like below...best regards

f("123")
{
if ('2' == '\0') // false
return;
f("23")
{
if ('3' == '\0') // false
return;
f("3")
{
if ('\0' == '\0') // true
return;
}
f("3")
{
if ('\0' == '\0') // true
return;
}
printf("%c", a[1]); // prints "3"
} // f("23")
f("23")
{
if ('3' == '\0') // false
return;
f("3")
{
if ('\0' == '\0') // true
return;
}
f("3")
{
if ('\0' == '\0') // true
return;
}
printf("%c", a[1]); // prints "3"
} // f("23")
printf("%c", a[1]); // prints "2"
} // f("123")
 
D

Dibyendu

Hi,

Need some clarification regarding the following example w.r.t
recursive .

void f(char *) ;
main()
{
f("123");}

void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);

}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get '332' as output ?

Thx in advans,
Karthik Balaguru

Modify the function like:

void f(char *a)
{
printf("\nstart ->> %c %c %c\n", a[0],a[1],a[2]);

if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c\n", a[1]);

}

If there is any more problem, please ask me.
 
P

Peter Dickerson

karthikbalaguru said:
Hi,

Need some clarification regarding the following example w.r.t
recursive .

void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);

and what's wrong with putchar?
}

The output for this program has been stated as 332.
How is it possible ?

Why not compile it and see.
It should actually return without printing anything.

Why do you think that?
How can one get '332' as output ?

Why not debug the code to see what it does and why?
 
I

Ian Shef

Hi,

Need some clarification regarding the following example w.r.t
recursive .


void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

Quite simple. This is a test of certain concepts, such as:
duality between arrays and pointers
how arrays are indexed (zero-based)
how character strings are stored (null terminated).
It should actually return without printing anything.
Wrong. It is simple to compile and run this program. Have you even tried?
How can one get '332' as output ?
It is time for you to do your own homework.
If you want help, you should supply some information yourself, such as how
far you get when you pretend to be the computer yourself.
Alternatively, you might use a debugger or stick in some additional printf
statements to follow the progress of the program as it executes.

It may help to play computer, and keep track of the variables on paper.
Remeber that each call to the function f sets up a new definition of what
a[] is, until that particular call returns.
 
K

karthikbalaguru

Need some clarification regarding the following example w.r.t
recursive .
void f(char *) ;
main()
{
f("123");}
void f(char a[])
{
if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);

The output for this program has been stated as 332.
How is it possible ?
It should actually return without printing anything.
How can one get '332' as output ?
Thx in advans,
Karthik Balaguru

Modify the function like:

void f(char *a)
{
printf("\nstart ->> %c %c %c\n", a[0],a[1],a[2]);

if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c\n", a[1]);

}

If there is any more problem, please ask me.- Hide quoted text -

- Show quoted text -

Thx for your tricks to track the flow.

Thx,
Karthik Balaguru
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

A process take input from /proc/<pid>/fd/0, but won't process it 0
%a in printf 6
Command Line Arguments 0
Fibonacci 0
Switch for floating !! 70
Printing the bitfields 18
Why do i get runtime error ? 19
++*p++ 24

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top