# Task on a one-dimensional array

Page 1 of 1

## 0 Replies - 2053 Views - Last Post: 07 December 2012 - 03:51 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303097&amp;s=3bc12fdfa6710d9c62a3fd2ed55287f5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 DimiBy

Reputation: 0
• Posts: 11
• Joined: 28-September 11

# Task on a one-dimensional array

Posted 07 December 2012 - 03:51 AM

Please help with the solution and the explanation of this problem. In integer vector vector (n) in order to find the second negative and positive elements of the penultimate and swap them. The situation when such elements in the vector is not present.

EXAMPLE:
Input:
n = 10
v_min = -5
v_max = 10
Original vector:
7 3 1 -5 -2 5 2 0 10 9

Imprint
second_otr = -2 n_second_otr = 5
predp_pol = 10 n_predp_pol = 9
Output vector:
7 3 1 -5 10 5 2 0 -2 9

Itx programm:
```Var V:array[1..100] of integer;
n,vmin,vmax,i,j,p,k,t,buf:integer;
Begin
k:=0;t:=0;
writeln('Исходный');
For i:=1 to n do
Begin
V[i]:=random(31)-15;
write(V[i]:4);
if (V[i]<0)and(t=0)and(k=1) then t:=i;//второй отрицательный
if (V[i]<0) then k:=1;
End;
j:=0;p:=0;
For i:=n downto 1 do
Begin
if (V[i]>0)and(p=0)and(j=1) then p:=i;//предпоследний положительный
if (V[i]>0) then j:=1;
End;
writeln;
if (t>0)and(p>0) then
Begin
writeln('Второй отрицательный = ',V[t],' он находится V[',t,']');
writeln('Предпоследний положительный = ',V[p],' он находится V[',p,']');
buf:=V[t];
V[t]:=V[p];
V[p]:=buf;
writeln('Преобразованный');
For i:=1 to n do
write(V[i]:4);
End
else writeln('Нету второго отрицательного или предпоследнего положительного');
End.
```

But I can not understand how it works here, this part of the program and what all the variables k and t

```k:=0;t:=0;
writeln('Исходный');
For i:=1 to n do
Begin
V[i]:=random(31)-15;
write(V[i]:4);
if (V[i]<0)and(t=0)and(k=1) then t:=i;//второй отрицательный
if (V[i]<0) then k:=1;
End;
```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }