Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
User-defined array range? Posted by blackr1234 on 26 Apr 2011 at 9:33 AM
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.
VAR ar: ARRAY[1..100] OF STRING;

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


I think there would be a better way, maybe using PROCEDURE, but I fail:
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.


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:
PROCEDURE stringsss(n: integer; ar: ARRAY[1..n] OF STRING);
BEGIN END;

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


The last two are just my concepts, please make some adjustments to suite me needs. Thank you.
Report
Re: User-defined array range? Posted by blackr1234 on 26 Apr 2011 at 10:39 AM
.
Can I use dynamic arrays and setlength function to give it a new range?
.
Report
Re: User-defined array range? Posted by _Atex_ on 26 Apr 2011 at 8:28 PM
: .
: Can I use dynamic arrays and setlength function to give it a new
: range?
: .
:

Yep, that's the only way.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.