Recent

Author Topic: Variables as indexes of arrays (Solved)  (Read 2254 times)

JMarques

  • New Member
  • *
  • Posts: 13
Variables as indexes of arrays (Solved)
« on: March 30, 2023, 11:54:58 pm »
Hello, I am using the Free Pascal IDE for Win32, version 1.0.12, compiler version 3.2.2

In this very simple program, row[1][0] is made equal to triad[0], which is 1. But when I ask the value of row[j][0] for j=1, I get the value 0. The problem appears also for other values of j and for the other members of row[j], but I believe that just concentrating in this fact may help to find the solution. Thanks for any help!

Code: Pascal  [Select][+][-]
  1. type
  2.   Triads = Array [0..2] of Integer;
  3. var
  4.   row: Array [0..3] of Triads;
  5.   j: Integer;
  6. Const  
  7.   triad: Triads = (1, 2, 3);
  8. Begin
  9. row[1][0] := triad[0];
  10. Writeln (row[1][0]);
  11. j:=1;
  12. Writeln (row[j][0]);
  13. Readln;
  14. End.
  15.  

« Last Edit: April 01, 2023, 12:03:10 pm by JMarques »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Variables as indexes of arrays
« Reply #1 on: March 31, 2023, 12:04:50 am »
Code: Pascal  [Select][+][-]
  1. type
  2.   Triads = Array [0..2] of Integer;
  3. var
  4.   row: Array [0..3] of Triads;
  5.   i: Integer;
  6. Const
  7.   triad: Triads = (1, 2, 3);
  8. Begin
  9. row[1][0] := triad[0];
  10. Writeln (row[1][0]);
  11. i:=1;
  12. Writeln (row[0]);
  13. Readln;
  14. End.
What do you expect to get by doing:
Code: Pascal  [Select][+][-]
  1. Writeln (row[0]);
