Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Storing multiple 0's Posted by martin5150 on 5 May 2005 at 10:44 AM
I'm new to Pascal and programming, and I need some help with a problem. Here's what I need to do:

I want to use an array of type integer to store 10,000 numbers ranging from 0000 - 9999. The problem is, when I try storing a number with multiple 0's such as '0000', the trailing zeros are removed. I don't want them removed. How can I store these 0's without having them removed?

Thanks
Report
Re: Storing multiple 0's Posted by Gaashius on 5 May 2005 at 12:03 PM
: I'm new to Pascal and programming, and I need some help with a problem. Here's what I need to do:
:
: I want to use an array of type integer to store 10,000 numbers ranging from 0000 - 9999. The problem is, when I try storing a number with multiple 0's such as '0000', the trailing zeros are removed. I don't want them removed. How can I store these 0's without having them removed?
:
: Thanks
:
Well - you will need strings to do this. Here is a zero padding func:
function zpad(num:longint;zeroes:byte):string;
var
 d:longint;
 s:string;
begin
 str(num,s);
 for d := length(s) to zeroes-1 do s := concat('0',s);
 zpad:=s;
end;

You can also use a special type to track the range of the numbers:
type
 TSpec = 0..9999;


Here is a sample code for a program that operates with your problem:
uses
 crt;

type
 TSpec = 0..9999;

var
 myarray:array[1..10000] of TSpec;
 input,n:TSpec;

function zpad(num:longint;zeroes:byte):string;
var
 d:longint;
 s:string;
begin
 str(num,s);
 for d := length(s) to zeroes-1 do s := concat('0',s);
 zpad:=s;
end;

begin
 for n:=1 to 10000 do myarray[n]:=random(10000)+1;
 clrscr;
 write('Please type a number:'); readln(input);
 writeln; writeln(zpad(myarray[n]));
end.


****************
Any questions? Just ask!

GAASHIUS


Report
Re: Storing multiple 0's Posted by martin5150 on 5 May 2005 at 12:49 PM
Thanks! The only problem is with the example program you posted. I tried compiling that under Turbo Pascal 7, with range checking off, and it reported "Constant out of range." What could be wrong?
Report
Re: Storing multiple 0's Posted by zibadian on 5 May 2005 at 2:22 PM
: Thanks! The only problem is with the example program you posted. I tried compiling that under Turbo Pascal 7, with range checking off, and it reported "Constant out of range." What could be wrong?
:
N can only hold 0 to 9999, while in the for-do loops it also has the value of 10000. Solution: make n an integer.
Report
Re: Storing multiple 0's Posted by Phat Nat on 5 May 2005 at 2:41 PM
: Thanks! The only problem is with the example program you posted. I tried compiling that under Turbo Pascal 7, with range checking off, and it reported "Constant out of range." What could be wrong?
:

The problem is that n is ranging from 1-10000, but defined as a number between 0-9999, so 10000 is out of range. Try it with these fixes:

uses
 crt;

type
 TSpec = 0..9999;

var
 myarray:array[0..9999] of TSpec;
 input,n:TSpec;

function zpad(num:longint;zeroes:byte):string;
var
 d:longint;
 s:string;
begin
 str(num,s);
 for d := length(s) to zeroes-1 do s := concat('0',s);
 zpad:=s;
end;

begin
 for n:=0 to 9999 do myarray[n]:=random(10000){+1};
 clrscr;
 write('Please type a number:'); readln(input);
 writeln; writeln(zpad(myarray[n]));
end.



Although in order to store a number with zeros like Gashius said, you need to use strings. Also it depends on whether you need to have both 0, 00, 000 and 0000 or just the four digit one.
This would handle both:

VAR
   MyArray : Array[0..9999] Of String[4];

This would allow you to differentiate between 0, 00, 000 and 0000

