1

I'm using PASCAL for a course i'm doing and i'm having trouble with an assignment, in my program i'm using 2 arrays that uses a variable from a user's input but when i go to run the program it comes up with, Error: Can't evaluate constant expression. The code for the array is:

Var
        x : integer;
        s : array[1..x] of real;
        n : array[1..x] of string[30];

Here x is the user's input, is there a way for an array to go from 1 to x?

4
  • Aren't you missing a ;? Commented Jul 27, 2016 at 6:08
  • yeah that's just a typo on my behalf, i'm not missing any ; in my program Commented Jul 27, 2016 at 6:08
  • You need a dynamic array Commented Jul 27, 2016 at 6:33
  • Please state the exact compiler and version the next time. Commented Jul 28, 2016 at 10:17

3 Answers 3

6

If x is a variable, that won't work indeed. The range of a so called static array must be a constant expression, i.e. it must be known at compile time.

So what you want won't work, as is.

In FreePascal, you can use dynamic arrays, though. Their lengths can be set and changed at runtime.

var
  x: Integer;
  s: array of Real;
  n: array of string[30]; // why not just string?

and later:

  x := someUserInput(); // pseudo code!
  SetLength(s, x);
  SetLength(n, x);

You should be aware of the fact that dynamic arrays are 0-based, so your indexes run from 0 up to x - 1. But for the limits of the array, you should rather use Low() and High() instead:

  for q := Low(s) to High(s) do
    // access s[q];

(Low() and High() are not the topic, but just know they can also be used for static arrays, and that they return the actual array bounds -- I always use High and Low for this)

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! I have seemed to run into more problems which i assume has to do with the array, It says Error: Operator is not overloaded: "Dynamic Array of SmallInt" div "SmallInt", "Dyanmic Array of SmallInt" mod "ShortInt". I've put my entire program in the OP.
Vampiric activity detected
@David: yes, indeed.
@LeCarloVC: if you have more problems, you are free to post them as new questions.Shifting your goalposts is not well regarded in sports, and neither is it here.
@LeCarloVC: I see quite a few problems with your code. You can probably see them too, if you properly format the code. Do not mix different styles for keywords and work on your indentation. And a - b is subtraction.
|
-1

Here is not so simple as using managed types like dynamic arrays suggested by @RudyVelthuis but more funny solution providing some comprehension about how it works internally :)

program dynarr;

{$mode objfpc}{$H+}

type
    TArrayReal = array[1..High(Integer)] of Real;
    TArrayString30 = array[1..High(Integer)] of string[30];
    PArrayReal = ^TArrayReal;
    PArrayString30 = ^TArrayString30;
var
    i: Integer;
    x: Integer;
    s: PArrayReal;
    n: PArrayString30;
begin
    Write('Count: '); Readln(x);
    // Allocate memory for the actual count of the elements
    s := GetMem(SizeOf(Real) * x);
    n := GetMem(SizeOf(string[30]) * x);
    try
        for i := 1 to x do
        begin
            Write('Row ', i:3, ': '); Readln(s^[i], n^[i]);
        end;
        Writeln('Your input was:');
        for i := 1 to x do
            Writeln('Row ', i:3, ': ', s^[i]:10:3, n^[i]: 20);
    finally
        // Do not forget to release allocated memory
        FreeMem(s);
        FreeMem(n);
    end;
end.

6 Comments

Yeah, much more "fun" than a simple dynamic array.
This is simply abysmal advice
@DavidHeffernan What you do not understand in my introduction to the code?
Why stop there. Why not write the whole thing in asm. This is shockingly bad advice. Hence my vote. You state that it is a joke answer. What response would you expect?
@DavidHeffernan My answer is half-joke but also for educational purposes. Luck of basic knowledge (up to assembler) leads to "Vampiric activity" :)
|
-1

The limits of an array must be known at the point in time the array is instantiated. That means you cannot declare your array’s length x and the array itself within the same block. You need to declare and define x in a surrounding block:

program variableLengthArrayDemo(input, output);
    type
        natural = 1..maxInt;
    
    var
        count: natural;
    
    procedure processData;
        var
            data: array[1..count] of real;
        begin
        end;
    
    begin
        write('Enter total count of data points: ');
        readLn(count);
        processData
    end.

You may make count a routine parameter, it is still the outside block. Another more elaborate, yet more powerful method uses Extended Pascal schema data types.

Note, the shown program requires a processor complying with the requirements of ISO standard 10206 “Extended Pascal”. Unfortunately, as of 2024 the FreePascal Compiler intends to, but does not (yet) entirely support EP, in particular above program is not accepted even in {$mode extendedPascal}.

5 Comments

If the question is tagged FreePascal, why are you posting an answer that you already know is not supported by FreePascal. I know this won't work, because it's not supported by the compiler you've said you're using, but I'll post it anyway?
@Ken Because the FPC intends to support Extended Pascal and even has its dedicated compiler compatibility mode {$mode extendedPascal} that is meant to support Extended Pascal eventually. Also tag freepascal is not the only tag, but there is also tag pascal.
If there is a specific language tag, the question is about that language. It's common to add the regular Delphi tag to all Delphi questions, but when a tag for a specific version is added, answers are expected to be compatible with that specific version. The same applies here.
@Ken It seems reasonable that the tag delphi comprises delphi-xe8, delphi-11-alexandria, delphi-12-athens, and any other version of Delphi. FreePascal, however, is not “a version” of Pascal, but its own dialect with significant deviations, extensions and idiosyncrasies that cannot be transferred to all other Pascal implementations.
Yep, you're right. And none of those dialects support the code you've posted to a question specifically asking for code for one of those dialects. The question asks for FreePascal code, and you've posted code that does not work on FreePascal. I think that's what I said in my first comment, isn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.