Computação Eletrônica

13
Computação Eletrônica Vetores e Matrizes

description

Computação Eletrônica. Vetores e Matrizes. Vetor. Conjunto de variáveis de um mesmo tipo que são acessadas através de índices Tipo estruturado homogêneo Declaração var identificador :array[ v i .. v f ] of tipo; onde v i

Transcript of Computação Eletrônica

Page 1: Computação Eletrônica

Computação Eletrônica

Vetores e Matrizes

Page 2: Computação Eletrônica

Vetor

Conjunto de variáveis de um mesmo tipo que são acessadas através de índices

Tipo estruturado homogêneo Declaração

var identificador:array[vi .. vf] of tipo;

onde vi <= vf

Page 3: Computação Eletrônica

Vetoresprogram somaVetor;var i, soma: integer; numeros: array[1..5] of integer;

begin numeros[1] := 3; numeros[2] := 7; numeros[3] := 2; numeros[4] := 4; numeros[5] := 5; i := 1; soma := 0; while (i <= 5) do begin soma := soma + numeros[i]; i := i + 1; end; writeln('soma = ', soma);end.

Page 4: Computação Eletrônica

Vetoresprogram vetor;

var vetorDeNumeros: array[1..5] of integer; i: integer;

begin i := 1; while i <= 5 do begin write('Entre um numero: '); readln(vetorDeNumeros[i]); i := i + 1; end; i := 5; while i >= 1 do begin writeln('Numero: ',vetorDeNumeros[i]); i := i - 1; end; readln;end. 

Page 5: Computação Eletrônica

Vetoresprogram data;

var meses: array[1..12] of String; dia, mes, ano: integer;

begin meses[1] := 'Janeiro'; meses[2] := 'Fevereiro'; meses[3] := 'Marco'; meses[4] := 'Abril'; meses[5] := 'Maio'; meses[6] := 'Junho'; meses[7] := 'Julho'; meses[8] := 'Agosto'; meses[9] := 'Setembro'; meses[10] := 'Outubro'; meses[11] := 'Novembro'; meses[12] := 'Dezembro'; write('Entre a data: '); readln(dia,mes,ano); write(dia,' de ',meses[mes], ' de ', ano); readln;end.

Page 6: Computação Eletrônica

VetoresProgram contaAcimaDaMedia;var notas: array[1..5] of real; acimaDaMedia: integer; i: integer; soma, media: real;begin i := 1; acimaDaMedia := 0; while (i <= 5) do begin write('Entre a nota: '); readln(notas[i]); soma := soma + notas[i]; i := i + 1; end; i := 1; media := soma / 5; while (i <= 5) do begin if (notas[i] > media) then begin acimaDaMedia := acimaDaMedia + 1; end; i := i + 1; end; writeln('Media :', media:5:2); write('Acima da media: ',acimaDaMedia); readln;end.

Page 7: Computação Eletrônica

For x While

program forWhile1;

var uns: array[1..5] of integer; i: integer;begin for i := 1 to 5 do begin uns[i] := 1; end;end.

program forWhile2;

var uns: array[1..5] of integer; i: integer;begin

i := 1; while (i <= 5) do begin uns[i] := 1;

i := i + 1; end;end.

Page 8: Computação Eletrônica

While e for: correção de prova{Programa que:1) lê o gabarito de 10 questões

(respostas em número real)2) lê o número de matrícula e as

respostas dos alunos3) imprime a nota

Quando o número de matrícula for 9999 o programa termina.

}

Program correcao;var gabarito, resposta: array[1..10]

of real; nota, matricula, i: integer;begin for i := 1 to 10 do begin write('Entre com o

gabarito da questao ', i,': '); readln(gabarito[i]); end; write('Entre a matricula: '); readln(matricula);

while (matricula <> 9999) do begin nota := 0; for i := 1 to 10 do begin write('Entre com a

resposta da questao ', i,

': '); readln(resposta[i]); if (resposta[i] =

gabarito[i]) then begin nota :=

nota+1; end; end; writeln('A nota do aluno

numero ', matricula,' foi:

',nota); write('Entre a matricula: '); readln(matricula); end; write('Fim'); readln;end.

Page 9: Computação Eletrônica

Matriz Vetor multidimensional Declaração

var

identificador:array[vi .. vf] of array[vi .. vf] of tipo;

ou, de forma mais simples,

identificador:array[vi .. Vf ,vi .. Vf] of tipo;

Page 10: Computação Eletrônica

MatrizProgram lerMatriz;var matriz: array[1..5, 1..3] of integer; i,j: integer;

begin i := 1; while (i <= 5) do begin j := 1; while (j <= 3) do begin write('Entre o valor na posicao ',i,',',j,': '); readln(matriz[i,j]); j := j + 1; end; i := i + 1; end;end.

Page 11: Computação Eletrônica

MatrizProgram lerEscreveMatriz;

var matriz: array[1..5, 1..3] of integer;

i,j: integer;

begin

i := 1;

while (i <= 5) do

begin

j := 1;

while (j <= 3) do

begin

write('Entre o valor ',i,',',j,': ');

readln(matriz[i,j]);

j := j + 1;

end;

i := i + 1;

end;

i := 1;

while (i <= 5) do

begin

j := 1;

while (j <= 3) do

begin

write(matriz[i,j],' ');

j := j + 1;

end;

writeln(' ');

i := i + 1;

end;

readln;

end.

Page 12: Computação Eletrônica

MatrizLer 3 vetores de inteiros a, b e c com 3 elementos cada.Produzir a matriz m (4x3) tal que:- As 3 primeiras linhas correspondem aos vetores a, b e c.- A 4 linha seja a soma de a+b+c- Imprimir a matriz;

Page 13: Computação Eletrônica

MatrizProgram somavetor;var a,b,c: array[1..3] of integer; m: array[1..4,1..3] of integer; i,j: integer;begin i := 1; while (i <= 3) do begin write('Entre a na posicao ',i,': '); readln(a[i]); i := i + 1; end; i := 1; while (i <= 3) do begin write('Entre b na posicao ',i,': '); readln(b[i]); i := i + 1; end;

i := 1; while (i <= 3) do begin write('Entre c na posicao ',i,': '); readln(c[i]); i := i + 1; end; i := 1; while (i <= 3) do begin m[1,i] := a[i]; m[2,i] := b[i]; m[3,i] := c[i]; m[4,i] := a[i]+b[i]+c[i]; i := i + 1; end; i := 1; while (i <= 4) do begin j := 1; while (j <= 3) do begin write(m[i,j]:5,' '); j := j + 1; end; i := i + 1; writeln; end; readln;end.