Recent

Author Topic: Array not reading elements  (Read 373 times)

Sukuna

  • New Member
  • *
  • Posts: 15
Array not reading elements
« on: April 22, 2025, 10:59:39 am »
Hello,
I am experimenting with this https://castle-engine.io/modern_pascal guide to modern Pascal. I am not happy with it by the way and am searching for other online material on Pascal, should be a tutorial.

When I write elements for my array they are not squared, instead the numbers from 1 to 10 are squared.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes
  10.   { you can add units after this };
  11.             var
  12.               x:integer;
  13.                 MyArray:array[1..10]of integer = (88,82,89,80,29,20,28,21,22,20);
  14. begin
  15.  
  16.  
  17.   for x:=1 to 10 do
  18.   begin
  19.     MyArray[x] := x*x;
  20.     writeln('Square of array elements is: ',MyArray[x]);
  21.   end;
  22.  
  23.   for x:= Low(MyArray) to High(MyArray) do
  24.   writeln('Square is: ',MyArray[x]);
  25.   readln;
  26. end.
  27.  

Thaddy

  • Hero Member
  • *****
  • Posts: 16810
  • Ceterum censeo Trump esse delendam
Re: Array not reading elements
« Reply #1 on: April 22, 2025, 11:12:21 am »
Try:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var
  3.   x:integer;
  4.   MyArray:array[1..10]of integer = (88,82,89,80,29,20,28,21,22,20);
  5. begin
  6.  
  7.  
  8.   for x in myarray do
  9.   begin
  10.     MyArray[x] := x*x;
  11.       writeln('Square of array elements is: ',MyArray[x]);
  12.   end;
  13.  
  14.   for x in myarray do
  15.     writeln('Square is: ',MyArray[x]);
  16.   readln;
  17. end.
The reason is that x has double meaning in your code.
In my example x has single meaning, the value, not the value AND the index! It is also "Modern Pascal".

(The modern pascal guide you refer to has only one really big flaw: a complete misunderstanding of interfaces and the differences between COM and CORBA, ignore that chapter, it is a known shortcoming, there are better guides, many)
« Last Edit: April 22, 2025, 11:20:16 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11036
  • Debugger - SynEdit - and more
    • wiki
Re: Array not reading elements
« Reply #2 on: April 22, 2025, 11:16:28 am »
Quote
Code: Pascal  [Select][+][-]
  1.   MyArray[x] := x*x;

Squares "x", which in the original code is 1 to 10
Quote
Code: Pascal  [Select][+][-]
  1.   for x:=1 to 10 do

Should be
Code: Pascal  [Select][+][-]
  1.   MyArray[x] := MyArray[x]*MyArray[x];

Nimbus

  • New Member
  • *
  • Posts: 29
Re: Array not reading elements
« Reply #3 on: April 22, 2025, 12:13:24 pm »
Or
Code: Pascal  [Select][+][-]
  1. MyArray[x] := Sqr(MyArray[x]);

as the System unit has built-in function for squaring numbers
https://www.freepascal.org/docs-html/rtl/system/sqr.html
« Last Edit: April 22, 2025, 12:16:05 pm by Nimbus »

Sukuna

  • New Member
  • *
  • Posts: 15
Re: Array not reading elements
« Reply #4 on: April 22, 2025, 03:59:11 pm »
Try:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var
  3.   x:integer;
  4.   MyArray:array[1..10]of integer = (88,82,89,80,29,20,28,21,22,20);
  5. begin
  6.  
  7.  
  8.   for x in myarray do
  9.   begin
  10.     MyArray[x] := x*x;
  11.       writeln('Square of array elements is: ',MyArray[x]);
  12.   end;
  13.  
  14.   for x in myarray do
  15.     writeln('Square is: ',MyArray[x]);
  16.   readln;
  17. end.
The reason is that x has double meaning in your code.
In my example x has single meaning, the value, not the value AND the index! It is also "Modern Pascal".

(The modern pascal guide you refer to has only one really big flaw: a complete misunderstanding of interfaces and the differences between COM and CORBA, ignore that chapter, it is a known shortcoming, there are better guides, many)

Could you mention the other guides?

Sukuna

  • New Member
  • *
  • Posts: 15
Re: Array not reading elements
« Reply #5 on: April 22, 2025, 04:00:10 pm »
Quote
Code: Pascal  [Select][+][-]
  1.   MyArray[x] := x*x;

Squares "x", which in the original code is 1 to 10
Quote
Code: Pascal  [Select][+][-]
  1.   for x:=1 to 10 do

Should be
Code: Pascal  [Select][+][-]
  1.   MyArray[x] := MyArray[x]*MyArray[x];

Thank you, I understood this now.

Sukuna

  • New Member
  • *
  • Posts: 15
Re: Array not reading elements
« Reply #6 on: April 22, 2025, 04:17:13 pm »
I have another issue now. The first for - to loop gives just the highest number squared, not all 10 elements.
If I use this code only 9801 is printed on the console for the first loop, the second one prints all 10. How could I change this?

Code: Pascal  [Select][+][-]
  1. rogram Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes
  10.   { you can add units after this };
  11.  
  12. var
  13.   x:integer;
  14.  
  15.   MyArray : array[1..10] of integer = (88,89,80,82,20,29,28,22,90,99);
  16. begin
  17.  
  18.   for x := 1 to 10 do
  19.   MyArray[x] := MyArray[x]*MyArray[x];
  20.   writeln('The number squared is: ',MyArray[x]);
  21.  
  22.   for x := Low(MyArray) to High(MyArray) do
  23.   writeln('The number squared is: ',MyArray[x]);
  24.  
  25.   readln;
  26. end.  

Khrys

  • Full Member
  • ***
  • Posts: 215
Re: Array not reading elements
« Reply #7 on: April 22, 2025, 04:22:28 pm »
You need to use  begin  and  end  to group multiple statements together:

Code: Pascal  [Select][+][-]
  1. for x := 1 to 10 do begin
  2.   MyArray[x] := MyArray[x]*MyArray[x];
  3.   writeln('The number squared is: ',MyArray[x]);
  4. end;

 

TinyPortal © 2005-2018