User-defined array range?

How can I manage to let the user input a number (n) and the program will read strings for n times?

I can do that in this way, but not good enough if user enters a number greater than 100.
[code]
VAR ar: ARRAY[1..100] OF STRING;

BEGIN
write('Amount: '); readln(n);
FOR i:= 1 TO n DO readln(ar[i]);
[/code]

I think there would be a better way, maybe using PROCEDURE, but I fail:
[code]
VAR n: integer;

PROCEDURE stringsss(j: integer);
VAR ar: ARRAY[1..j] OF STRING;
BEGIN END;

BEGIN {main program}
write('Amount '); readln(n);
stringsss(n)
END.
[/code]

Actually is there a way of doing that?
Also, if I want to pass strings to the procedure instead of reading from users, is it possible? I mean like:
[code]
PROCEDURE stringsss(n: integer; ar: ARRAY[1..n] OF STRING);
BEGIN END;

BEGIN {main program}
stringsss(3, 'john', 'tommy', 'mandy')
END.[/code]

The last two are just my concepts, please make some adjustments to suite me needs. Thank you.

Comments

  • .
    Can I use dynamic arrays and setlength function to give it a new range?
    .
  • : .
    : Can I use dynamic arrays and setlength function to give it a new
    : range?
    : .
    :

    Yep, that's the only way.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories