Recent

Author Topic: Single deminision arrays(Solved)  (Read 1218 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1169
Single deminision arrays(Solved)
« 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
« Last Edit: June 19, 2022, 04:00:52 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dsiders

  • Hero Member
  • *****
  • Posts: 683
Re: Single deminision arrays
« Reply #1 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
Preview Lazarus 2.3.0 documentation at: https://dsiders.gitlab.io/lazdocsnext

jamie

  • Hero Member
  • *****
  • Posts: 4763
Re: Single deminision arrays
« Reply #2 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..


The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1169
Re: Single deminision arrays
« Reply #3 on: June 18, 2022, 10:54:45 pm »
@Jamie
A static array of integers

So the array elements could be 1 ro 52
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 4763
Re: Single deminision arrays
« Reply #4 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.
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1169
Re: Single deminision arrays
« Reply #5 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; 
 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1169
Re: Single deminision arrays
« Reply #6 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?     
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

wp

  • Hero Member
  • *****
  • Posts: 10060
Re: Single deminision arrays
« Reply #7 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.

jamie

  • Hero Member
  • *****
  • Posts: 4763
Re: Single deminision arrays
« Reply #8 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.

The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1169
Re: Single deminision arrays
« Reply #9 on: June 19, 2022, 04:00:17 am »
Yea, That did it. Declaring as a type.

Thanks all.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

MarkMLl

  • Hero Member
  • *****
  • Posts: 5201
Re: Single deminision arrays(Solved)
« Reply #10 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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1086
Re: Single deminision arrays(Solved)
« Reply #11 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.

JLWest

  • Hero Member
  • *****
  • Posts: 1169
Re: Single deminision arrays(Solved)
« Reply #12 on: June 20, 2022, 07:12:17 pm »
No, I never considered sets. I have a array of records.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

winni

  • Hero Member
  • *****
  • Posts: 3134
Re: Single deminision arrays(Solved)
« Reply #13 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

Thaddy

  • Hero Member
  • *****
  • Posts: 11933
Re: Single deminision arrays(Solved)
« Reply #14 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.
« Last Edit: June 20, 2022, 10:32:32 pm by Thaddy »
Black themes should be banned.

 

TinyPortal © 2005-2018