Documentos de Académico
Documentos de Profesional
Documentos de Cultura
2
Motivao
3
Motivao
Nota Media
Como faz-lo? No exemplo
8.0 7.75 anterior, uma nota
4.6 7.75 sobreposta por outra em
2.3 7.75 cada iterao do para.
3.7 7.75 A soluo armazenar
7.8 7.75 todas as 50 notas lidas...
9.0 7.75
Mas como?!?
.... ...
4
Variveis Compostas Homogneas
5
Variveis Compostas Homogneas
Unidimensionais (Vetores)
As variveis compostas homogneas
unidimensionais so utilizadas para
representar arranjos unidimensionais de
elementos de um mesmo tipo, em outras
palavras, so utilizadas para representar
vetores.
6
Vetores: Declarao
Exemplo:
// vetor com 5 elementos do tipo inteiro
inteiro dados[5] ; // em C seria ficaria int dados[5];
ndices do vetor
0 1 2 3 4
dados
5 elementos quaisquer do tipo inteiro
7
Vetores: Referncia (Manipulao)
Cada um dos elementos de um vetor referenciado individualmente por meio de
um nmero inteiro entre colchetes aps o nome do vetor.
0 1 2 3 4
Exemplos: dados 3 2 4 7 1
PseudoLinguagem Linguagem c
dados[0] 6; ou dados[0] = 6; ou
I 0; I = 0;
dados[i] 6; dados[i] = 6; 8
Vetores: Exemplos
O programa a seguir, usa o comando para para inicializar com zeros
os elementos de um array inteiro n de 10 elementos e o imprime sob
a forma de uma tabela.
principal
Elemento Valor
{
inteiro n[10], i; 0 0
1 0
0; i <= 9; i
para (i i+1) faa 2 0
{ 3 0
n[i] 0; 4 0
} 5 0
6 0
imprima("Elemento Valor");
7 0
0; i <= 9; i
para (i i+1) faa
8 0
{
imprima(i," ", n[i]); 9 0
}
}
9
Vetores: Exemplos
Exemplo anterior em C:
principal
{
constante inteiro TAMANHO 10;
Elemento Valor
inteiro s[TAMANHO], j;
0 2
1 4
para (j 0; j <= TAMANHO - 1; j j+1) faa
2 6
{
3 8
s[j] 2 + 2*j;
4 10
}
5 12
imprima("Elemento Valor");
6 14
para (j 0; j <= TAMANHO - 1; j j+1) faa
{
7 16
imprima (j, " ",s[j]);
8 18
9 20
}
}
11
Vetores: Exemplos
Exemplo anterior em C:
#include <stdio.h> Elemento Valor
0 2
#define TAMANHO 10 1 4
int main() 2 6
{ 3 8
int s[TAMANHO], j; 4 10
for (j=0; j<= TAMANHO - 1; j++) 5 12
{ 6 14
s[j] = 2 + 2*j;
7 16
8 18
}
9 20
printf("%s%13s\n","Elemento", "Valor");
for (j=0; j<= TAMANHO - 1; j++)
{
printf("%8d%13d\n",j,s[j]);
}
return 0;
}
12
Vetores e Subrotinas
13
Vetores e Subrotinas
No exemplo abaixo apresentado um procedimento imprimeVetor
que imprime um vetor de tamanho tam.
imprimeVetor (inteiro vet[], inteiro tam)
{
inteiro i;
para (i 0; i <= tam - 1; i i+1) faa
{
imprima (vet[i]);
}
}
principal
{
constante inteiro TAMANHO 10;
inteiro s[TAMANHO], j;
para (j 0; j <= TAMANHO - 1; j j+1)
{
imprima ("Informe o valor do vetor na posio ", j);
leia (s[j]);
}
imprimeVetor(s, TAMANHO);
}
14
Vetores e Subrotinas
Exemplo anterior em C:
#include <stdio.h>
#define TAMANHO 10
void imprimeVetor (int vet[], int tam)
{
int i;
for (i = 0; i <= tam - 1; i++)
{
printf("%d\n", vet[i]);
}
}
int main()
{
int s[TAMANHO], i;
for (i = 0; i <= TAMANHO- 1; i++)
{
printf ("Informe o valor do vetor na posio %d: ", i);
scanf (%d", &s[i]);
}
imprimeVetor(s, TAMANHO);
return 0;
}
15
Exerccio Resolvido 1
16
Exerccio 1 - Soluo Proposta
inteiro encontraMaior (real vet[], inteiro tam)
{
inteiro indice, i;
real maior vet[0];
indice 0;
para (i 1; i < tam; i i+1) faa
{
se( vet[i] > maior)
{
maior vet[i];
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
17
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{
inteiro indice, i;
real maior vet[0];
indice 0;
para (i 1; i < tam; i i+1) faa
{
se( vet[i] > maior)
{
maior vet[i];
indice i;
}
}
retorne indice;
}
principal
{ Inicialmente so
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4}; criadas as variveis
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
18
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{
inteiro indice, i;
real maior vet[0];
indice 0;
para (i 1; i < tam; i i+1) faa
{
se( vet[i] > maior)
{
maior vet[i];
indice i;
}
}
retorne indice;
}
principal
{ Chama a funo
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4}; encontraMaior
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
19
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Vetor e seu
inteiro indice, i; tamanho so
real maior vet[0];
passados como
indice 0;
parmetro
para (i 1; i < tam; i i+1) faa
{
se( vet[i] > maior)
{
maior vet[i];
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
20
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 3.0
indice 0; indice = 0
para (i 1; i < tam; i i+1) faa
{
se( vet[i] > maior) Varivies locais da
{ funo so inicializadas
maior vet[i];
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
21
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 3.0
indice 0; indice = 0
para (i 1; i < tam; i i+1) faa i=1
{
se( vet[i] > maior)
{
maior vet[i]; Comea a busca pelo
indice i; ndice 1, uma vez que o
} ndice zero j foi utilizada
} para inicializar a varivel
retorne indice; indice
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
22
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 3.0
indice 0; indice = 0
para (i 1; i < tam; i i+1) faa i=1
{
se( 4.3 > 3.0 )
{
maior vet[i]; Executa o primeiro teste
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
23
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 4.3
indice 0; indice = 1
para (i 1; i < tam; i i+1) faa i=1
{
se( 4.3 > 3.0 )
{ Sendo o teste verdadeiro,
maior vet[i]; atualiza variveis maior e
indice i; indice.
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
24
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 4.3
indice 0; indice = 1
para (i 1; i < tam; i i+1) faa i=2
{
se( vet[i] > maior )
{ Passa para a prxima
maior vet[i]; iterao do loop.
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
25
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 4.3
indice 0; indice = 1
para (i 1; i < tam; i i+1) faa i=2
{
se( 5.6 > 4.3 )
{ Mais um teste verdadeiro.
maior vet[i];
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
26
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 5.6
indice 0; indice = 2
para (i 1; i < tam; i i+1) faa i=2
{
se( 5.6 > 4.3 )
{ Sendo o teste verdadeiro,
maior vet[i]; atualiza variveis maior e
indice i; indice.
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
27
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 5.6
indice 0; indice = 2
para (i 1; i < tam; i i+1) faa i=3
{
se( vet[i] > maior )
{ Passa para a prxima
maior vet[i]; iterao do loop.
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
28
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 5.6
indice 0; indice = 2
para (i 1; i < tam; i i+1) faa i=3
{
se( 2.8 > 5.6 )
{ Valor do vetor menor,
maior vet[i]; no entra na condio.
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
29
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 5.6
indice 0; indice = 2
para (i 1; i < tam; i i+1) faa i=4
{
se( vet[i] > maior )
{ Passa para a prxima
maior vet[i]; iterao do loop.
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
30
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 5.6
indice 0; indice = 2
para (i 1; i < tam; i i+1) faa i=4
{
se( 7.9 > 5.6 )
{ Mais um teste verdadeiro.
maior vet[i];
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
31
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 7.9
indice 0; indice = 4
para (i 1; i < tam; i i+1) faa i=4
{
se( 7.9 > 5.6 )
{ Sendo o teste verdadeiro,
maior vet[i]; atualiza variveis maior e
indice i; indice.
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
32
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 7.9
indice 0; indice = 4
para (i 1; i < tam; i i+1) faa i=4
{
se( vet[i] > maior )
{ Passa para a penltima
maior vet[i]; iterao do loop.
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
33
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 7.9
indice 0; indice = 4
para (i 1; i < tam; i i+1) faa i=4
{
se( 3.4 > 7.9 )
{ Valor do vetor menor,
maior vet[i]; no entra na condio.
indice i;
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
34
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 7.9
indice 0; indice = 4
para (i 1; i < tam; i i+1) faa i=5
{
se( vet[i] > maior )
{ Passa para a ltima
maior vet[i]; iterao do loop (nesse
indice i; caso, no entra)
}
}
retorne indice;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
35
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 7.9
indice 0; indice = 4
para (i 1; i < tam; i i+1) faa i=5
{
se( vet[i] > maior )
{
maior vet[i];
indice i;
Retorna o valor do maior
}
} valor
retorne 4;
}
principal
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6)
imprima("Maior valor esta na posicao ", posicao);
}
36
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 7.9
indice 0; indice = 4
para (i 1; i < tam; i i+1) faa i=5
{
se( vet[i] > maior )
{
maior vet[i];
indice i;
}
}
retorne indice;
}
Volta para a funo principal,
principal pegando o retorno do ndice
{ do maior valor
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6) posicao = 4
imprima("Maior valor esta na posicao ", posicao);
}
37
Exerccio 1 - Teste de Mesa
inteiro encontraMaior (real vet[], inteiro tam)
{ Variveis
inteiro indice, i;
real maior vet[0];
maior = 7.9
indice 0; indice = 4
para (i 1; i < tam; i i+1) faa i=5
{
se( vet[i] > maior )
{
maior vet[i];
indice i;
}
}
retorne indice;
}
Imprime o ndice do
principal maior valor
{
real vetor[6] = {3.0, 4.3, 5.6, 2.8, 7.9, 3.4};
inteiro posicao;
posicao encontraMaior( vetor, 6) posicao = 4
imprima("Maior valor esta na posicao ", posicao);
Maior valor esta na posicao 4
}
38
Exerccio Resolvido 2
39
Exerccio 2 - Soluo Proposta
void ordena(float vet[], int tam)
{
int i, j;
float aux;
for(i = 0; i <= (tam-2); i++)
{
for(j = tam-1; j > i; j--)
{
if ( vet[j] < vet[j-1] )
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
40
Exerccio 2 Teste de Mesa
void ordena(float vet[], int tam) Vamos supor para esse
{ exerccio que o vetor de
int i, j; entrada tenha 5 posies e
float aux; os seguintes valores
for(i = 0; i <= (tam-2); i++)
{ 11.0, 22.0, 3.0, 44.0, 5.0
for(j = tam-1; j > i; j--)
{
if ( vet[j] < vet[j-1] )
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
41
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 44.0 5.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
for(j = tam-1; j > i; j--)
{
if ( vet[j] < vet[j-1] )
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux; Inicialmente funo
} chamada passando-se
} os valores propostos
}
}
42
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 44.0 5.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =
for(j = tam-1; j > i; j--)
{ j =
if ( vet[j] < vet[j-1] )
aux =
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
} A seguir so criadas as
} variveis auxiliares
}
}
43
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 44.0 5.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =
if ( vet[j] < vet[j-1] )
aux =
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} A idia central ir puxando
os menores valores atravs de
}
trocas para o incio do vetor
}
44
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 44.0 5.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =4
if ( vet[j] < vet[j-1] )
aux =
{
aux=vet[j];
vet[j]= vet[j-1];
no loop interno que iremos iterar
vet[j-1]=aux;
sobre o vetor efetuando essas trocas,
} conforme os menores valores forem
} sendo identificados.
} Repare que iremos iterar do ltimo
} elemento at o elemento i+1.
45
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 44.0 5.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =4
if ( 5.0 < 44.0 )
aux =
{
aux=vet[j];
vet[j]= vet[j-1];
Os testes so feitos sempre entre os
vet[j-1]=aux;
valores i e o seu antecessor (i-1)
}
}
}
}
46
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 44.0 5.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =4
if ( 5.0 < 44.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
} Como menor, efetua a troca
}
}
}
47
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 44.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =4
if ( 5.0 < 44.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
48
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =4
if ( 5.0 < 44.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
49
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =3
if ( vet[j] < vet[j-1] )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
50
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =3
if ( 5.0 < 3.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
Aqui o teste falha. Vai para a prxima
vet[j-1]=aux;
iterao do loop interno.
}
}
}
}
51
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =2
if ( vet[j] < vet[j-1] )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
52
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 22.0 3.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =2
if ( 3.0 < 22.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
53
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 3.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =2
if ( 3.0 < 22.0 )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} Como valor menor, efetua a troca
} como visto anteriormente.
}
54
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 3.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =1
if ( vet[j] < vet[j-1] )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
55
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 11.0 3.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =1
if ( 3.0 < 11.0 )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
56
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =1
if ( 3.0 < 11.0 )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} Como valor menor, efetua a troca
} como visto anteriormente.
}
57
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =0
for(j = tam-1; j > i; j--)
{ j =0
if ( vet[j] < vet[j-1] )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} Nesse ponto, como j = 0, saiu do lao
} interno e volta para o externo.
}
58
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =0
if ( vet[j] < vet[j-1] )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} Como o i incrementado, a posio
} 0 do vetor no ser mais modificada
}
59
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =4
if ( vet[j] < vet[j-1] )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
60
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =4
if ( 44.0 < 5.0 )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
61
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =3
if ( vet[j] < vet[j-1] )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
62
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 22.0 5.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =3
if ( 5.0 < 22.0 )
aux = 3.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
63
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 5.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =3
if ( 5.0 < 22.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} Como valor menor, efetua a troca
} como visto anteriormente.
}
64
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 5.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =2
if (vet[j] < vet[j-1] )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
65
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 11.0 5.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =2
if (5.0 < 11.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
66
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =2
if (5.0 < 11.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} Como valor menor, efetua a troca
} como visto anteriormente.
}
67
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =1
for(j = tam-1; j > i; j--)
{ j =1
if (vet[j] < vet[j-1] )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
} Nesse ponto, como j = 1, saiu do lao
} interno e volta para o externo.
68
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =2
for(j = tam-1; j > i; j--)
{ j =1
if (vet[j] < vet[j-1] )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} Como o i incrementado, a posio
} 1 do vetor no ser mais modificada
}
69
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =2
for(j = tam-1; j > i; j--)
{ j =4
if ( 44.0 < 22.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
70
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =2
for(j = tam-1; j > i; j--)
{ j =3
if ( 22.0 < 11.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
71
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =2
for(j = tam-1; j > i; j--)
{ j =2
if ( vet[j] < vet[j-1] )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
} Como j = 2, saiu do lao interno e
} volta para o externo.
72
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =3
for(j = tam-1; j > i; j--)
{ j =2
if ( vet[j] < vet[j-1] )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} Como o i incrementado, a posio
} 2 do vetor no ser mais modificada
}
73
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =3
for(j = tam-1; j > i; j--)
{ j =4
if ( 44.0 < 22.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
}
}
74
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =3
for(j = tam-1; j > i; j--)
{ j =3
if ( 44.0 < 22.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
}
} Como j = 3, saiu do lao interno e
} volta para o externo.
75
Exerccio 2 Teste de Mesa
0 1 2 3 4
void ordena(float vet[], int tam)
{ 3.0 5.0 11.0 22.0 44.0
int i, j;
Variveis:
float aux;
for(i = 0; i <= (tam-2); i++) tam = 5
{
i =4
for(j = tam-1; j > i; j--)
{ j =3
if ( 44.0 < 22.0 )
aux = 5.0
{
aux=vet[j];
vet[j]= vet[j-1];
vet[j-1]=aux;
}
} Como o i incrementado, e sai do lao
} externo, finalizando a procedimento.
}
76
Programa Completo
#include <stdio.h>
int main()
{
int i;
float vet[5]={11.0,22.0,3.0,44.0,5.0};
ordena(vet, 5);
for (i=0; i < 5; i++)
printf("%.2f\n",vet[i]);
return 0;
}
77
Exerccios
78
Exerccios
3) Dada um tabela contendo a idade de 10
alunos, faa um algoritmo que calcule o nmero
de alunos com idade superior a mdia.
81
Vetores
O vetor uma estrutura:
Homognea
Esttica
82
Vetores: Declarao
A sintaxe em C para declarao de varivel do tipo
vetor a seguinte
tipo_primitivo identificador[qtde_elementos];
Exemplo:
// vetor com 5 (0 a 4) elementos do tipo int
int dados[5];
ndices do vetor
0 1 2 3 4
dados
5 elementos quaisquer do tipo int
83
Vetores: Referncia
(Manipulao)
Cada um dos elementos de um vetor referenciado individualmente por meio de
um nmero inteiro entre colchetes aps o nome do vetor.
0 1 2 3 4
Exemplos:
X = valores[1]; //atribui a x o valor da posio 1 do vetor valores
Y = valores[4]; //atribui a x o valor da posio 4 do vetor valores
valores[0] = 3.2; // a posio zero do vetor valores recebe o valor 3.2
84
Vetores: Inicializao
85
Vetores: Inicializao
Observaes:
Os vetores NO so automaticamente inicializados!!!
86
Vetores: Exemplo
Leia um vetor de 10 posies (inteiros) e imprima-o na ordem
invertida (da ltima para a primeira posio).
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numeros[10],i;
for (i=0; i<10;i++){
printf("Digite valor %d: ",i);
scanf("%d", &numeros[i]);
}
printf("Vetor na ordem invertida:\n");
for (i=9; i>=0; i--)
printf("%d\n",numeros[i]);
return 0;
}
87
Vetores e Subrotinas
88
Vetores e Subrotinas: Exemplo
89
Vetores e Subrotinas: Exemplo (1/3)
#include <stdio.h>
#define TAMANHO 10
90
Vetores e Subrotinas: Exemplo (2/3)
int maiorElemento(int vet[], int tam)
{
int i, maior = vet[0];
for(i = 1; i < tam; i++)
{
if(vet[i] > maior)
maior = vet[i];
}
return maior;
}
91
Vetores e Subrotinas: Exemplo (3/3)
float mediaVetor(int vet[], int tam)
{
int i, soma = 0;
for(i = 0; i < tam; i++)
{
soma = soma + vet[i];
}
return soma / (float)tam;
}
int main()
{
int v[TAMANHO];
leVetor(v, TAMANHO);
imprimeVetor(v, TAMANHO);
printf("\nO maior elemento do vetor %d.", maiorElemento(v, TAMANHO));
printf("\nO menor elemento do vetor %d.", menorElemento(v, TAMANHO));
printf("\nA mdia dos valores do vetor %.2f.",mediaVetor(v, TAMANHO));
return 0;
}
92
Exerccios
1) Desenvolva um programa que leia um vetor de nmeros
reais, um escalar e imprima o resultado da multiplicao do
vetor pelo escalar.
94
Exerccios
8) Faa um programa que leia um vetor G[13] que o
gabarito de um teste da loteria esportiva, contendo os valores
1 quando for coluna 1, 0 quando for coluna do meio e 2
quando for coluna 2.
Ler a seguir, para 5 apostadores, seu carto de apostas
(R[13]) e depois da leitura imprimir quantos acertos o
apostador teve.
Faa o teste atravs de funes.