My integer array is:
AR: array [0..5] of integer = (6, 5, 4, 8, 9, 7);
Algorithm:
Take the first value of the AR array (6)
Find the first great value that comes after it (9)
Find out how ahead you are after yourself (4)
write this as the first value in the result array
....
Get the second value in the AR index (5)
The first great value after him (9)
Find out how ahead you are after yourself (3)
write 3 after 4 in our results series.
.
.
This process will be repeated for each array entry. If there is no larger than the values that follow, '0'
will be written. The last value must be '0'
.
The expected output must be:
OutValue = 4-3-2-1-0-0
I don't have the codes that really work.
const
AR:array [0..5] of integer = (6, 5, 4, 8, 9, 7)
var
S:Tstringlist;
output:string;
i,j,g,n:integer;
begin
S:=Tstringlist.create;
for i:=0 to 5 do
for j:=i+1 to 5 do begin
if AR[i] > AR[j] then begin
g:=0;
AR[i]:=AR[j];
AR[i]:=g;
S.add( inttostr(g));
end
else if AR[i] < AR[j] then
begin
g:=sizeof(maxintvalue(AR[i]));
AR[i]:=AR[j];
AR[i]:=g;
S.add( inttostr(g));
end;
end;
For i:=0 to 5 do
begin
//S.Sorted:=true;
output:=S.Text;
end;
S.free;
writeln(output);
readln;
end.
Thank You.
I hope I understand the question correctly. The following approach (using a nested for
loop) is a possible solution to your problem.
program SpecialArraySort;
{$APPTYPE CONSOLE}
uses
SysUtils;
procedure SortArray;
const
aInput: array [0..5] of integer = (6, 5, 4, 8, 9, 7);
var
aOutput: array [0..5] of integer;
i, j, value, offset: Integer;
begin
// Generate output
for i := 0 to 5 do begin
value := aInput[i];
offset := 0;
for j := i + 1 to 5 do begin
if value < aInput[j] then begin
value := aInput[j];
offset := j - i;
end{if};
end;
aOutput[i] := offset;
end;
// Print output
for i := 0 to 5 do begin
Write(IntToStr(aOutput[i]) + ' ');
end;
end;
begin
SortArray;
end.