row[0] is no qualified number for writeln at all, it is part of Triads but not a number...
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Variables as indexes of arrays
« Reply #2 on: March 31, 2023, 12:07:15 am »
( I attached a text file with the program because it doesn't appear correctly in this box.)
Please use on forum topic editor the [ # ] button to generate [ code=pascal ][ /code ] and put your code inside the ][
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

JMarques

  • New Member
  • *
  • Posts: 13
Re: Variables as indexes of arrays
« Reply #3 on: March 31, 2023, 12:17:07 am »
What do you expect to get by doing:
Code: Pascal  [Select]

Writeln (row[0]);

In fact it is WriteLn (row[j][0]); as appears in the code I post just now.

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: Variables as indexes of arrays
« Reply #4 on: March 31, 2023, 12:25:45 am »
In fact it is WriteLn (row[j][0]); as appears in the code I post just now.

With the code that you just posted:

Code: Pascal  [Select][+][-]
  1. program triad_test;
  2.     type
  3.       Triads = Array [0..2] of Integer;
  4.     var
  5.       row: Array [0..3] of Triads;
  6.       j: Integer;
  7.     const  
  8.       triad: Triads = (1, 2, 3);
  9.   begin
  10.     row [1][0] := triad[0];
  11.     Writeln (row[1][0]);
  12.     j:=1;
  13.     Writeln (row[j][0]);
  14.     Readln;
  15.  end.

Code: Bash  [Select][+][-]
  1.  > /home/fred/fpc_test/triad_test

gives this on with fpc 3.2.2 Linux 64 bit (not tested on Windows 32):

Code: Bash  [Select][+][-]
  1. 1
  2. 1

[EDIT] Tested with fpc.exe 3.2.2 32 bit on Windows 11 and same result.
« Last Edit: March 31, 2023, 01:19:23 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Variables as indexes of arrays
« Reply #5 on: March 31, 2023, 12:30:41 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.   Triads = array [0..2] of integer;
  5. var
  6.   row: array [0..3] of Triads;
  7.   i: integer;
  8. const
  9.   triad: Triads = (1, 2, 3);
  10. begin
  11.   // fill all array elements with values
  12.   row[0][0] := triad[0];
  13.   row[0][1] := triad[1];
  14.   row[0][2] := triad[2];
  15.   row[1][0] := triad[0];
  16.   row[1][1] := triad[1];
  17.   row[1][2] := triad[2];
  18.   row[2][0] := triad[0];
  19.   row[2][1] := triad[1];
  20.   row[2][2] := triad[2];
  21.   row[3][0] := triad[0];
  22.   row[3][1] := triad[1];
  23.   row[3][2] := triad[2];
  24.   // loop thru all values inside array
  25.   for i := Low(row[0]) to High(row[0]) do
  26.     WriteLn(row[0][i]);
  27.   for i := Low(row[1]) to High(row[1]) do
  28.     WriteLn(row[1][i]);
  29.   for i := Low(row[2]) to High(row[2]) do
  30.     WriteLn(row[2][i]);
  31.   for i := Low(row[3]) to High(row[3]) do
  32.     WriteLn(row[3][i]);
  33.   Readln;
  34. end.
Does that help/answer your question?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Variables as indexes of arrays
« Reply #6 on: March 31, 2023, 12:34:30 am »
Or more compact:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.   Triads = array [0..2] of integer;
  5. var
  6.   row: array [0..3] of Triads;
  7.   i, ii: integer;
  8. const
  9.   triad: Triads = (1, 2, 3);
  10. begin
  11.   for i := Low(Row) to High(Row) do
  12.     for ii := Low(Row[i]) to High(Row[i]) do
  13.       Row[i][ii] := triad[ii];
  14.   for i := Low(Row) to High(Row) do
  15.     for ii := Low(Row[i]) to High(Row[i]) do
  16.       WriteLn(Row[i][ii]);
  17.   Readln;
  18. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Variables as indexes of arrays
« Reply #7 on: March 31, 2023, 02:16:54 am »
Quote
Hello, I am using the Free Pascal IDE for Win32, version 1.0.12.

 did you notice the version number? Isn't that quite old and what version fpc was used for that back then ?
The only true wisdom is knowing you know nothing

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Variables as indexes of arrays
« Reply #8 on: March 31, 2023, 02:27:00 am »
Quote
Hello, I am using the Free Pascal IDE for Win32, version 1.0.12.

 did you notice the version number? Isn't that quite old and what version fpc was used for that back then ?

I also have this version, I think it's current one. My has FPC version 3.2.2.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Variables as indexes of arrays
« Reply #9 on: March 31, 2023, 02:43:05 am »
current release is 2.2.6 for  the IDE and 3.2.2 comes with it believe.

I started Lazarus I think when it was 1.64.x something way back.....
Maybe it's a typo but also maybe it's accurate, too?
The only true wisdom is knowing you know nothing

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: Variables as indexes of arrays
« Reply #10 on: March 31, 2023, 03:37:52 am »
current release is 2.2.6 for  the IDE and 3.2.2 comes with it believe.

I started Lazarus I think when it was 1.64.x something way back.....
Maybe it's a typo but also maybe it's accurate, too?

They're referring to the version number for fp.exe - the character mode IDE in FPC.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

JMarques

  • New Member
  • *
  • Posts: 13
Re: Variables as indexes of arrays
« Reply #11 on: March 31, 2023, 11:00:19 am »
In fact it is WriteLn (row[j][0]); as appears in the code I post just now.

gives this on with fpc 3.2.2 Linux 64 bit (not tested on Windows 32):

Code: Bash  [Select][+][-]
  1. 1
  2. 1

[EDIT] Tested with fpc.exe 3.2.2 32 bit on Windows 11 and same result.

Thanks for testing. That is the expected result, and I dont understand why it doesn't show in my system. I have been compiling much more complex programs in it without any issue. I wonder what can be happening.
« Last Edit: March 31, 2023, 11:22:26 am by JMarques »

JMarques

  • New Member
  • *
  • Posts: 13
Re: Variables as indexes of arrays
« Reply #12 on: March 31, 2023, 11:12:27 am »
Or more compact:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3.  

Thank you for your attention. I ran the program in the long version and got a correct result. With the compact version I got the following result, instead of the expected one. I see that only the compact version uses a variable for the first index of "row", and that is exactly where the problem lies.

Running "f:\zprograms\m2\project.exe "
1
1
1
3
0
0
0
0
0
0
3
0
« Last Edit: March 31, 2023, 11:20:03 am by JMarques »

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: Variables as indexes of arrays
« Reply #13 on: March 31, 2023, 12:02:51 pm »
I use the textmode IDE at version 1.0.12 with FPC 3.2.0, and also have 3.2.3 installed. Windowns 10. I tested your version and KodeZwerg's compact version and always get the expected output, regardless which compiler settings I tried, with 3.2.0 as well as 3.2.3. I would be really surprised if that was an FPC bug. Could you please specify your compiler settings i.e. target processor, optimisation etc.

JMarques

  • New Member
  • *
  • Posts: 13
Re: Variables as indexes of arrays
« Reply #14 on: March 31, 2023, 01:13:39 pm »
I use the textmode IDE at version 1.0.12 with FPC 3.2.0, and also have 3.2.3 installed. Windowns 10. I tested your version and KodeZwerg's compact version and always get the expected output, regardless which compiler settings I tried, with 3.2.0 as well as 3.2.3. I would be really surprised if that was an FPC bug. Could you please specify your compiler settings i.e. target processor, optimisation etc.

Thank you the attention to my problem. The target processor is Win32 for i386. The compiler mode is set to Free Pascal dialect. I will be happy to give more information as needed provided you inform where to look at. Thanks again!

 

TinyPortal © 2005-2018