Vilmis Neviera Vilmis Neviera - 4 days ago 5
Pascal Question

Pascal arranging array, with a row number

type
mas = array [1 .. 10000] of real;

var
n: integer;
a: mas;
Rf: text;

Procedure Skaityti;
var
i: integer;
Df: text;
begin
Assign(Df, 'duom1.txt');
Reset(Df);
Readln(Df, n);
for i := 1 to n do
Read(Df, a[i]);
Close(Df);
end;

Procedure MinMax(var min, max, nrmin, nrmax, vid: real);
var
i: integer;
begin
min := a[1];
max := a[1];
nrmin := 1;
nrmax := 1;
for i := 1 to n do
begin
if min > a[i] then
begin
min := a[i];
nrmin := i;
end;
if max < a[i] then
begin
max := a[i];
nrmax := i;
end;
vid := vid + a[i];
end;
vid := vid / n;
end;

var
min, max, nrmin, nrmax, vid: real;

begin
Skaityti;
MinMax(min, max, nrmin, nrmax, vid);
Assign(Rf, 'rez.txt');
Rewrite(Rf);
WriteLn(Rf, 'biggest amount ', max:7:2, ' number ', nrmax:7:0);
WriteLn(Rf, 'smallest amount ', min:7:2, ' number', nrmin:7:0);
WriteLn(Rf, 'average amount ', vid:7:2);
WriteLn(Rf);
WriteLn(Rf, 'number amount');
Close(Rf);
end.


This is the code and it Reads elements from a .txt file to an array, I created procedure to find biggest and smallest value of it. Now I need to create a procedure to write my array from biggest to smallest and a row number next to it. It should look something like this:

Row number value
5 1000000
6 999999
8 888888


and so on.. Any ideas ?

P.S. I can arrange that array to be from biggest to smallest but then the row numbers will mess up :/

Answer

Create a record type with two fields, one for the value from the file and the other for the "row" number.

Change the array to be an array of that record type.

When reading the file in the for loop, assign the read value to the value field of the record and the for loop control variable value as the "row" number.

When you sort the array, you sort the records, thus keeping value and row together.

Comments