0 Replies - 1230 Views - Last Post: 07 December 2012 - 03:51 AM Rate Topic: -----

#1 DimiBy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
write('Введите n: ');readln(n);
write('Введите vmin: ');readln(vmin);
write('Введите vmax: ');readln(vmax);
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('Нету второго отрицательного или предпоследнего положительного');
readln;
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