SRL Archive - OSR Help

Big trouble with simple function


bg5

2011-12-31 19:19:59

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…

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.

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?

Shuttleu

2011-12-31 19:24:38

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

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

bg5

2011-12-31 19:30:19

Thank's. It works fine now:)