Pascal/Turbo Pascal - consulta sobre un ejercicio con randomize

 
Vista:

consulta sobre un ejercicio con randomize

Publicado por abril (1 intervención) el 22/12/2021 13:35:41
a- Generar en forma aleatoria las dimensiones M y N de una matriz, y también en forma aleatoria sus MxN componentes reales en un rango entre -1000 y 1000.

b- En cada columna, reemplazar por su raíz cuadrada a los elementos positivos que sean mayores al elemento central de la columna. De no haber un elemento central, considerar el promedio entre los dos elementos centrales. No reemplazar el o los elementos considerados como centrales.

c- Generar y mostrar en pantalla un arreglo conteniendo los elementos negativos de la matriz.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 36
Ha aumentado su posición en 4 puestos en Pascal/Turbo Pascal (en relación al último mes)
Gráfica de Pascal/Turbo Pascal

consulta sobre un ejercicio con randomize

Publicado por Armando José (43 intervenciones) el 26/12/2021 22:32:33
(*
Armando Fuenmayor
[email protected]
[email protected]
WhatsApp +58 412-2689131

el programa consiste
en:
a- Generar en forma
aleatoria las dimensiones
M y N de una matriz,
y también en forma
aleatoria sus MxN
componentes reales en un
rango entre -1000 y 1000.

b- En cada columna,
reemplazar por su
raíz cuadrada a los
elementos positivos
que sean mayores al
elemento central de
la columna.
De no haber un elemento
central,
considerar el promedio
entre los dos elementos
centrales.
No reemplazar el o los
elementos
considerados como centrales.

c- Generar y mostrar en pantalla un arreglo conteniendo los elementos negativos de la matriz.
*)

uses crt;
const
fi = 12;
co = 12;
var
ma: array[1..fi,1..co] of real;
arreglo: array[1..fi*co] of real;
tem, prom : real;
con, x, i, j: byte;
fila , columna : byte;
verdad : boolean;

po_cen1, po_cen2: byte;


begin
clrscr;
randomize;
repeat
fila := (random(fi) + 1);

until (fila > 2);
columna := (random(co) + 1);
con := 0;


writeln('a. Generar aleatoriamente las dimensiones M y N de una ma');
writeln('y también en forma aleatoria sus elementos MxN ');
writeln;
writeln('ma de [', fila,' filas y ',columna ,' columnas]');
writeln;
for i := 1 to fila do
begin
for j := 1 to columna do
begin

x := Random(5);

if (x <= 2) then
begin
inc(con);

ma[i,j] := (-1 * (random(1000) +1)) ;
arreglo[con] := ma[i,j];
write('|',ma[i,j]:7:2,'|');
end
else
begin
ma[i,j] := (random(1000) + 1) ;
write('|',ma[i,j]:7:2,'|');
end;


end;
writeln;
end;
// proceso B
verdad := false;
po_cen1 := 0;
po_cen2 := 0;

if (fila mod 2 = 0) then
begin
po_cen1 := fila div 2;
po_cen2 := po_cen1 + 1;

end
else
begin
po_cen1 := (fila div 2) + 1;
verdad := true;
end;

for j := 1 to columna do
for i := 1 to fila do
if (verdad = true) then
begin
if (i <> po_cen1) then
begin
if (ma[i,j] > 0) and (ma[i,j] > ma[po_cen1,j]) then
begin

tem := sqrt(ma[i,j]);
ma[i,j] := tem;
end
end
end
else
begin
prom := ma[po_cen1,j]
+ ma[po_cen2,j]/ 2;

if (i <> po_cen1) and (i <> po_cen2) then
begin
if (ma[i,j] > 0) and (ma[i,j] > prom) then
begin
tem := sqrt(ma[i,j]);
ma[i,j] := tem;
end;
end;
end;

writeln;
writeln('b. matriz modificada ');
writeln;

for i := 1 to fila do
begin
for j := 1 to columna do
begin
write('|',ma[i,j]:7:2,'|');
end;

writeln;
end;

//


writeln;
writeln('c. arreglo con numeros negativos de la ma ');
writeln;
for i := 1 to con do
write(int(arreglo[i]):7);




end.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 36
Ha aumentado su posición en 4 puestos en Pascal/Turbo Pascal (en relación al último mes)
Gráfica de Pascal/Turbo Pascal

consulta sobre un ejercicio con randomize

Publicado por Armando José (43 intervenciones) el 30/12/2021 11:50:27
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.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar