Lazarus

Free Pascal => Beginners => Topic started by: JLWest on June 18, 2022, 09:07:47 pm

Title: Single deminision arrays(Solved)
Post by: JLWest on June 18, 2022, 09:07:47 pm
is there a way to declare an static single dimension array as 1 base Say 1..13 as opposed to 0 based 0..12
Title: Re: Single deminision arrays
Post by: dsiders on June 18, 2022, 09:23:35 pm
is there a way to declare an static single dimension array as 1 base Say 1..13 as opposed to 0 based 0..12

Sure. It's covered in the Language reference: https://www.freepascal.org/docs-html/current/ref/refse24.html#x55-750004.4
Title: Re: Single deminision arrays
Post by: jamie on June 18, 2022, 10:49:49 pm
is there a way to declare an static single dimension array as 1 base Say 1..13 as opposed to 0 based 0..12

Static array of what?

Code: Pascal  [Select][+][-]
  1. Const A:Array[1..10] of Byte =(1,2,3,4,5,6,7,8,9,10);  
  2.  
You don't specify the data type so I just winged it..


Title: Re: Single deminision arrays
Post by: JLWest on June 18, 2022, 10:54:45 pm
@Jamie
A static array of integers

So the array elements could be 1 ro 52
Title: Re: Single deminision arrays
Post by: jamie on June 18, 2022, 10:59:57 pm
Then just use what I showed and change the type to integer and fill the initial values 1..52 at edit time.

good luck.
Title: Re: Single deminision arrays
Post by: JLWest on June 18, 2022, 11:11:53 pm
I don't understand. You defined the array as a Const.

 Do they have to be a Const?

I'll try it.

 Const Deck : Array[1..52] of Integer;
 Const South : Array[1..13] of integer;
 Const North : Array[1..13] of integer;
 Const East   : Array[1..13] of integer;
 Const West  : Array[1..13] of integer; 
 
Title: Re: Single deminision arrays
Post by: JLWest on June 18, 2022, 11:30:53 pm
That didn't work. Got an error 'unit1.pas(334,38) Fatal: Syntax error, "=" expected' but ";" found on Const Deck : Array[1..52] of Integer;  It looks like it looking for values in the array elements.

  Deck : Array[1..52] of CardRecords;
  South : Array[1..13] of integer;
  North : Array[1..13] of integer;
  East   : Array[1..13] of integer;
  West  : Array[1..13] of integer;

The Deck array holds a record with card attributes, suit, position ect.
The North, South,East and West represent the hands at a bridge table. The integers
in the hand arrays could be an integer value of 1..52 and would be a card of the Deck.

I need to change the values in al 5 arrays while playing a hand of bridge.

Is there a way to have a static array that is 1 based as opposed to 0 based?     
Title: Re: Single deminision arrays
Post by: wp on June 19, 2022, 12:21:09 am
You declare a static array like any other variable and specify the index limits in the declaration; in this case you must enter the values one by one:
Code: Pascal  [Select][+][-]
  1. var
  2.   South: array[1..3] of Integer;
  3. begin
  4.   South[1] := 10;
  5.   South[2] := 32;
  6.   South[3] := 41;

You can also initialize the array element immediately in the declaration by calling
Code: Pascal  [Select][+][-]
  1. var
  2.   South:array[1..3] of Integer = (10, 32, 41);
  3. // or
  4. const
  5.   South: array[1..3] of Integer = (10, 32, 41);
Although in the second case, the array is declared as "const", it still can be changed in the default settings of FPC/Lazarus. A pecularity of the Pascal language. Read about "typed constants" in https://www.freepascal.org/docs-html/ref/refse10.html.
Title: Re: Single deminision arrays
Post by: jamie on June 19, 2022, 02:05:53 am
That didn't work. Got an error 'unit1.pas(334,38) Fatal: Syntax error, "=" expected' but ";" found on Const Deck : Array[1..52] of Integer;  It looks like it looking for values in the array elements.

  Deck : Array[1..52] of CardRecords;
  South : Array[1..13] of integer;
  North : Array[1..13] of integer;
  East   : Array[1..13] of integer;
  West  : Array[1..13] of integer;

The Deck array holds a record with card attributes, suit, position ect.
The North, South,East and West represent the hands at a bridge table. The integers
in the hand arrays could be an integer value of 1..52 and would be a card of the Deck.

I need to change the values in al 5 arrays while playing a hand of bridge.

Is there a way to have a static array that is 1 based as opposed to 0 based?   

You answered your own question.

 what you are looking for is a fixed. Dynamic arrays are 0 based. just use a fixed array somewhere.
Code: Pascal  [Select][+][-]
  1. Var
  2.  South :Array[1..13] of integer;
  3.  ....
  4.  

 If you are having problems passing that to functions then create a type.
Code: Pascal  [Select][+][-]
  1.  Type
  2.   TMyCompasArray = Array[1..13] of integer;
  3.   // when you define it some where.
  4. Var
  5.  South : TMyCompasArray;
  6.  

 When passing these to functions create the function to accept this type as a Reference so you make changes to it.
For example
  Function AcceptMyCompasArrays(Var aCompasArray:TMyCompasArray);.....

 if you stick with using this type you can pass a reference to the array of any procedure or function.

 Hoped that gave you a little more insight.

Title: Re: Single deminision arrays
Post by: JLWest on June 19, 2022, 04:00:17 am
Yea, That did it. Declaring as a type.

Thanks all.
Title: Re: Single deminision arrays(Solved)
Post by: MarkMLl on June 19, 2022, 10:12:29 am
Also note that declarations such as

Code: Pascal  [Select][+][-]
  1. var
  2.   South:array[-1..1] of Integer = (10, 32, 41);
  3.  

generally work as expected.

MarkMLl
Title: Re: Single deminision arrays(Solved)
Post by: SymbolicFrank on June 20, 2022, 09:36:04 am
@JLWest: did you check out sets? That might be what you're looking for. The elements can have a numeric value and there are functions to translate between that value and the name. You can have at most 255 elements in a set, but that won't be a problem in this case.
Title: Re: Single deminision arrays(Solved)
Post by: JLWest on June 20, 2022, 07:12:17 pm
No, I never considered sets. I have a array of records.
Title: Re: Single deminision arrays(Solved)
Post by: winni on June 20, 2022, 07:56:30 pm
Hi!

Remember that you can avoid the computing of the index if you choose the right range of an array:

Code: Pascal  [Select][+][-]
  1. LastCenturyInfo : array[1901..2000] of string;
  2.  

Winni
Title: Re: Single deminision arrays(Solved)
Post by: Thaddy on June 20, 2022, 10:24:40 pm
No, I never considered sets. I have a array of records.
That would be ill informed for a card game (for lack of an abusive term) because for a card game you can use include/exclude, so the set will represent the cards that are still in the deck or already played. If you write a card game, sets are the way to go.
E.g. Poker can be written in less than 100 lines using sets. Bridge a bit more, but that is just handling logic, so does not count to your problem.
An array of records can be handled with e.g. the sets in rtl-generics.
Title: Re: Single deminision arrays(Solved)
Post by: MarkMLl on June 21, 2022, 11:27:06 am
No, I never considered sets. I have a array of records.
That would be ill informed for a card game (for lack of an abusive term) because for a card game you can use include/exclude, so the set will represent the cards that are still in the deck or already played. If you write a card game, sets are the way to go.
E.g. Poker can be written in less than 100 lines using sets. Bridge a bit more, but that is just handling logic, so does not count to your problem.
An array of records can be handled with e.g. the sets in rtl-generics.

Might get more complicated than a simple set if more than one deck is involved... or a game has more than one indistinguishable joker.

MarkMLl
Title: Re: Single deminision arrays(Solved)
Post by: Thaddy on June 21, 2022, 11:38:34 am
Might get more complicated than a simple set if more than one deck is involved... or a game has more than one indistinguishable joker.

MarkMLl
Actually not. The logic is the same. Only provision is to identify the deck in the record.
Title: Re: Single deminision arrays(Solved)
Post by: JLWest on July 05, 2022, 10:18:15 pm
@Thaddy

No disrespect Thaddy but I don't think a bridge game came be written in less than 200 lines.

I'm at about 3,000 and and probably a 1,000 more line to finish. 
Title: Re: Single deminision arrays(Solved)
Post by: MarkMLl on July 05, 2022, 10:22:46 pm
No disrespect Thaddy but I don't think a bridge game came be written in less than 200 lines.

Thaddy loves a challenge :-)

MarkMLl
TinyPortal © 2005-2018