473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

recursion

momotaro
357 100+
this fonction is supposed to print character by character am just wondring if I can do better

Expand|Select|Wrap|Line Numbers
  1. void PROMPT(char *T)
  2. {
  3.     int i = 0;
  4.  
  5.     if(*(T+i) != '\0')
  6.     {
  7.         printf("%c", *(T+i));
  8.         i++;
  9.     }
  10.     else
  11.         return;
  12.     PROMPT(T+i);
  13. }
Nov 21 '09 #1
13 1912
YarrOfDoom
1,247 Expert 1GB
There is still some room for improvement.
Try going over this code line by line with some example input and see if you can find what is obsolete.
If you really can't find it, I could provide you with some more hints.
Nov 22 '09 #2
momotaro
357 100+
I feel that there is something odd because its not beautiful :) but can't get it
Nov 22 '09 #3
YarrOfDoom
1,247 Expert 1GB
Well to me the code looks like a converted loop (replace if by while and remove everything starting from else and it would work, however I guess you want to keep it recursive here), so try thinking about something you would need in a loop, but not necessarily in a function.
Nov 22 '09 #4
momotaro
357 100+
I know that that "i" is not a good idea but how do you wanna keep track of your position on the string??
Nov 22 '09 #5
YarrOfDoom
1,247 Expert 1GB
If you look closely at the code, you see that i only takes the values '0' and '1', because every time the function is called, it is initialised back to '0'.
So in this case, i can be replaced with constants throughout the code.
Nov 22 '09 #6
momotaro
357 100+
the code become:


Expand|Select|Wrap|Line Numbers
  1.  
  2. void PROMPT(char *T)
  3. {
  4.     if(*T != '\0')
  5.         printf("%c", *T);
  6.     else
  7.         return;
  8.     PROMPT(T+1);
  9. }
and it's working! thx
Nov 22 '09 #7
Banfa
9,065 Expert Mod 8TB
Your printf could be replaced with putchar.

Where you recursively call PROMPT ask yourself under what condition you get there? There are 2 return points from this function, they could be reduced to 1.
Nov 22 '09 #8
momotaro
357 100+
sorry but I don't see how
Nov 22 '09 #9
Banfa
9,065 Expert Mod 8TB
The function has 2 execution paths when *T != '\0' and when *T == '\0'. It also has 1 explicit return (and i implicit return). Obviously you can not remove the implicit return so examine the 2 execution paths to see which statements get executed for the 2 conditions and see if you can simplify the 2 paths and reduce to a single implicit return.

As a clue you need an if without an else.
Nov 22 '09 #10
momotaro
357 100+
Expand|Select|Wrap|Line Numbers
  1. void PROMPT(char *T)
  2. {
  3.     if(*T == '\0') 
  4.         return;
  5.     printf("%c", *T);
  6.     PROMPT(T+1);    
  7. }
thx!
Nov 22 '09 #11
Banfa
9,065 Expert Mod 8TB
Not bad but reverse the if condition and you can remove the return statement too, you will need 1 each of these {, } too.
Nov 22 '09 #12
momotaro
357 100+
perfect this is recursion ! thx
Nov 22 '09 #13
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. if(*T == '\0') 
is the same as

Expand|Select|Wrap|Line Numbers
  1. if(!(*T))  
so you get:

Expand|Select|Wrap|Line Numbers
  1. if(!(*T)) return; 
Nov 23 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.