* * *

Author Topic: simple problem: count digits of number  (Read 472 times)

chickpea

  • Newbie
  • Posts: 2
simple problem: count digits of number
« on: January 30, 2018, 12:02:33 pm »
Hi there,

Here's my approach to counting the digits of a number in pascal.
It does work, but as soon as I enter a number with more than 5 digits the program returns a 5.
I don't get why. If anyone could give me a hint that'd be great.
Comments and program names in German, but I suppose you get the idea.

Thanks in advance!




program ZifferAnzahl (input, output);


  type
  tNatZahl = 0..maxint;
 
  var
  inZahl : tNatZahl;
 
function countZiff (inZahl : tNatZahl) : tNatZahl;

{ gibt Anzahl der Ziffern von inZahl zurück }

  var
  i,
  j : tNatZahl;
 
begin

  { initialisierung }
 
  i := inZahl;
  j := 0;
 
  { inZahl so oft durch 10 Teilen, bis 0 erreicht wird }
 
  while i > 0 do
    begin
    i := i div 10;
    j := j + 1
    end;
 
  countZiff := j
end; { countZiff }


begin
  writeln ('Bitte Zahl eingeben');
  readln (inZahl);
  writeln(countZiff(inZahl))
end. { ZifferAnzahl }
   

rvk

  • Hero Member
  • *****
  • Posts: 3120
Re: simple problem: count digits of number
« Reply #1 on: January 30, 2018, 12:19:07 pm »
You are probably in TP mode. {$mode TP}
There maxint is a 2 byte integer.
Which is maximum 32767 (see, 5 digits)

Add {$mode objfpc} just below program and your Integer will be 4 bytes.

B.T.W. you can check this by adding the lines
Code: Pascal  [Select]
  1. writeln(sizeof(inZahl));
  2. writeln(maxint);

Of course you can also do this instead of changing mode:
Code: Pascal  [Select]
  1. type
  2.   tNatZahl = 0..99999999;
« Last Edit: January 30, 2018, 12:21:54 pm by rvk »

chickpea

  • Newbie
  • Posts: 2
Re: simple problem: count digits of number
« Reply #2 on: January 30, 2018, 04:05:34 pm »
Great, that was the error indeed. Thanks for explaining.

Working correctly when change to

tNatZahl = 0..999999999;

Cheers

Bart

  • Hero Member
  • *****
  • Posts: 2856
    • Bart en Mariska's Webstek
Re: simple problem: count digits of number
« Reply #3 on: January 30, 2018, 09:08:30 pm »
Perhaps: tNatZahl = QWord;

Another dummy approach  O:-)
Code: Pascal  [Select]
  1. uses sysutils;
  2.  
  3. function countZiff (inZahl : tNatZahl) : tNatZahl;
  4. begin
  5.   Result := Length(IntToStr(inZahl));
  6. end;
  7.  

or
Code: Pascal  [Select]
  1. uses Math;
  2.  
  3. function countZiff (inZahl : tNatZahl) : tNatZahl;
  4. begin
  5.   Result := Floor(Log10(inZahl));
  6. end;
  7.  

Bart
« Last Edit: January 30, 2018, 09:32:20 pm by Bart »

furious programming

  • New member
  • *
  • Posts: 13
  • I click a little.
    • TreeStructInfo - format for text and binary configuration files
Re: simple problem: count digits of number
« Reply #4 on: February 05, 2018, 05:43:04 am »
@chickpea: you don't need a custom function, just use some helpers from SysUtils unit:

Code: Pascal  [Select]
  1. Count := MyIntVar.ToString.Length;

It's readable enough.

Bart

  • Hero Member
  • *****
  • Posts: 2856
    • Bart en Mariska's Webstek
Re: simple problem: count digits of number
« Reply #5 on: February 05, 2018, 01:45:10 pm »
Code: Pascal  [Select]
  1. Count := MyIntVar.ToString.Length;

That's basically the same as I wrote above (Length(IntToStr(InZahl))), proabbly a bit less efficient (unless all this code for the helper is inlined), albeit more fancy.
The Log10 approach might actually be faster?

Bart

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus