Documente Academic
Documente Profesional
Documente Cultură
Vetores, Matrizes,
Strings
Variáveis Compostas
◗ Heterogêneas (estruturas)
Variáveis Compostas Homogêneas
Posição: 0 1 2 3 4 5 6 7 8 9
Arranjos unidimensionais
❖ Arranjos de 2 dimensões
0 1 2 … n1
0 788 598 265 … 156
1 145 258 369 … 196
2 989 565 345 … 526
⋮ ⋮ ⋮ ⋮ ⋱ ⋮
m1 845 153 564 892 210
Arranjos multidimensionais
:: Exemplos
❖ Arranjo de 3 dimensões
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
0 1 2 3
Arranjos multidimensionais
:: Exemplos
❖ Arranjo de 4 dimensões
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
(0,0) (0,1) (0,2) (0,3)
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
(1,0) (1,1) (1,2) (1,3)
Vetores e Matrizes na Linguagem C
<nome>:
<nome>: vetor
vetor [<tamanho1>,<tamanho2>]
[<tamanho1>,<tamanho2>] <tipo>;
<tipo>;
❖ Exemplos :
VAR
VALORES : VETOR [1..3,1..3] DE REAL
Vetores e Matrizes na Linguagem C
❖ Como declarar:
<tipo>
<tipo> <nome>
<nome> [<tamanho1>][<tamanho2>]...;
[<tamanho1>][<tamanho2>]...;
❖ Exemplos:
float
float VetReais[100];
VetReais[100];
int
int Vetor[5][9];
Vetor[5][9];
char
char Nome_cliente[50];
Nome_cliente[50];
float
float cubo[20][12][7];
cubo[20][12][7];
Vetores e Matrizes na Linguagem C
int
int VetInt[n];
VetInt[n];
VetInt ...
0 1 2 3 4 5
2 9 8
X1 Vetor[6] X2
int
int X1;
X1; Vetor[1]
Vetor[1] == 9;
9;
int
int Vetor[6];
Vetor[6]; Vetor[-1]
Vetor[-1] == 2;
2;
int
int X2
X2 Vetor[6]
Vetor[6] == 8;
8; 37
Vetores e Matrizes na Linguagem C
Leia (notas[i,j])
fimpara
Fimpara
Vetores e Matrizes na Linguagem C
:: Exemplos
int
int vetor[5]
vetor[5] == {1,2,3,4,5};
{1,2,3,4,5};
float
float matriz[2][3]
matriz[2][3] == {{1,2,3},{4,5,6}};
{{1,2,3},{4,5,6}};
Vetores e Matrizes na Linguagem C
:: Exemplos
for
for (i=0;
(i=0; i<5;
i<5; i++)
i++)
Vetor[i]
Vetor[i] == ii ++ 1;
1;
for
for (i=0;
(i=0; i<5;
i<5; i++)
i++)
Vetor[i]
Vetor[i] == 55 -- i;
i;
Vetores e Matrizes na Linguagem C
:: Exemplos
for
for (i=0;
(i=0; ii << N;
N; i++)
i++)
for
for (j=0;
(j=0; jj << M;
M; j++)
j++)
Matriz[i][j]
Matriz[i][j] == 0;
0;
Vetores e Matrizes na Linguagem C
:: Exemplos
#define
#define TAM_MAX
TAM_MAX 10
10
double
double VetReais[TAM_MAX],
VetReais[TAM_MAX], VetCopia[TAM_MAX];
VetCopia[TAM_MAX];
for
for (i=0;
(i=0; i<TAM_MAX;
i<TAM_MAX; i++)
i++)
VetCopia[i]
VetCopia[i] == VetReais[i];
VetReais[i];
for
for (i=0;
(i=0; i<TAM_MAX;
i<TAM_MAX; i++)
i++)
{{
printf("Digite
printf("Digite um
um número:
número: ");
");
scanf("%f",
scanf("%f", &Vet[i]);
&Vet[i]);
}}
Vetores e Matrizes na Linguagem C
:: Problema A
Determinar: 0 1 2 3
1. M[3][0] 0 1 2 3 4
2. M[4][2] 1 5 5 3 0
3. M[1][3]
2 1 1 1 1
3 3 2 0 0
4 0 0 1 1
5 1 1 2 2
1. -3
2. 1
3. 0
Vetores e Matrizes na Linguagem C
:: Problema D
39
Vetores e Matrizes na Linguagem C
:: Problema E
Fonte: DENIT
FORTALEZA
SÃO PAULO
SALVADOR
MANAUS
JANEIRO
RIO DE
VELHO
PORTO
BELÉM
40
Strings
❖ Função gets()
◗ Lê string até o primeiro enter
❖ Função scanf()
◗ Lê string até o primeiro espaço em branco
17
Strings
:: Atribuição de valores
char
char curso[15]
curso[15] == "Engenharia";
"Engenharia"; //
// Valido
Valido somente
somente
na
na
//
// declaracao!
declaracao!
char
char curso[15];
curso[15];
strcpy(curso,"Engenharia");
strcpy(curso,"Engenharia"); //
// Requer
Requer biblioteca
biblioteca
//
// string.h
string.h
43
Manipulação de strings
:: Biblioteca string.h
❖ strcat(str1, str2)
Concatena str2 ao final de str1
47
Manipulação de strings
:: Biblioteca string.h
❖ strupr(str)
Converte uma string para maiúsculas.
❖ strlwr(str)
Converte uma string para minúsculas.
❖ strrev(str)
Inverte o conteúdo de uma string.
❖ strset(str,char)
Substitui todos os caracteres de uma string pelo caractere
especificado.
48
Manipulação de strings
:: Conversão para números
Função Converte
atoi(<str>) String em int
atof(<str>) String em float
itoa(<int>) Int em string
49
Manipulação de caracteres
❖ Função toupper
◗ Converte seu argumento para uma letra maiúscula:
<var1>
<var1> == toupper(var2);
toupper(var2);
❖ Função tolower
◗ Converte seu argumento para uma letra minúscula:
<var1>
<var1> == tolower(var2);
tolower(var2);
44
Manipulação de caracteres
:: Outras funções
Função Testa se seu argumento é um
isalnum caractere alfanumérico
isalpha caractere alfabético
isascii caractere ASCII (0 a 127)
iscntrl caractere de controle (00x1F ou 0x7F)
isgraph caractere imprimível na tela (não leva espaço em
consideração)
isprint caractere imprimível na tela (leva espaço em
consideração)
45
Manipulação de caracteres
:: Outras funções
Função Testa se seu argumento é um
islower caractere minúsculo
isupper caractere maiúsculo
ispunct caractere de pontuação
isspace caractere de espaço, tabulação
isdigit caractere numérico (09)
isxdigit dígito hexadecimal (09, af ou AF)
46
Questões