Velocity Reviews > prints out an unsigned long in decimal

# prints out an unsigned long in decimal

Matt
Guest
Posts: n/a

 09-28-2003
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.

Joona I Palaste
Guest
Posts: n/a

 09-28-2003
Matt <(E-Mail Removed)> 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 ((E-Mail Removed)) ---------------------------\
| 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

Ivan Vecerina
Guest
Posts: n/a

 09-28-2003
"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

Richard Heathfield
Guest
Posts: n/a

 09-28-2003
"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:

> "Matt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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 : http://www.velocityreviews.com/forums/(E-Mail Removed)
"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

Joe Wright
Guest
Posts: n/a

 09-28-2003
Matt 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 (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Ivan Vecerina
Guest
Posts: n/a

 09-29-2003

"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:bl7m4k\$6i9\$(E-Mail Removed)...
> "Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:
>
> > "Matt" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) 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

Richard Heathfield
Guest
Posts: n/a

 09-29-2003
"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:

>
> "Richard Heathfield" <(E-Mail Removed)> wrote in message
> news:bl7m4k\$6i9\$(E-Mail Removed)...
>> "Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:
>>
>> > "Matt" <(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed) 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 : (E-Mail Removed)
"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

Ivan Vecerina
Guest
Posts: n/a

 09-29-2003
"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:bl8hst\$6rv\$(E-Mail Removed)...
| "Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:
| > "Richard Heathfield" <(E-Mail Removed)> wrote in message
| > news:bl7m4k\$6i9\$(E-Mail Removed)...
| >> "Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:
| >> > "Matt" <(E-Mail Removed)> wrote in message
| >> > news:(E-Mail Removed) 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

Martijn
Guest
Posts: n/a

 09-29-2003
Ivan 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

pete
Guest
Posts: n/a

 09-29-2003
Ivan Vecerina wrote:

> A conflict with the exp() function if using <math.h> ?

How about regardless of whether or not using <math.h> ?

--
pete

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post pozz C Programming 12 03-20-2011 11:32 PM veryhotsausage C++ 1 07-04-2008 05:41 PM Daniel Rudy C Programming 5 09-20-2005 02:37 AM ashtonn@gmail.com Python 1 06-01-2005 07:00 PM George Marsaglia C Programming 1 07-08-2003 05:16 PM

Advertisments