consulta sobre un ejercicio con randomize
uses crt;
const
fi = 6;
co = 6;
type
// matriz y vector dinamica
matriz = array of array of real;
vector = array of real;
var
ma : matriz;
ve : vector;
fila, columna ,
negativos,
po_central1, po_central2: integer;
i,j : integer;
(*******************************)
(* genera un numero aleatirio *)
(* entre 1 y x *)
(*******************************)
function lot(x:integer):integer;
begin
lot := (random(x) + 1);
end;
procedure filcol(n1,n2: integer ;
var z1,z2,z3,po1,po2 : integer);
var
i, j , con : byte;
g : Integer;
begin
randomize;
// genera el valor de fila >=3
repeat
z1 := lot(n1);
until z1 > 2;
// genera el valor de la columna
z2 := lot(n2);
setlength(ma, z1+1, z2+1);
setlength(ve, z1+1*z2+1+4);
con := 0;
for i := 1 to z1 do
begin
for j := 1 to z2 do
begin
g := lot(6);
if (g < 3) then
begin
ma[i,j]:= (-1 * lot(1000));
inc(con);
ve[con] := ma[i,j];
end
else
begin
ma[i,j]:= lot(1000);
end;
end;
end;
z3 := con;
if (z1 mod 2 = 0) then
begin
po1 := z1 div 2;
po2 := po1 + 1;
end
else
begin
po1 := (z1 div 2) + 1;
end;
end;
(********************)
(* muestra la matriz *)
(********************)
procedure mos_mat(xxx:matriz; a,b :integer );
var
i, j : byte;
begin
for i := 1 to a do
begin
for j := 1 to b do
begin
write('|',xxx[i,j]:7:2,'|');
end;
writeln;
end;
end;
procedure mos_vec(x:vector; n:integer);
var
i : integer;
begin
for i := 1 to n do
write('|',x[i]:7:2,'|');
end;
procedure procesobb(xxx:matriz;
a,b, pc1,pc2:integer );
var
i, j : byte;
tem, prom : real;
begin
for j := 1 to b do
for i := 1 to a do
if (pc2 = 0) then
begin
if (i <> pc1) then
begin
if (xxx[i,j] > 0) and (xxx[i,j] > xxx[pc1,j]) then
begin
tem := sqrt(xxx[i,j]);
xxx[i,j] := tem;
end
end
end
else
begin
prom := xxx[pc1,j] + xxx[pc2,j]/ 2;
if (i <> pc1) and (i <> pc2) then
begin
if (xxx[i,j] > 0) and (xxx[i,j] > prom) then
begin
tem := sqrt(xxx[i,j]);
ma[i,j] := tem;
end;
end;
end;
end; // fin funcion
(*****************************************)
(* muestra la matriz modificada *)
(* resaltando la posicin central en rojo *)
(*****************************************)
procedure mostrarcolor(xxx:matriz;
a,b, pc1,pc2:integer );
var
i, j : byte;
begin
for i := 1 to a do
begin
for j := 1 to b do
begin
if (pc2 = 0) then
begin
if i = pc1 then
begin
textcolor(red);
write('|',xxx[i,j]:7:2,'|');
end
else
begin
textcolor(white);
write('|',xxx[i,j]:7:2,'|');
end;
end
else
begin
if ((i = pc1) or (i = pc2)) then
begin
textcolor(red);
write('|',xxx[i,j]:7:2,'|');
end
else
begin
textcolor(white);
write('|',xxx[i,j]:7:2,'|');
end;
end ;
end; // fin j
writeln;
end; // fin i
end; // fin de funcion
begin
clrscr;
filcol(fi,co,fila,columna,negativos,
po_central1, po_central2);
writeln('a.- Generar aleatoriamente las dimensiones M y N de una');
writeln('matriz y también en forma aleatoria sus elementos MxN ');
writeln('matriz de [', fila,' filas y ',columna ,' columnas]');
writeln;
mos_mat(ma,fila,columna);
procesobb(ma,
fila,columna, po_central1,po_central2);
writeln;
writeln('b. matriz modificada segun condiciones');
writeln;
writeln;
mostrarcolor(ma,
fila,columna, po_central1,po_central2);
writeln;
writeln;
writeln('c.- La matriz tiene ', negativos ,' numeros negativos');
mos_vec(ve,negativos);
readln;
end.