number of digits in a base

Reply

Join Date: Sep 2007
Posts: 27
Reputation: winky is an unknown quantity at this point 
Solved Threads: 0
winky's Avatar
winky winky is offline Offline
Light Poster

number of digits in a base

 
0
  #1
Sep 8th, 2008
I am supposed to rewrite the code below to "compute the number of digits to any base(such as base 2, base 16, base 8)." Just wondering if any of you can provide any explanation or pseudocode on how to go about doing this?

Thanks for any help you can give .
  1. int numdigits(int x)
  2. {
  3. int t = x, n = 1;
  4. while(t>= 10)
  5. {
  6. n++;
  7. t = t/10;
  8. }
  9. return n;
  10. }
"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack."
-George Carrette
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: number of digits in a base

 
0
  #2
Sep 8th, 2008
you must mean the number of digits in a number,
You're program is almost right, except the while statement should check for t > 0, not t >= 10. That function doesn't need variable t at all, just use the parameter variable x.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 27
Reputation: winky is an unknown quantity at this point 
Solved Threads: 0
winky's Avatar
winky winky is offline Offline
Light Poster

Re: number of digits in a base

 
0
  #3
Sep 8th, 2008
The code that I wrote is supplied to us; we are supposed to change it, so that it computes the number of digits to any base. I don't exactly understand what that means or how I would go about doing that.

At first, I thought it was asking how many digits are in a particular base( i.e. base 8 has 8 digits in its alphabet [0-7]), but that seems too easy.

I believe it wants us to write a program that we input a number, and we have to be able to output how many digits are output to a particular base.
"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack."
-George Carrette
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: number of digits in a base

 
0
  #4
Sep 8th, 2008
I guess you would have to convert the number to a string and then return the length of the string. For example hex digits can't be represented by a simple integer -- only strings.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: number of digits in a base

 
0
  #5
Sep 8th, 2008
>I believe it wants us to write a program that we input a number, and we
>have to be able to output how many digits are output to a particular base.
That's trivial too. numdigits as it stands is specific to decimal only because it uses literal 10's in the loop condition and division step.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC