Recent

Author Topic: Problem with integer array- likely basic  (Read 725 times)

rnfpc

  • Jr. Member
  • **
  • Posts: 91
Problem with integer array- likely basic
« on: June 22, 2019, 08:33:42 am »
I am trying following code to square a dynamic array of integers:

Code: Pascal  [Select][+][-]
  1. program mysquare_prog;
  2. {$mode objfpc}
  3.  
  4. type
  5.         tarr = array of integer;
  6. var
  7.         arr : tarr;
  8.  
  9. function mysquare(sarr: tarr): tarr;
  10. var
  11.         outarr: tarr;
  12.         i: integer;
  13. begin
  14.         outarr := tarr.create;
  15.         outarr.setlength(length(sarr));
  16.         for i in [0..length(sarr)-1] do
  17.                 outarr[i] = sarr[i] * sarr[i];
  18.         result := outarr;
  19. end;
  20.  
  21. begin
  22.         arr := tarr.create();
  23.         setlength(arr, 10);
  24.         arr.add(1);
  25.         arr.add(2);
  26.         arr.add(3);
  27.         writeln(mysquare(arr));
  28. end.
  29.  
However, I am getting following error:

Quote
$ fpc rnmaparr.pas
Free Pascal Compiler version 3.0.0+dfsg-11+deb9u1 [2017/06/10] for i386
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for i386
Compiling rnmaparr.pas
rnmaparr.pas(16,9) Fatal: Syntax error, "CREATE" expected but "SETLENGTH" found
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode

I have already used create in previous statement but it is still looking for create. I suspect the problem is very basic since I have just started using Pascal. Thanks for your help.
« Last Edit: June 22, 2019, 09:46:00 am by rnfpc »

Handoko

  • Hero Member
  • *****
  • Posts: 4081
  • My goal: build my own game engine using Lazarus
Re: Problem with integer array- likely basic
« Reply #1 on: June 22, 2019, 08:47:14 am »
Not very sure, but maybe this is what you want:

Code: Pascal  [Select][+][-]
  1. program mysquare_prog;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.         tarr = array of integer;
  7.  
  8. var
  9.         arr : tarr;
  10.  
  11. function mysquare(sarr: tarr): tarr;
  12. var
  13.         outarr: tarr;
  14.         i: integer;
  15. begin
  16.         setlength(outarr, length(sarr));
  17.         for i in [0..length(sarr)-1] do
  18.                 outarr[i] := sarr[i] * sarr[i];
  19.         result := outarr;
  20. end;
  21.  
  22. var
  23.         NewArr: tarr;
  24.         i: Integer;
  25. begin
  26.         setlength(arr, 3);
  27.         arr[0] := 1;
  28.         arr[1] := 2;
  29.         arr[2] := 3;
  30.         NewArr := mysquare(arr);
  31.         for i := 0 to Length(NewArr-1) do
  32.                 writeln(NewArr[i]);
  33. end.

~~~ correction ~~~
The line #31 above, it should be:
  for i := 0 to Length(NewArr)-1 do

Documentation about dynamic array:
https://wiki.freepascal.org/Dynamic_array

----------

You seems to have some programming knowledge but new in Pascal language. I recommend you to read the tutorials in the links in this post:

https://forum.lazarus.freepascal.org/index.php/topic,45078.msg317864.html#msg317864
« Last Edit: June 22, 2019, 12:41:16 pm by Handoko »

rnfpc

  • Jr. Member
  • **
  • Posts: 91
Re: Problem with integer array- likely basic
« Reply #2 on: June 22, 2019, 09:44:55 am »
Your code is giving error at this line:
Code: Pascal  [Select][+][-]
  1. for i := 0 to Length(NewArr-1) do ....

The error is:
Quote
Error: Operator is not overloaded: "tarr" - "ShortInt"

It works with:
Code: Pascal  [Select][+][-]
  1. for i := 0 to (Length(NewArr)-1) do ....
Thanks.
« Last Edit: June 22, 2019, 10:04:26 am by rnfpc »

totya

  • Hero Member
  • *****
  • Posts: 643
Re: Problem with integer array- likely basic
« Reply #3 on: June 22, 2019, 09:48:41 am »
I am trying following code to square a dynamic array of integers:

Quite terrible code :) See this workable sample:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. type
  7.   Tarr = array of integer;
  8.  
  9.   function mysquare(const sarr: Tarr): Tarr;
  10.   var
  11.     i: integer;
  12.   begin
  13.     Setlength(Result, length(sarr));
  14.  
  15.     for i := low(sarr) to high(sarr) do
  16.       Result[i] := sarr[i] * sarr[i];
  17.   end;
  18.  
  19. const
  20.   arr_count = 11;
  21. var
  22.   arr_in, arr_out: Tarr;
  23.   i: integer;
  24. begin
  25.   setlength(arr_in, arr_count);
  26.   setlength(arr_out, arr_count);
  27.  
  28.   for i := low(arr_in) to high(arr_in) do
  29.     arr_in[i] := i;
  30.  
  31.   arr_out := mysquare(arr_in);
  32.  
  33.   for i := low(arr_out) to high(arr_out) do
  34.     WriteLn(Format('%d. %d -> %d', [i, arr_in[i], arr_out[i]]));
  35.  
  36.   ReadLN;
  37. end.

totya

  • Hero Member
  • *****
  • Posts: 643
Re: Problem with integer array- likely basic
« Reply #4 on: June 22, 2019, 09:57:11 am »
Your code is giving error at this line:
Code: Pascal  [Select][+][-]
  1. for i := 0 to Length(NewArr-1) do ....

Code: Pascal  [Select][+][-]
  1. for i := 0 to Length(NewArr)-1 do


bytebites

  • Sr. Member
  • ****
  • Posts: 383
Re: Problem with integer array- likely basic
« Reply #5 on: June 22, 2019, 11:57:57 am »
Code: Pascal  [Select][+][-]
  1. for i in NewArr do writeln(i);

 

TinyPortal © 2005-2018