문제

There is any way of making x array constant after the data is read from user? There is any way of making variable not modifiable after it's value is read from user (eg. y)?

program hmm;
    uses crt;
    var 
        i, y: word;
        x: array of word;
begin
    readln(y);
    y:=y-1;
    SetLength(x,y); 
    for i := 0 to y do begin
        read(x[i]);
    end;
readkey;
end.

To make y constant I tried something like this, but it won't work - y will be set as 0.

program hmm;
    uses crt;
    var 
        i: word;
        x: array of word;
    const
    {$J+}
        y:word = 0;
    {$J-}
begin
    {$J+}
    readln(y);
    y:=y-1;
    {$J-}
    y:=0;
    SetLength(x,y); 
    for i := 0 to y do begin
        read(x[i]);
    end;
readkey;
end.

Thanks for help.

도움이 되었습니까?

해결책

Yes. Don't change either of them in your code after you set the initial value.

Other than that, there's no way. A dynamic array by definition is changeable, and so is a variable - that's why they have dynamic and variable as names.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top