#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Storing multiple 0's

Posts: 6Member
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
· ·

• Posts: 349Member
: 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:
[code]
var
d:longint;
s:string;
begin
str(num,s);
for d := length(s) to zeroes-1 do s := concat('0',s);
end;
[/code]
You can also use a special type to track the range of the numbers:
[code]
type
TSpec = 0..9999;
[/code]

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

type
TSpec = 0..9999;

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

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

begin
for n:=1 to 10000 do myarray[n]:=random(10000)+1;
clrscr;
end.
[/code]

****************

:-) [b][blue]GAASHIUS[/blue][/b] :-)

· ·
• Posts: 6Member
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?
· ·
• Posts: 6,349Member
: 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.
· ·
• Posts: 757Member
: 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:

[code]
uses
crt;

type
TSpec = 0..9999;

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

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

begin
for n:=[b]0 to 9999[/b] do myarray[n]:=random(10000)[b]{+1}[/b];
clrscr;
end.
[/code]

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:

[code]
VAR
MyArray : Array[0..9999] Of String[4];
[/code]
This would allow you to differentiate between 0, 00, 000 and 0000

Phat Nat
· ·
• Posts: 6Member
: : 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:
:
: [code]
: uses
: crt;
:
: type
: TSpec = 0..9999;
:
: var
: myarray:array[[b]0..9999[/b]] of TSpec;
: input,n:TSpec;
:
: var
: d:longint;
: s:string;
: begin
: str(num,s);
: for d := length(s) to zeroes-1 do s := concat('0',s);
: end;
:
: begin
: for n:=[b]0 to 9999[/b] do myarray[n]:=random(10000)[b]{+1}[/b];
: clrscr;
: end.
: [/code]
:
:
: 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:
:
: [code]
: VAR
: MyArray : Array[0..9999] Of String[4];
: [/code]
: 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?

· ·
• Posts: 757Member
: 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.

[code]
FUNCTION PadNumber(Number : Word; Digits : Byte) : String;
VAR
X : Byte;
S : String;
Begin
Str(Number,S);
While Length(S) < 4 Do S := '0' + 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.
[/code]

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

· ·
• Posts: 6,349Member
: : 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.
:
: [code]
: FUNCTION PadNumber(Number : Word; Digits : Byte) : String;
: VAR
: X : Byte;
: S : String;
: Begin
: Str(Number,S);
: While Length(S) < [red]4[/red] Do S := '0' + 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.
: [/code]
:
: 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.
· ·
• Posts: 349Member
: : : 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.
: :
: : [code]
: : FUNCTION PadNumber(Number : Word[red]; Digits : Byte[/red]) : String;
: : VAR
: : X : Byte;
: : S : String;
: : Begin
: : Str(Number,S);
: : While Length(S) < 4 Do S := '0' + 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.
: : [/code]
: :
: : 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.

****************

:-) [b][blue]GAASHIUS[/blue][/b] :-)

· ·
• Posts: 757Member

: : 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.
: :
: : [code]
: : FUNCTION PadNumber(Number : Word; Digits : Byte) : String;
: : VAR
: : X : Byte;
: : S : String;
: : Begin
: : Str(Number,S);
: : While Length(S) < [red]4[/red] Do S := '0' + 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.
: : [/code]
: :
: : 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

· ·
• Posts: 349Member
:
: : : 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.
: : :
: : : [code]
: : : FUNCTION PadNumber(Number : Word; Digits : Byte) : String;
: : : VAR
: : : X : Byte;
: : : S : String;
: : : Begin
: : : Str(Number,S);
: : : While Length(S) < [red]4[/red] 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.
: : : [/code]
: : :
: : : 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"...

****************