Results 1 to 3 of 3

Thread: Big trouble with simple function

  1. #1
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Unhappy Big trouble with simple function

    Ok ,what I'm going to do? I have an array of integer as input and I want function ,which add 3 to every element of array. What is important this function must do it with every array regardless of it's length. I thought it's easy...

    Simba Code:
    program new;
    {$DEFINE SRL5}
    {$DEFINE SMART}
    //{$I SRL/SRL.scar}
    {$I SRL/srl.simba}


    var
    TestArr : array[0..4] of integer;
    a :integer;
    ss : string;

    function Add3ToEveryIntArray(ArrayWithUnknownLenght : array of integer) : array of integer;
    var
    a,b:integer;
    begin
      b:=Length(ArrayWithUnknownLenght);
      writeln('Length is : '+inttostr(b));
      for a:=0 to b-1 do
      begin
        ArrayWithUnknownLenght[a]:= ArrayWithUnknownLenght[a] + 3;
        writeln(inttostr(a));
      end;
      Result:=ArrayWithUnknownLenght;
    end;

    begin

        TestArr[0]:=8;
        TestArr[1]:=6;
        TestArr[2]:=9;
        TestArr[3]:=18;
        TestArr[4]:=2;

        for a:=0 to 4 do
        begin
          ss := ss +' '+inttostr(TestArr[a]);
        end;
        writeln(ss);

        TestArr:=Add3ToEveryIntArray(TestArr); // here is line 42

        for a:=0 to 4 do
        begin
          ss := ss +' '+inttostr(TestArr[a]);
        end;
        writeln(ss);
    end.

    Code:
    Compiled successfully in 920 ms.
     8 6 9 18 2
    Length is : 5
    0
    1
    2
    3
    4
    Error: Type Mismatch at line 42
    I think problem is propably that i'm trying to put Array with undefined length into array[0..4]. But how to fix it?

  2. #2
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default

    change it to just a array of integer, and just set the array length at the start

    Simba Code:
    program new;
    {$DEFINE SRL5}
    {$DEFINE SMART}
    //{$I SRL/SRL.scar}
    {$I SRL/srl.simba}


    var
    TestArr : array of integer;
    a :integer;
    ss : string;

    function Add3ToEveryIntArray(ArrayWithUnknownLenght : array of integer) : array of integer;
    var
    a,b:integer;
    begin
      b:=Length(ArrayWithUnknownLenght);
      writeln('Length is : '+inttostr(b));
      for a:=0 to b-1 do
      begin
        ArrayWithUnknownLenght[a]:= ArrayWithUnknownLenght[a] + 3;
        writeln(inttostr(a));
      end;
      Result:=ArrayWithUnknownLenght;
    end;

    begin
        SetArrayLength(TestArr, 5);

        TestArr[0]:=8;
        TestArr[1]:=6;
        TestArr[2]:=9;
        TestArr[3]:=18;
        TestArr[4]:=2;

        for a:=0 to 4 do
        begin
          ss := ss +' '+inttostr(TestArr[a]);
        end;
        writeln(ss);

        TestArr:=Add3ToEveryIntArray(TestArr); // here is line 42

        for a:=0 to 4 do
        begin
          ss := ss +' '+inttostr(TestArr[a]);
        end;
        writeln(ss);
    end.

    ~shut

  3. #3
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    Thank's. It works fine now

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •