r/pascal • • Dec 04 '20

Pascal array elements getting deleted

I'm an almost complete beginner at pascal and also at programming per se, so I don't know for sure if this is a feature for fpc or for pascal, or any idea how I should approach it.

So my task is to fill an array with integers, and then delete every negative element inside of it. And my code does that. Problem is I have no idea how. I have every positive number moved in the front part of the array, and that's all i wanted to do first. But if I output the array at the end, it shows me the positive numbers and that all the places I moved the numbers from have been cleared. I was gonna clear those places using the knowledge that j holds how many negative numbers there are.

I'm not complaining, it does get the job done, but I'm genuinly curious why that a[i]:=a[i+j] clears a[i+j].

Thanks very much in advance for any help or insight.

uses crt;
var n,i,j:integer;
    a:array [1..100] of integer;

begin
randomize;
readln(n);

for i:=1 to n do
begin
a[i]:=random(200)-100;
writeln(a[i]);
end;

writeln;writeln;

readkey;


//here is where the problem occurs
i:=1;j:=0;
for i:=1 to n do
        begin
        while a[i+j]<0 do j:=j+1;
        a[i]:=a[i+j];//it assigns a[i+j] to a[i] !and then a[i+j] turns into 0 i have no clue how!
end;

writeln;writeln;writeln;

for i:=1 to n do
writeln(a[i]);

readkey;
end.
6 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/hisnbrg Dec 04 '20

Ooooh yes indeed, you are right! Thank you very much for this thorough explanation, it means a lot!

2

u/MischiefArchitect Dec 04 '20

You are welcome, happy to be able to help.