473,385 Members | 1,312 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,385 software developers and data experts.

K&R exercise 4-12

Hi,

I'm working through K & R, and I have a few questions about recursive
functions. I've solved the problem at hand, but my solution seems
clumsy. In particular, I suspect there must be a better way to allow
link each instance of the function to the array cell that it produces.
I've used a static variable, which is ok except that it requires an
extra variable to indicate whether or not the function call was the
original, or spawned by another instance of printd.

Also, I've written this as a void function - since it's directly
modifying the string, is there any value to returning the string, or
is it fine to allow the calling program to simply access the modified
string?

I hope this makes sense. The code is pasted below.

Thanks,

Tyler

#include <stdio.h>

void printd (int n, char s[], int new)
{
static int p;
p = new ? 0: p;

if (n< 0 ) {
s[p++]='-';
n = -n;
}
if (n/10)
printd(n/10, s, 0);
s[p++]= (n % 10 + '0');
}

int main (void)
{
char s[100];

printd(123, s, 1);
printf("string is %s\n", s);

printd(456, s, 1);
printf("string is %s\n", s);
}
Aug 9 '07 #1
3 1859
Tyler Smith wrote, On 09/08/07 18:41:
Hi,

I'm working through K & R, and I have a few questions about recursive
functions. I've solved the problem at hand, but my solution seems
clumsy. In particular, I suspect there must be a better way to allow
link each instance of the function to the array cell that it produces.
Yes, there is. See
http://clc-wiki.net/wiki/K&R2_soluti..._4:Exercise_12 for one
possible example.
I've used a static variable, which is ok except that it requires an
extra variable to indicate whether or not the function call was the
original, or spawned by another instance of printd.
Why did you not just pass s+1 in your recursive call? Remember, that the
parameter s is actually a pointer, not an array. I suggest having a look
at chapter 6 of the comp.lang.c FAQ located at http://c-faq.com
Also, I've written this as a void function - since it's directly
modifying the string, is there any value to returning the string, or
is it fine to allow the calling program to simply access the modified
string?
In C strings are not first class citizens, so this is actually quite normal.
I hope this makes sense. The code is pasted below.

Thanks,

Tyler

#include <stdio.h>

void printd (int n, char s[], int new)
{
static int p;
p = new ? 0: p;

if (n< 0 ) {
s[p++]='-';
n = -n;
This is a problem if n is the maximum negative value of an int on most
systems. For example on a typical 16 bit system the range of int is
-32768 to +32767, since 32768 is not in this range you should see the
problem. This may be why Gregory did things the way he did on his
attempt at the link I included.
}
if (n/10)
printd(n/10, s, 0);
s[p++]= (n % 10 + '0');
}

int main (void)
{
char s[100];

printd(123, s, 1);
printf("string is %s\n", s);

printd(456, s, 1);
printf("string is %s\n", s);
main returns an int, so returning one would be a good idea. 0 means
success so is a good value to choose.
return 0;
}
--
Flash Gordon
Aug 9 '07 #2
On 2007-08-10, pete <pf*****@mindspring.comwrote a really snazzy
recursive itoa function.

Thanks! It took me twenty minutes to walk myself through the code,
but now it looks so simple! How much time does one spend working on C
code until these sorts of solutions are obvious *before* you've been
shown them I wonder?

Cheers,

Tyler
>
void itoa(int n, char *s);
static char *utoa(unsigned n, char *s);
static char *utoap1(unsigned n, char *s);

void itoa(int n, char *s)
{
if (0 n) {
*s++ = '-';
*utoap1(-(n + 1), s) = '\0';
} else {
*utoa(n, s) = '\0';
}
}

static char *utoa(unsigned n, char *s)
{
unsigned digit, tenth;

tenth = n / 10;
digit = n - 10 * tenth;
if (tenth != 0) {
s = utoa(tenth, s);
}
*s = (char)(digit + '0');
return s + 1;
}

static char *utoap1(unsigned n, char *s)
{
unsigned digit, tenth;

tenth = n / 10;
digit = n - 10 * tenth;
if (digit == 9) {
if (tenth != 0) {
s = utoap1(tenth, s);
} else {
*s++ = '1';
}
*s = '0';
} else {
if (tenth != 0) {
s = utoa(tenth, s);
}
*s = (char)(digit + '1');
}
return s + 1;
}
Aug 14 '07 #3
Tyler Smith wrote:
>
On 2007-08-10, pete <pf*****@mindspring.comwrote a really snazzy
recursive itoa function.

Thanks! It took me twenty minutes to walk myself through the code,
but now it looks so simple! How much time does one spend working on C
code until these sorts of solutions are obvious *before* you've been
shown them I wonder?
At least five years for myself.

--
pete
Aug 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: leonard greeff | last post by:
I want to know the correct way to answer exercise 1-11 of K&R. The only bug that I can find is that nw counts one to many words. (if there are 8 words, nw will be 9) Am I correct aor is there more...
46
by: Herrcho | last post by:
Hi~ i've studied C for a few months myself, and i'd appreciate it if anyone could improve my coding or correct it. the following is my solution to the K&R exercise 2-3 "Write the function...
12
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which...
12
by: Merrill & Michele | last post by:
It's very difficult to do an exercise with elementary tools. It took me about fifteen minutes to get exercise 1-7: #include <stdio.h> int main(int orange, char **apple) { int c; c=-5;...
8
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as...
16
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of...
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
9
by: JFS | last post by:
I know most of you have probably read "The C Programming Language" (K&R) at some point. Well here is something that is driving me crazy. The exercises are impossible (most of them) for me to do....
88
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.