DVODIMENZIONALNI NIZOVI - MATRICE
1. Učitati realnu matricu dimenzija n x m, n-br. redova(vrsta) i m br. kolona(stubaca). Sabrati članove matrice i
ispisati matricu.
PROGRAM matrica1;
uses wincrt;
var a:array[1..50,1..50]of real;
i,j,m,n:integer;
s:real;
begin
writeln('Unesite br.redova n:');
readln(n);
writeln('Unesite broj kolona m:');
readln(m);
writeln('Unosite elemente matrice po redovima:');
{Unos elemenata matrice}
for i:=1 to n do
begin
for j:=1 to m do
read(a[i,j]);
readln;
end;
s:=0;
for i:=1 to n do
for j:=1 to m do
s:=s+a[i,j];
{Ispis matrice}
for i:=1 to n do
begin
for j:=1 to m do
write(a[i,j]:6:2);
writeln;
end;
writeln('Zbir elemenata je:',s:0:2);
end.
2. Svaki član unete matrice(n x m) realnih elemenata uvećati 2 puta. Korististi proceduru za unos elemenata
matrice i proceduru za ispis matrice.
program uvecaj;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m:integer;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
1
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
procedure Ispisi(n,m:integer; var a:matrica);
var i,j:integer;
begin
writeln('Nova matrica je:');
for i:=1 to n do
begin
for j:=1 to m do write(a[i,j]*2:6:2);
writeln;
end;
end;
begin
write('n=');readln(n);
write('m=');readln(m);
Ucitaj(n,m,a);
Ispisi(n,m,a);
end.
3. Ispisati jediničnu matricu n x n
1000
0100
0010
0001
program uuuu;
uses wincrt;
type matrica=array[1..20,1..20]of integer;
var a:matrica;
i,j,n:integer;
procedure Ispisi(n:integer; a:matrica);
var i,j:integer;
begin
for i:=1 to n do
begin
for j:=1 to n do write(a[i,j]);
writeln
end;
end;
begin
write('n= '); readln(n);
for i:=1 to n do
for j:=1 to n do
2
if i=j then a[i,j]:=1
else a[i,j]:=0;
Ispisi(n,a);
end.
4. Ispisati sledeću matricu n x n
2000
1200
1120
1112
program uuuu;
uses wincrt;
type matrica=array[1..20,1..20]of integer;
var a:matrica;
i,j,n:integer;
procedure Ispisi(n:integer; a:matrica);
var i,j:integer;
begin
for i:=1 to n do
begin
for j:=1 to n do write(a[i,j]);
writeln
end;
end;
begin
write('n= '); readln(n);
for i:=1 to n do
for j:=1 to n do
if i=j then a[i,j]:=2
else if i>j then a[i,j]:=1
else a[i,j]:=0;
Ispisi(n,a);
end.
5. Pronaci najveći i najmanji element unete matrice dimenzija n x m. Korististi proceduru za unos elemenata
matrice.
program najnaj;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m:integer;
max,min:real;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
3
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
begin
write('n=');readln(n);
write('m=');readln(m);
Ucitaj(n,m,a);
max:=a[1,1];
min:=a[1,1];
for i:=1 to n do
for j:=1 to m do
begin
if max<a[i,j] then max:=a[i,j];
if min>a[i,j] then min:=a[i,j];
end;
writeln('max= ',max:0:2);
writeln('min= ',min:0:2);
end.
6. Pronaci najveći i najmanji element unete matrice dimenzija n x m i mesto na kome se nalaze. Korististi
proceduru za unos elemenata matrice.
program najnaj;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m,maxi,maxj,mini,minj:integer;
max,min:real;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
begin
write('n=');readln(n);
write('m=');readln(m);
Ucitaj(n,m,a);
max:=a[1,1]; maxi:=1; maxj:=1;
min:=a[1,1]; mini:=1; minj:=1;
for i:=1 to n do
4
for j:=1 to m do
begin
if max<a[i,j] then
begin
max:=a[i,j]; maxi:=i; maxj:=j;
end;
if min>a[i,j] then
begin
min:=a[i,j]; mini:=i; minj:=j;
end;
end;
writeln('max= ',max:0:2, 'To je clan a[',maxi,',',maxj,']');
writeln('min= ',min:0:2,'To je clan a[',mini,',',minj,']');
end.
7. Za unetu matrici n x n realnih elemenata pronaći:
• Zbir glavne dijagonale S1
• Zbir sporedne dijagonale S2
• Zbir iznad glavne dijagonale S3
• Zbir ispod glavne dijagonale S4
• Zbir negativnih elemenata iznad sporedne dijagonale S5
• maximum sporedne dijagonale
program dijagonale;
uses wincrt;
type matrica=array[1..10,1..10] of real;
var a:matrica;
i,j,n:integer;
s1,s2,s3,s4,s5,s6,max:real;
procedure ucitaj(n:integer; var a:matrica);
var i,j:integer;
begin
writeln('Unesite elemente matrice:');
for i:=1 to n do
begin
for j:=1 to n do
read(a[i,j]);
readln;
end;
end;
begin
writeln('Unesite broj redova i kolona matrice nxn:');
readln(n);
ucitaj(n,a);
{Zbir glavne dijagonale S1}
s1:=0;
for i:=1 to n do
s1:=s1+a[i,i];
5
writeln('Zbir glavne dijagonale je: ',s1:0:2);
{Zbir sporedne dijagonale S2}
s2:=0;
for i:=1 to n do
s2:= s2+a[i,n+1-i];
writeln('Zbir sporedne dijagonale je: ',s2:0:2);
{Zbir iznad glavne dijagonale S3}
s3:=0;
for i:=1 to n-1 do
for j:=i+1 to n do
s3:=s3+a[i,j];
writeln('Zbir iznad dijagonale je: ',s3:0:2);
{Zbir ispod glavne dijagonale S4}
s4:=0;
for i:=2 to n do
for j:=1 to i-1 do
s4:=s4+a[i,j];
writeln('Zbir ispod dijagonale je: ',s4:0:2);
{ Zbir negativnih elemenata iznad sporedne dijagonale}
s5:=0;
for i:=1 to n-1 do
for j:=1 to n-i do
if a[i,j]<0 then s5:=s5+a[i,j];
writeln('Zbir negativnih elemenata iznad sporedne dijagonale je',s5:0:2);
{maximum sporedne dijagonale}
max:=a[1,n];
for i:=2 to n do
if a[i,n+1-i]>max then max:=a[i,n+1-i];
writeln('Maximum sporedne dijagonale je ',max:0:2);
end.
8. Razmeniti vrednosti druge i cetvrte kolone matrice n x m (n>=2, m>=4)
program zamena;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m:integer;
pom:real;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
6
end;
procedure Razmeni(var x,y:real);
var pom:real;
begin
pom:=x; x:=y; y:=pom;
end;
procedure Ispisi(n,m:integer; var a:matrica);
var i,j:integer;
begin
writeln('Nova matrica je:');
for i:=1 to n do
begin
for j:=1 to m do write(a[i,j]:6:2);
writeln;
end;
end;
begin
write('n=');readln(n);
write('m=');readln(m);
Ucitaj(n,m,a);
for i:=1 to n do Razmeni(a[i,2],a[i,4]);
Ispisi(n,m,a);
end.
9. Razmena vrednosti K-tog i L-tog reda.
program razm;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m,k,l:integer;
pom:real;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
procedure Razmeni(var x,y:real);
var pom:real;
begin
pom:=x; x:=y; y:=pom;
end;
procedure Ispisi(n,m:integer; var a:matrica);
7
var i,j:integer;
begin
writeln('Nova matrica je:');
for i:=1 to n do
begin
for j:=1 to m do write(a[i,j]:6:2);
writeln;
end;
end;
begin
write('n=');readln(n);
write('m=');readln(m);
writeln('Unesite koje redove hocete da razmenite: ?');
readln(k,l);
Ucitaj(n,m,a);
for j:=1 to m do Razmeni(a[k,j],a[l,j]);
Ispisi(n,m,a);
end.
10. Napisati program kojim se vrste matrice uredjuju testerasto. (A1<A2>A3<A4..)
program testera;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m,z:integer;
pom:real;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
procedure Ispisi(n,m:integer; var a:matrica);
var i,j:integer;
begin
writeln('Nova matrica je:');
for i:=1 to n do
begin
for j:=1 to m do write(a[i,j]:6:2);
writeln;
end;
end;
begin
8
write('n=');readln(n);
write('m=');readln(m);
Ucitaj(n,m,a);
for i:=1 to n do
begin
z:=1;
for j:=1 to m-1 do
begin
if z*(a[i,j]-a[i,j+1])>0 then
begin
pom :=a[i,j];
a[i,j] :=a[i,j+1];
a[i,j+1]:=pom;
end;
z:=-z;
end;
end;
Ispisi(n,m,a);
end.
11. Napisati program kojim se vrste matrice uredjuju testerasto. (A1>A2<A3>A4..) Za unos, uredjenje i ispis
matrice koristiti odgovarajuce procedure.
program testera;
uses wincrt;
type niz=array[1..20]of real;
matrica=array[1..20]of niz;
var x:matrica;
n,i:integer;
procedure Ucitaj(n:integer; var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrice po vrstama:');
for i:=1 to n do
begin
for j:=1 to n do
read(a[i,j]);
readln;
end;
end;
procedure Ispisi(n:integer; var a:matrica);
var i,j:integer;
begin
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:6:2);
writeln;
9
end;
end;
procedure Razmeni(var k,p:real);
var pom:real;
begin
pom:=k; k:=p; p:=pom;
end;
procedure Uredi(n:integer; var x:niz);
var i:integer;
z:real;
begin
z:= -1;
for i:=1 to n-1 do
begin
if z*(x[i]-x[i+1])>0 then Razmeni(x[i],x[i+1]);
z:=-z;
end;
end;
begin
writeln('Unesite dimenziju matrice:');readln(n);
Ucitaj(n,x);
for i:=1 to n do
Uredi(n,x[i]);
Ispisi(n,x);
end.
12. Izracunati zbir svake vrste i kolone matrice n x m.
program zbirovi;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m:integer;
Sv,Sk:real;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
begin
write('n= '); readln(n);
write('m=');readln(m);
Ucitaj(n,m,a);
10
{Zbir po redovima}
for i:=1 to n do
begin
Sv:=0;
for j:=1 to m do Sv:=Sv +a[i,j];
writeln('Sv',i,'=',Sv:0:2);
end;
{Zbir po kolonama}
for j:=1 to m do
begin
Sk:=0;
for i:=1 to n do Sk:=Sk+a[i,j];
writeln('Sk',j,'=',Sk:0:2);
end;
end.
13. Od elemenata matrice n x m formirati:
a) niz Sv ciji su elementi zbirovi vrsta (redova) matrice;
b) niz Sk ciji su elementi zbirovi kolona matrice;
program zbirovi;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m:integer;
Sv,Sk:array[1..50]of real;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
begin
write('n= '); readln(n);
write('m=');readln(m);
Ucitaj(n,m,a);
{Zbir po redovima}
for i:=1 to n do
begin
Sv[i]:=0;
for j:=1 to m do Sv[i]:=Sv[i]+a[i,j];
end;
for i:=1 to n do write(Sv[i]:8:2);writeln;
11
{Zbir po kolonama}
for j:=1 to m do
begin
Sk[j]:=0;
for i:=1 to n do Sk[j]:=Sk[j]+a[i,j];
end;
for j:=1 to m do write(Sk[j]:8:2);
end.
13.1 Od elemenata matrice n x m formirati:
a) niz Sv ciji su elementi zbirovi vrsta (redova) matrice;
Koristiti proceduru Ucitaj i funkciju za računanje sume.
program zbirovi;
uses wincrt;
type niz=array[1..50]of real;
matrica=array[1..50]of niz;
var a:matrica;
i, n,m:integer;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
function Suma(n:integer;X:niz):real;
var i:integer;
S:real;
begin
S:=0;
for i:=1 to n do S:=S+X[i];
Suma:=S;
end;
begin
write('n= '); readln(n);
write('m=');readln(m);
Ucitaj(n,m,a);
writeln('Zbir po redovima je:');
for i:=1 to n do
write(Suma(m,a[i]):6:2); writeln;
end.
12
14.Razmenjuje vrednosti elemenata vrste koja sadrži minimalni element sa vrstom u kojoj se nalazi max
element.
program razm;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m,maxi,mini:integer;
max,min:real;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
procedure Razmeni(var x,y:real);
var pom:real;
begin
pom:=x; x:=y; y:=pom;
end;
procedure Ispisi(n,m:integer; var a:matrica);
var i,j:integer;
begin
writeln('Nova matrica je:');
for i:=1 to n do
begin
for j:=1 to m do write(a[i,j]:6:2);
writeln;
end;
end;
begin
write('n:= '); readln(n);
write('m:= '); readln(m);
Ucitaj(n,m,a);
max:=a[1,1]; maxi:=1;
min:=a[1,1]; mini:=1;
for i:=1 to n do
for j:=1 to m do
begin
if max<a[i,j] then
begin
max:=a[i,j];
maxi:=i;
end;
13
if min>a[i,j] then
begin
min:=a[i,j];
mini:=i;
end;
end;
for j:=1 to m do Razmeni(a[maxi,j],a[mini,j]);
Ispisi(n,m,a);
end.
15. Pronaći najveći član svake kolone i svakog reda
program trazi;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m:integer;
rmax,kmax:real;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
begin
write('n:= '); readln(n);
write('m:= '); readln(m);
Ucitaj(n,m,a);
for i:=1 to n do
begin
rmax:=a[i,1];
for j:=2 to m do
if rmax<a[i,j] then rmax:=a[i,j];
writeln('max ',i,'-tog reda je ',rmax:0:2);
end;
for j:=1 to m do
begin
kmax:=a[1,j];
for i:=2 to n do
if kmax<a[i,j] then kmax:=a[i,j];
writeln('max ',j,'-e kolone je ',kmax:0:2);
end;
end.
14
16. Sortirati vrste matrice u rastucem poretku
program sort;
uses wincrt;
type niz=array[1..20]of real;
matrica=array[1..20]of niz;
var a:matrica;
i,j,n,m:integer;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
procedure Razmeni(var x,y:real);
var pom:real;
begin
pom:=x; x:=y; y:=pom;
end;
procedure SortVrste(n:integer; var a:niz);
var i,j:integer;
begin
for i:=1 to n-1 do
for j:= i+1 to n do
if a[i]>a[j] then razmeni(a[i],a[j]);
end;
procedure Ispisi(n,m:integer; var a:matrica);
var i,j:integer;
begin
writeln('Nova matrica je:');
for i:=1 to n do
begin
for j:=1 to m do write(a[i,j]:6:2);
writeln;
end;
end;
begin
write('n=');readln(n);
write('m=');readln(m);
Ucitaj(n,m,a);
for i:=1 to n do
SortVrste(m,a[i]);
Ispisi(n,m,a);
end.
15
Transponovana matrica AT ili A’matrice A se dobija tako što se kolone matrice A smatraju za vrste
matrice AT
Ako je matrica A tipa mxn, onda je transponovana matrica AT tipa nxm.
17. Transponovati kvadratnu matricu A dimenzije n.Koristiti proceduru za unos elemenata matrice, proceduru
za transponovanje i proceduru za ispisivanje transponovane matrice.
program transpon;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a:matrica;
i,j,n,m:integer;
procedure Ucitaj(n:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to n do read(a[i,j]);
readln;
end;
end;
procedure Transponuj(n:integer; var a:matrica);
var i,j:integer;
pom:real;
begin
for i:=2 to n do
for j:=1 to i-1 do
begin
pom :=a[i,j];
a[i,j]:=a[j,i];
a[j,i]:=pom;
end;
end;
procedure Ispisi(n:integer; var a:matrica);
var i,j:integer;
begin
writeln('Transponovana matrica je:');
for i:=1 to n do
begin
for j:=1 to n do write(a[i,j]:6:2);
writeln;
end;
end;
begin
writeln('Unesite velicinu matrice n (n x n):');
readln(n);
Ucitaj(n,a);
16
Transponuj(n,a);
Ispisi(n,a);
end.
18. Transponovana matrica sa pomocnom matricom.
program transpon;
uses wincrt;
type matrica=array[1..50,1..50]of real;
var a,b:matrica;
i,j,n,m:integer;
procedure Ucitaj(n,m:integer;var a:matrica);
var i,j:integer;
begin
writeln('Unosite elemente matrica:');
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
end;
procedure Transponuj(n,m:integer);
var i,j:integer;
pom:real;
begin
for i:=1 to n do
for j:=1 to m do
b[j,i]:=a[i,j];
end;
procedure Ispisi(n,m:integer; var a:matrica);
var i,j:integer;
begin
writeln('Transponovana matrica je:');
for i:=1 to n do
begin
for j:=1 to m do write(a[i,j]:6:2);
writeln;
end;
end;
begin
writeln('Unesite velicinu matrice n ');
readln(n);
writeln('Unesite velicinu matrice m ');
readln(m);
Ucitaj(n,m,a);
Transponuj(n,m);
Ispisi(m,n,b);
end.
17
Download

DVODIMENZIONALNI NIZOVI - MATRICE