Phat Nat
Report
Re: Storing multiple 0's Posted by martin5150 on 5 May 2005 at 5:05 PM
: : Thanks! The only problem is with the example program you posted. I tried compiling that under Turbo Pascal 7, with range checking off, and it reported "Constant out of range." What could be wrong?
: :
:
: The problem is that n is ranging from 1-10000, but defined as a number between 0-9999, so 10000 is out of range. Try it with these fixes:
:
:
: uses
:  crt;
: 
: type
:  TSpec = 0..9999;
: 
: var
:  myarray:array[0..9999] of TSpec;
:  input,n:TSpec;
: 
: function zpad(num:longint;zeroes:byte):string;
: var
:  d:longint;
:  s:string;
: begin
:  str(num,s);
:  for d := length(s) to zeroes-1 do s := concat('0',s);
:  zpad:=s;
: end;
: 
: begin
:  for n:=0 to 9999 do myarray[n]:=random(10000){+1};
:  clrscr;
:  write('Please type a number:'); readln(input);
:  writeln; writeln(zpad(myarray[n]));
: end.
: 

:
:
: Although in order to store a number with zeros like Gashius said, you need to use strings. Also it depends on whether you need to have both 0, 00, 000 and 0000 or just the four digit one.
: This would handle both:
:
:
: VAR
:    MyArray : Array[0..9999] Of String[4];
: 

: This would allow you to differentiate between 0, 00, 000 and 0000
:
: Phat Nat
:



The format I need to store them would be as follows:


0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010

and so on. So would using the code posted above work for what I am asking to do?

Report
Re: Storing multiple 0's Posted by Phat Nat on 5 May 2005 at 9:19 PM
: The format I need to store them would be as follows:
:
:
: 0000
: 0001
: 0002
: 0003
: 0004
: 0005
: 0006
: 0007
: 0008
: 0009
: 0010
:
: and so on. So would using the code posted above work for what I am asking to do?

Yeah. You could store them as an integer (or word). This would store them as 0,1,2,3,4,5,6,7,8,9,10,11,..., but then when you want to display them, just add the correct number of zeroes in front. Easiest way is to convert it to a string before displaying it and pre-pad it with zeroes until it's 4 characters long.

FUNCTION PadNumber(Number : Word; Digits : Byte) : String;
VAR
  X : Byte;
  S : String;
Begin
     Str(Number,S);
     While Length(S) < 4 Do S := '0' + S;
     PadNumber := S;
End;

Begin
     WriteLn(PadNumber(  0,4));   { Show '0' with four digits   }
     WriteLn(PadNumber( 17,4));   { Show '17' with four digits  }
     WriteLn(PadNumber(573,4));   { Show '573' with four digits }
     WriteLn(PadNumber( 12,8));   { Show '12' with eight digits }
End.


