vetores – parte 2 (arrays / arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1...
TRANSCRIPT
![Page 1: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/1.jpg)
1
Vetores – Parte 2(Arrays / Arranjos )
Prof. Jesús P. Mena-ChalcoCMCC/UFABC
Q2/2018
Processamento da Informação
![Page 2: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/2.jpg)
2
Terminologia : Palavra reservada new
Usada para criar um novo objeto. Em Java, uma vetor é um objeto.
![Page 3: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/3.jpg)
3
Exercício da aula anterior: Soma dos elementos
![Page 4: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/4.jpg)
4
Exercício da aula anterior: Maior elemento
![Page 5: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/5.jpg)
5
Exercício da aula anterior:Busca de um elemento
![Page 6: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/6.jpg)
![Page 7: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/7.jpg)
![Page 8: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/8.jpg)
8
Atividade
![Page 9: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/9.jpg)
9
Questão 1
O método m1, dado um vetor de n inteiros, devolve o elemento que está na posição
![Page 10: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/10.jpg)
10
Questão 2
O método m2, dado um vetor de n inteiros, devolve a somatória dos valores de todos seus elementos.
![Page 11: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/11.jpg)
11
Questão 3
O método m3, dados dois vetores de inteiros, devolve a somatória dos valores de todos seus elementos.
![Page 12: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/12.jpg)
12
Questão 4
O método m4, dado um vetor de inteiros, devolve a média aritmética simples.
![Page 13: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/13.jpg)
13
Questão 5
O método m5, dado um vetor de inteiros, devolve a quantidade total de elementos menores ou iguais a zero.
![Page 14: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/14.jpg)
14
Questão 6
O método m6, dado um vetor de inteiros, imprime o menor e o maior elemento do vetor. Min-Max
![Page 15: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/15.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
v.lenght = 8
i m1 m2
20 20
7 0 20
6 0 20
5 0 20
4 -60 20
3 -60 30
2 -60 30
1 -60 100
0 -60 100
-1
Imprime: -60100
![Page 16: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/16.jpg)
16
Questão 7
O método m7, dado um vetor de inteiros, devolve true se a somatória dos elementos da primeira metade é igual à somatória dos elementos da segunda metade. Caso contrário devolve false
![Page 17: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/17.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
v.lenght = 8
![Page 18: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/18.jpg)
10020 300 8010 30v =
0 1 2 3 4 5 6
v.lenght = 7
![Page 19: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/19.jpg)
19
Questão 8 - Desafio
Crie um método que permita inverter a ordem dos elementos de um array. A inversão deve ser realizada no mesmo array (não pode usar array auxiliar).
Assinatura: static int[] inverter (int a[])
Exemplos:
a = [80,2,100]Resposta: [100,2,80]
a = [1,2,3,4,555]Resposta: [555,4,3,2,1]
![Page 20: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/20.jpg)
20
Questão 8
![Page 21: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/21.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
v.lenght = 8n = 8
![Page 22: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/22.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
v.lenght = 8n = 8
![Page 23: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/23.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
v.lenght = 8n = 8
20temp =
![Page 24: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/24.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
v.lenght = 8n = 8
20temp =
![Page 25: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/25.jpg)
1000 300 10-60 00v =
0 1 2 3 4 5 6 7
v.lenght = 8n = 8
20temp =
![Page 26: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/26.jpg)
1000 300 10-60 00v =
0 1 2 3 4 5 6 7
v.lenght = 8n = 8
20temp =
![Page 27: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/27.jpg)
1000 300 10-60 200v =
0 1 2 3 4 5 6 7
v.lenght = 8n = 8
20temp =
![Page 28: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/28.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
![Page 29: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/29.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
1000 300 10-60 200
![Page 30: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/30.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
1000 300 10-60 200
00 300 10-60 20100
![Page 31: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/31.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
1000 300 10-60 200
00 300 10-60 20100
00 3010 0-60 20100
![Page 32: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/32.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
1000 300 10-60 200
00 300 10-60 20100
00 3010 0-60 20100
00 -6010 030 20100
![Page 33: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/33.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
1000 300 10-60 200
00 300 10-60 20100
00 3010 0-60 20100
00 -6010 030 20100
O elemento da posição i deverá ser trocado pelo elemento da posição 8-i-1
v.length = 8
![Page 34: Vetores – Parte 2 (Arrays / Arranjos )professor.ufabc.edu.br/~jesus.mena/courses/pi-2q... · 1 Vetores – Parte 2 (Arrays / Arranjos ) Prof. Jesús P. Mena-Chalco CMCC/UFABC Q2/2018](https://reader036.vdocuments.pub/reader036/viewer/2022063006/5fb67cc4316f7624281c4e6b/html5/thumbnails/34.jpg)
10020 300 10-60 00v =
0 1 2 3 4 5 6 7
v.lenght = 8n = 8
Quando i=0temp = 20v[0] = 0v[7] = 20