| | |
number of digits in a base
![]() |
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
.
Thanks for any help you can give
. C Syntax (Toggle Plain Text)
int numdigits(int x) { int t = x, n = 1; while(t>= 10) { n++; t = t/10; } return n; }
"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack."
-George Carrette
-George Carrette
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.
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.
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.
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
-George Carrette
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.
>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.
>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.
![]() |
Similar Threads
- Request For Help With Any Base to Any Base Algorithm (Python)
- Prime Number (Posting Games)
- Printing binary (C++)
- Address Book Data Base (C++)
- counting digits (Assembly)
- Base conversion (C)
- Base converter (C++)
- Math/computerscience Log Problem- Helpppp (Computer Science)
Other Threads in the C Forum
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