This will let you do any number up to 65535 and pad up to 254 zeroes in front (a little excessive, but that's how it goes ;)

Phat Nat

Report
Re: Storing multiple 0's Posted by zibadian on 5 May 2005 at 9:30 PM
: : The format I need to store them would be as follows:
: :
: :
: : 0000
: : 0001
: : 0002
: : 0003
: : 0004
: : 0005
: : 0006
: : 0007
: : 0008
: : 0009
: : 0010
: :
: : and so on. So would using the code posted above work for what I am asking to do?
:
: Yeah. You could store them as an integer (or word). This would store them as 0,1,2,3,4,5,6,7,8,9,10,11,..., but then when you want to display them, just add the correct number of zeroes in front. Easiest way is to convert it to a string before displaying it and pre-pad it with zeroes until it's 4 characters long.
:
:
: FUNCTION PadNumber(Number : Word; Digits : Byte) : String;
: VAR
:   X : Byte;
:   S : String;
: Begin
:      Str(Number,S);
:      While Length(S) < 4 Do S := '0' + S;
:      PadNumber := S;
: End;
: 
: Begin
:      WriteLn(PadNumber(  0,4));   { Show '0' with four digits   }
:      WriteLn(PadNumber( 17,4));   { Show '17' with four digits  }
:      WriteLn(PadNumber(573,4));   { Show '573' with four digits }
:      WriteLn(PadNumber( 12,8));   { Show '12' with eight digits }
: End.
: 

:
: This will let you do any number up to 65535 and pad up to 254 zeroes in front (a little excessive, but that's how it goes ;)
:
: Phat Nat
:
:
This code only pads to 4 digits. A small correction will remedy that.
Report
Re: Storing multiple 0's Posted by Gaashius on 6 May 2005 at 4:45 AM
: : : The format I need to store them would be as follows:
: : :
: : :
: : : 0000
: : : 0001
: : : 0002
: : : 0003
: : : 0004
: : : 0005
: : : 0006
: : : 0007
: : : 0008
: : : 0009
: : : 0010
: : :
: : : and so on. So would using the code posted above work for what I am asking to do?
: :
: : Yeah. You could store them as an integer (or word). This would store them as 0,1,2,3,4,5,6,7,8,9,10,11,..., but then when you want to display them, just add the correct number of zeroes in front. Easiest way is to convert it to a string before displaying it and pre-pad it with zeroes until it's 4 characters long.
: :
: :
: : FUNCTION PadNumber(Number : Word; Digits : Byte) : String;
: : VAR
: :   X : Byte;
: :   S : String;
: : Begin
: :      Str(Number,S);
: :      While Length(S) < 4 Do S := '0' + S;
: :      PadNumber := S;
: : End;
: : 
: : Begin
: :      WriteLn(PadNumber(  0));   { Show '0' with four digits   }
: :      WriteLn(PadNumber( 17));   { Show '17' with four digits  }
: :      WriteLn(PadNumber(573));   { Show '573' with four digits }
: :      WriteLn(PadNumber( 12));   { Show '12' with eight digits }
: : End.
: : 

: :
: : This will let you do any number up to 65535 and pad up to 254 zeroes in front (a little excessive, but that's how it goes ;)
: :
: : Phat Nat
: :
: :
: This code only pads to 4 digits. A small correction will remedy that.
:
Our friend needs to pad only to 4 digits, I think he don't need that parameter in the proc.

****************
Any questions? Just ask!

GAASHIUS


Report
Re: Storing multiple 0's Posted by Phat Nat on 7 May 2005 at 11:19 PM

: : Yeah. You could store them as an integer (or word). This would store them as 0,1,2,3,4,5,6,7,8,9,10,11,..., but then when you want to display them, just add the correct number of zeroes in front. Easiest way is to convert it to a string before displaying it and pre-pad it with zeroes until it's 4 characters long.
: :
: :
: : FUNCTION PadNumber(Number : Word; Digits : Byte) : String;
: : VAR
: :   X : Byte;
: :   S : String;
: : Begin
: :      Str(Number,S);
: :      While Length(S) < 4 Do S := '0' + S;
: :      PadNumber := S;
: : End;
: : 
: : Begin
: :      WriteLn(PadNumber(  0,4));   { Show '0' with four digits   }
: :      WriteLn(PadNumber( 17,4));   { Show '17' with four digits  }
: :      WriteLn(PadNumber(573,4));   { Show '573' with four digits }
: :      WriteLn(PadNumber( 12,8));   { Show '12' with eight digits }
: : End.
: : 

: :
: : This will let you do any number up to 65535 and pad up to 254 zeroes in front (a little excessive, but that's how it goes ;)
: :
: : Phat Nat
: :
: :
: This code only pads to 4 digits. A small correction will remedy that.


Thanks. Missed that one ;)

Phat Nat

Report
Re: Storing multiple 0's Posted by Gaashius on 8 May 2005 at 12:17 AM
:
: : : Yeah. You could store them as an integer (or word). This would store them as 0,1,2,3,4,5,6,7,8,9,10,11,..., but then when you want to display them, just add the correct number of zeroes in front. Easiest way is to convert it to a string before displaying it and pre-pad it with zeroes until it's 4 characters long.
: : :
: : :
: : : FUNCTION PadNumber(Number : Word; Digits : Byte) : String;
: : : VAR
: : :   X : Byte;
: : :   S : String;
: : : Begin
: : :      Str(Number,S);
: : :      While Length(S) < 4 Do S := '0' + S;
: : :      PadNumber := S;
: : : End;
: : : 
: : : Begin
: : :      WriteLn(PadNumber(  0,4));   { Show '0' with four digits   }
: : :      WriteLn(PadNumber( 17,4));   { Show '17' with four digits  }
: : :      WriteLn(PadNumber(573,4));   { Show '573' with four digits }
: : :      WriteLn(PadNumber( 12,8));   { Show '12' with eight digits }
: : : End.
: : : 

: : :
: : : This will let you do any number up to 65535 and pad up to 254 zeroes in front (a little excessive, but that's how it goes ;)
: : :
: : : Phat Nat
: : :
: : :
: : This code only pads to 4 digits. A small correction will remedy that.
:
:
: Thanks. Missed that one ;)
:
: Phat Nat
:
:
No problem - "everybody make mistakes sometimes"...

****************
Any questions? Just ask!

GAASHIUS





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.