开发者

Pascal insert user input to an array

I am trying to get better at functional programming. As a start I am planning on trying out with couple of languages like Pascal, Scheme, ML etc. First I started with Pascal. I am trying to insert user input into a integer array in pascal and then get them reverse.

  1 program ReverseList;
  2 
  3 var
  4         i: Integer;
  5         k: Integer;
  6         a: array[1..100] of Integer;
  7 begin   
  8         i := 0;
  9         repeat  
 10                 writeln('Enter a number');
 11                 readln(k);
 12                 if k > -1 then 
 13                         i := i + 1;
 14                         a[i] := k;
 15         until(k < 0);
 16         for i := 1 to i do    
 17                 writeln(a[i]);
 18 end.

In past I have mostly been a java developer so I was so custom to using all the lists thats available. Also ideally I was wondering if I can build a list where I can iterate over the list based on the number of elements in that list.

It would be great if anyone could point me on the direction of g开发者_如何学编程ood tutorials in functional programming as well as syntax on above mentioned programming languages.


There are several problems with your program:

  • The array is not initialized.
  • There is no input checking, both i=0 and i>100 result in an illegal array index.
  • The array index and the value are the same, is that correct?
  • You only write the first 10 numbers (but you use a different index, which is certain to be out of range).
  • The output is not in reverse.

There are also several pascal tutorials.

By the way, Pascal isn't a functional language. So if you really want to learn a functional language, you better try another one (like Lisp, Ml or probably F#).


It was a good practice and I managed to figure a solution for this. I am sure there are better ways to do, and also this doesn't look like I am using the functionalities of functional programming. But if anyone wants to provide a better solution please do so,

{author: Null-Hypothesis}

program ReverseList;

var
        i: Integer; {integer to keep the array length}
        k: Integer; {user input value}
        a: array[1..100] of Integer; {array to store the user inputs}
begin
        i := 0;
        repeat  {iterate until user input is negative or number of inputs exceed array size}

                writeln('Enter a number or enter negative value to exit the program.');
                readln(k);
                if(k > -1) and (i < 100) then   {check for negative value and size of the array}
                begin
                        i := i + 1;     {increase array index}
                        a[i] := k       {assign value to array}
                end
                else
                        break;  {exit if array size exceed the limit of array}
        until(k < 0);
writeln;

{Printing the user input before the reversing the list}
writeln('Original order of the list');
for i := 1 to i do
        writeln(a[i]);
writeln;

{Printing the reverse list}
writeln('Reversed List');
for i := i downto 1 do  {decrement array index}
        writeln(a[i]);
writeln('Bye!!!');
end.

Happy Coding, off to the next language...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