Devi semplicemente assegnare agli elementi del vettore, quelli della matrice, subito dopo la read, usando il valore m*n come indice vettore[m*n]:=matrice[m,n];
Non è così semplice, la gestione dell'indice è leggermente più sofisticata. Infatti se scrivesse:
codice:
Program ConvertiMatriceInVettore;
const M = 4;
const N = 4;
var mat:array[1..N,1..M] of integer;
vet:array[1..N*M] of integer;
i,j:integer;
begin
// Popola la matrice
for i := 1 to N do
for j := 1 to M do
mat[i,j] := i;
// Stampa la matrice
writeln('Matrice:');
for i:= 1 to N do
begin
for j:= 1 to M do
begin
write(mat[i,j],' ');
end;
writeln;
end;
// Matrice --> Vettore
for i := 1 to N do
for j := 1 to M do
begin
writeln('i= ',i,' j=',j,' i*j = ', i*j);
vet[i*j] := mat[i, j]
end;
writeln(#13#10, 'Vettore:');
// Stampa vettore
for i := 1 to N*M do
write(vet[i],' ');
end.
otterrebbe un risultato sbagliato
codice:
Matrice:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
i= 1 j=1 i*j = 1
i= 1 j=2 i*j = 2
i= 1 j=3 i*j = 3
i= 1 j=4 i*j = 4
i= 2 j=1 i*j = 2
i= 2 j=2 i*j = 4
i= 2 j=3 i*j = 6
i= 2 j=4 i*j = 8
i= 3 j=1 i*j = 3
i= 3 j=2 i*j = 6
i= 3 j=3 i*j = 9
i= 3 j=4 i*j = 12
i= 4 j=1 i*j = 4
i= 4 j=2 i*j = 8
i= 4 j=3 i*j = 12
i= 4 j=4 i*j = 16
Vettore:
1 2 3 4 0 3 0 4 3 0 0 4 0 0 0 4