Escolar Documentos
Profissional Documentos
Cultura Documentos
Linguagem C
Vetores, Matrizes, Strings
Eliane Pozzebon
Variáveis Compostas
Heterogêneas (estruturas)
Variáveis Compostas Homogêneas
Notas: 6,1 2,3 9,4 5,1 8,9 9,8 10 7,0 6,3 4,4
Posição: 0 1 2 3 4 5 6 7 8 9
Variáveis Compostas Homogêneas
:: Exemplo
Posição do livro
0 1 2 … n-1
⋮ ⋮ ⋮ ⋮ ⋱ ⋮
Arranjos de 2 dimensões
0 1 2 … n-1
⋮ ⋮ ⋮ ⋮ ⋱ ⋮
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
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] == 8;
Vetor[6] 8;
Vetores e Matrizes na Linguagem C
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] == 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]);
}}
Strings
Função gets()
Lê string até o primeiro enter
Função scanf()
Lê string até o primeiro espaço em branco
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
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);
Manipulação de caracteres
:: Outras funções
strcat(str1, str2)
Concatena str2 ao final de str1
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.
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
Referências:
Livros:
• Fundamentos da programaç
programação de computadores
• C a linguagem de programaç
programação padrão ANSI
• C completo e total