Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count the number of loops 2

Status
Not open for further replies.

superblades

Technical User
Nov 17, 2005
2
GB
Im trying to create a program that outputs the number of digits in a given integer e.g if i input 4392 it would output 4 or if i input 2499238 it would output 7 how can i do this with a loop?

Thanks

Superblades
 
This sounds like schoolwork to me.
No-one in their right mind would use a loop!


Trojan.
 
im just new to it all, ive created a loop that divides an integer by 10 until it gets past the decimal point e.g. if i supply 123 it would loop until it got 0.123 i just need to work out how to count the number of loops it took to get the integer past the decimal point?

Help Please! :)
 
<Hope its not schoolwork>

Well ... one way :

Code:
int i = 1234;
char szString[128];
sprintf(szString, "%d", i);
int iNumCharacters = (int)strlen(szString);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
What about this?

Code:
#include <math.h>

int number = 1234;
int numChars=(int) log10(number*(double)1.0) + 1;

Cheers,
Dian
 
(Piling on.)

You don't need the [tt]strlen[/tt] call after [tt]sprintf[/tt], you can just use [tt]sprintf[/tt]'s return value.
Code:
#include <stdio.h>

int main(void)
{
   int value[] = 
   {
      1,12,123,1234,12345,123456,1234567,12345678,123456789
   };
   size_t i;
   for ( i = 0; i < sizeof value / sizeof *value; ++i )
   {
      char text[128];
      [blue]int digits = sprintf(text, "%d", value[i]);[/blue]
      printf("value[%lu] = %d, text = \"%s\", digits = %d\n", 
             (long unsigned)i, value[i], text, digits);
   }
   return 0;
}

/* my output
value[0] = 1, text = "1", digits = 1
value[1] = 12, text = "12", digits = 2
value[2] = 123, text = "123", digits = 3
value[3] = 1234, text = "1234", digits = 4
value[4] = 12345, text = "12345", digits = 5
value[5] = 123456, text = "123456", digits = 6
value[6] = 1234567, text = "1234567", digits = 7
value[7] = 12345678, text = "12345678", digits = 8
value[8] = 123456789, text = "123456789", digits = 9
*/
 
That is a nice solution Dian !
And thanks for the tip on sprintf Dave :)

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
You could also take a log to the base 10 of the number, take the integer part and add one.


Trojan.
 
(1) Using the log solution. How do you think logs are calculated? Is this less, or more work than converting to text and counting the digits? Does this matter if your code is later used in an application where someone wishes to find the number of digits in 50 million integers?

(2) looking at TrojanWarBlades' post: some of the solutions don't show a loop, but I can't think of any memory-efficient way to do the job without a loop in there somewhere. Even calculating a log is an iterative, looping process. It's worth being aware of what's going on behind the scenes.

(3) Dividing by ten until you're past the decimal point is probably similar to the solution your teacher was looking for (because this sounds like homework to me, too). But why use a floating point format when you're starting with a decimal? Think what division by ten does when everything is strictly integer.
 
lionelhill,

You're right, of course, but the reason I pointed out the log process is because it's very short and verys simple. Also you might find the speed different not as severe as you might think.
You forget that the algorithm for the log is optimised C/assembler code whereas your loop is interpreted looping bytecodes.
Also, the other reason I gave a solution that did NOT include a loop was because if this WERE schoolwork then I would not be helping and thereby breaching the usage terms of this site!



Trojan.
 
Spot on, I agree about homework. In the same spirit, my aim wasn't to criticise, but to suggest to the original writer that when (as usual) there are a multitude of ways of solving the problem, it's a good idea to look at what each is really doing, and choose the one that suits the situation. It bothers me intensely that some modern programers just bolt together bits of libraries, and the small subset of instructions from their favourite language that they've bothered to remember, and never think what they're really asking the hardware to do.
 
ok, I'll go with that.

But there is an argument that so long as you are relying on something that is written by a reputable source then you shouldn't have to worry about how it works. Indeed this is one of the main principles of OO design and programming.

It is fair to say, though, that an understanding of what you are dealing with can never be a bad thing.



Trojan.
 
Well, I have some programming experience and, as a normal rule, I don't think what the hardware is doing.

Unless there's a critical performance issue, I think it's better to use well known solutions everyone knows and you're confortable with.

Just in a few cases where you're trying to get the most from the processor you can go down to the hell of assembler to investigate what a function is actually doing.

For the rest of the cases, that constitutes the real life, the "do it as you know it works" approach is definitley fine.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top