- **C Programming**
(*http://www.velocityreviews.com/forums/f42-c-programming.html*)

- - **prints out an unsigned long in decimal**
(*http://www.velocityreviews.com/forums/t315507-prints-out-an-unsigned-long-in-decimal.html*)

prints out an unsigned long in decimalGiven only putchar (no sprintf, itoa, etc.) write a routine putlong
that prints out an unsigned long in decimal. No array allowed. I have no idea if we can't use array to solve the problem. |

Re: prints out an unsigned long in decimalMatt <jrefactors@hotmail.com> scribbled the following:
> Given only putchar (no sprintf, itoa, etc.) write a routine putlong > that prints out an unsigned long in decimal. No array allowed. > I have no idea if we can't use array to solve the problem. Hey come on. This problem is really quite easy. I'll leave you with a skeleton of the solution. void writeLong(unsigned long l) { putchar( /* what goes here? */ ); if (l) { writeLong( /* what goes here? */ ); } } I managed to solve the problem in a way similar to the above without using arrays, or indeed other variables than l, at all. No need for sprintf, itoa, etc either. -- /-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\ | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++| | http://www.helsinki.fi/~palaste W++ B OP+ | \----------------------------------------- Finland rules! ------------/ "Life without ostriches is like coffee with milk." - Mika P. Nieminen |

Re: prints out an unsigned long in decimal"Matt" <jrefactors@hotmail.com> wrote in message
news:ba8a039e.0309281025.4079eeab@posting.google.c om... > Given only putchar (no sprintf, itoa, etc.) write a routine putlong > that prints out an unsigned long in decimal. No array allowed. > > I have no idea if we can't use array to solve the problem. One idea is to go with something like: unsigned long exp = 10; while(exp<val) exp*=10; while( exp/=10 ) putchar( '0'+(val/exp)%10 ); Bug left in on purpose, and optimizations are possible. Cheers, Ivan -- http://ivan.vecerina.com |

Re: prints out an unsigned long in decimal"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:
> "Matt" <jrefactors@hotmail.com> wrote in message > news:ba8a039e.0309281025.4079eeab@posting.google.c om... >> Given only putchar (no sprintf, itoa, etc.) write a routine putlong >> that prints out an unsigned long in decimal. No array allowed. >> >> I have no idea if we can't use array to solve the problem. > > One idea is to go with something like: > unsigned long exp = 10; > while(exp<val) exp*=10; > while( exp/=10 ) putchar( '0'+(val/exp)%10 ); > > Bug left in on purpose, and optimizations are possible. Which bug? I count at least two. -- Richard Heathfield : binary@eton.powernet.co.uk "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton |

Re: prints out an unsigned long in decimalMatt wrote:
> > Given only putchar (no sprintf, itoa, etc.) write a routine putlong > that prints out an unsigned long in decimal. No array allowed. > > I have no idea if we can't use array to solve the problem. Because it's an example program on page 64 or K&R2 which uses an array as temporary storage. The alogrithm to convert binary to 'decimal' generates digits, least significant first. Then the array is reversed (another example of how to reverse a string). They don't want you to copy the example from the book. You can do it without an array. Think recursion. Good luck. -- Joe Wright mailto:joewwright@earthlink.net "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- |

Re: prints out an unsigned long in decimal"Richard Heathfield" <dontmail@address.co.uk.invalid> wrote in message news:bl7m4k$6i9$1@hercules.btinternet.com... > "Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote: > > > "Matt" <jrefactors@hotmail.com> wrote in message > > news:ba8a039e.0309281025.4079eeab@posting.google.c om... > >> Given only putchar (no sprintf, itoa, etc.) write a routine putlong > >> that prints out an unsigned long in decimal. No array allowed. > >> > >> I have no idea if we can't use array to solve the problem. > > > > One idea is to go with something like: > > unsigned long exp = 10; > > while(exp<val) exp*=10; > > while( exp/=10 ) putchar( '0'+(val/exp)%10 ); > > > > Bug left in on purpose, and optimizations are possible. > > Which bug? I count at least two. shush! ;) Is a portability problem included in your count? Regards, Ivan -- http://ivan.vecerina.com |

Re: prints out an unsigned long in decimal"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:
> > "Richard Heathfield" <dontmail@address.co.uk.invalid> wrote in message > news:bl7m4k$6i9$1@hercules.btinternet.com... >> "Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote: >> >> > "Matt" <jrefactors@hotmail.com> wrote in message >> > news:ba8a039e.0309281025.4079eeab@posting.google.c om... >> >> Given only putchar (no sprintf, itoa, etc.) write a routine putlong >> >> that prints out an unsigned long in decimal. No array allowed. >> >> >> >> I have no idea if we can't use array to solve the problem. >> > >> > One idea is to go with something like: >> > unsigned long exp = 10; >> > while(exp<val) exp*=10; >> > while( exp/=10 ) putchar( '0'+(val/exp)%10 ); >> > >> > Bug left in on purpose, and optimizations are possible. >> >> Which bug? I count at least two. > > shush! ;) Is a portability problem included in your count? Well, the first is that it basically gives the wrong answer sometimes. The second is a namespace issue. -- Richard Heathfield : binary@eton.powernet.co.uk "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton |

Re: prints out an unsigned long in decimal"Richard Heathfield" <dontmail@address.co.uk.invalid> wrote in message
news:bl8hst$6rv$2@sparta.btinternet.com... | "Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote: | > "Richard Heathfield" <dontmail@address.co.uk.invalid> wrote in message | > news:bl7m4k$6i9$1@hercules.btinternet.com... | >> "Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote: | >> > "Matt" <jrefactors@hotmail.com> wrote in message | >> > news:ba8a039e.0309281025.4079eeab@posting.google.c om... | >> >> Given only putchar (no sprintf, itoa, etc.) write a routine putlong | >> >> that prints out an unsigned long in decimal. No array allowed. | >> >> | >> >> I have no idea if we can't use array to solve the problem. | >> > | >> > One idea is to go with something like: | >> > unsigned long exp = 10; | >> > while(exp<val) exp*=10; | >> > while( exp/=10 ) putchar( '0'+(val/exp)%10 ); | >> > | >> > Bug left in on purpose, and optimizations are possible. | >> | >> Which bug? I count at least two. | > | > shush! ;) Is a portability problem included in your count? Hi Richard, | Well, the first is that it basically gives the wrong answer sometimes. Yes - a classic bounds problem I left for Matt to eventually find. | The second is a namespace issue. I am not sure what you mean by this. I assume you refer to C name spaces, and not C++ namespace-s. A conflict with the exp() function if using <math.h> ? What I though of as a portability problem was the '0'+.... It could be replaced with "0123456789"[...], or ...["0123456789"] ;) Also, note that the code I posted in this thread is not a recommendation, just a hint at a possible approach. Regards, Ivan -- http://ivan.vecerina.com |

Re: prints out an unsigned long in decimalIvan Vecerina wrote:
>> The second is a namespace issue. > I am not sure what you mean by this. I assume you refer > to C name spaces, and not C++ namespace-s. > A conflict with the exp() function if using <math.h> ? That, plus val not being declared... :) -- Martijn http://www.sereneconcepts.nl |

Re: prints out an unsigned long in decimalIvan Vecerina wrote:
> A conflict with the exp() function if using <math.h> ? How about regardless of whether or not using <math.h> ? -- pete |

All times are GMT. The time now is 10:01 AM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.