diff --git a/aulas/06-vetores/01-media-arr.c b/aulas/06-vetores/01-media-arr.c new file mode 100644 index 0000000..1fe8fe9 --- /dev/null +++ b/aulas/06-vetores/01-media-arr.c @@ -0,0 +1,65 @@ +#include + +// Soma os elementos de um vetor de tipo float... +float farr_sum(float arr[], int size); + +// Média dos elementos de um vetor do tipo float... +float farr_avg(float arr[], int size); + +// Imprime as notas... +void print_grades(float arr[], int size); + + +#define NBIM 4 + +int main(void) { + + float notas[NBIM]; + + notas[0] = 7.5; + notas[1] = 9.0; + notas[2] = 8.3; + notas[3] = 8.6; + + /* Cálculo e impressão por iteração direta */ + puts("============"); + float sum = 0; + for (int i = 0; i < NBIM; i++) { + sum += notas[i]; + printf("Bim. %d: %.1f\n", i + 1, notas[i]); + } + puts("------------"); + printf("Média : %.1f\n", sum / NBIM); + puts("============"); + + /* Cálculo e impressão por funções + puts("============"); + print_grades(notas, NBIM); + puts("------------"); + printf("Média : %.1f\n", farr_avg(notas, NBIM)); + puts("============"); + */ + + return 0; +} + + +// Média dos elementos de um vetor do tipo float... +float farr_avg(float arr[], int size) { + return farr_sum(arr, size) / size; +} + +// Soma os elementos de um vetor de tipo float... +float farr_sum(float arr[], int size) { + float sum = 0; + for (int i = 0; i < size; i++) + sum += arr[i]; + return sum; +} + +// Imprime as notas... +void print_grades(float arr[], int size) { + for (int i = 0; i < size; i++) + printf("Bim. %d: %.1f\n", i + 1, arr[i]); +} + diff --git a/aulas/06-vetores/01-media-vars.c b/aulas/06-vetores/01-media-vars.c new file mode 100644 index 0000000..7179f31 --- /dev/null +++ b/aulas/06-vetores/01-media-vars.c @@ -0,0 +1,59 @@ +#include +#include + +// Função variádica (número indeterminado de argumentos)... +float vf_avg(int count, ...); + +// Função comum (só serve para este caso)... +float cf_avg(float a, float b, float c, float d); + + + + +int main(void) { + + float nota1 = 7.5; + float nota2 = 9.0; + float nota3 = 8.3; + float nota4 = 8.6; + + float avg; + + // Cálculo direto... + avg = (nota1 + nota2 + nota3 + nota4) / 4; + + puts("============"); + printf("Nota 1: %.1f\n", nota1); + printf("Nota 2: %.1f\n", nota2); + printf("Nota 3: %.1f\n", nota3); + printf("Nota 4: %.1f\n", nota4); + puts("------------"); + printf("Média : %.1f\n", avg); + puts("============"); + + return 0; +} + + + + + + +// Função variádica... +float vf_avg(int count, ...) { + va_list args; + va_start(args, count); + + float total = 0; + for (int i = 0; i < count; i++) + total += va_arg(args, double); + + va_end(args); + return total / count; +} + + +// Função comum... +float cf_avg(float a, float b, float c, float d) { + return (a + b + c + d) / 4; +} diff --git a/aulas/06-vetores/01-media.c b/aulas/06-vetores/01-media.c deleted file mode 100644 index d00af3d..0000000 --- a/aulas/06-vetores/01-media.c +++ /dev/null @@ -1,23 +0,0 @@ -#include - -int main(void) { - - float nota1 = 7.5; - float nota2 = 9.0; - float nota3 = 8.3; - float nota4 = 8.6; - - float soma = nota1 + nota2 + nota3 + nota4; - float media = soma / 4; - - puts("============"); - printf("Nota 1: %.1f\n", nota1); - printf("Nota 2: %.1f\n", nota2); - printf("Nota 3: %.1f\n", nota3); - printf("Nota 4: %.1f\n", nota4); - puts("------------"); - printf("Média : %.1f\n", media); - puts("============"); - - return 0; -} diff --git a/aulas/06-vetores/02-addr.c b/aulas/06-vetores/02-addr.c index 40aa440..7b9049e 100644 --- a/aulas/06-vetores/02-addr.c +++ b/aulas/06-vetores/02-addr.c @@ -1,16 +1,15 @@ #include int main(void) { - - int v[] = {1, 2, 3, 4}; // 4 elementos x 4 bytes = 16 bytes - long a = 10; // 1 valor de 8 bytes - puts("Saltos pelo tipo dos elementos (4 bytes)..."); + // 4 elementos x 4 bytes = 16 bytes + int v[] = {1, 2, 3, 4}; + + puts("Saltos pelos elementos (4 bytes)..."); for (int i = 0; i < 4; i++) printf("%p\n", v + i); - puts("Saltos pelo tamanho total do vetor (4 x 4 bytes)..."); + + puts("Saltos pelo tamanho do vetor (4 x 4 bytes)..."); for (int i = 0; i < 4; i++) printf("%p\n", &v + i); - puts("Equivale ao comportamento de variáveis (8 bytes)..."); - for (int i = 0; i < 4; i++) printf("%p\n", &a + i); return 0; } diff --git a/aulas/06-vetores/04-func.c b/aulas/06-vetores/04-func.c index 303e6aa..7fc3139 100644 --- a/aulas/06-vetores/04-func.c +++ b/aulas/06-vetores/04-func.c @@ -1,5 +1,6 @@ #include +#define VSIZE 5 void print_array(int v[], int size) { for (int i = 0; i < size; i++) @@ -7,11 +8,11 @@ void print_array(int v[], int size) { } int main() { - int vetor[5]; + int vetor[VSIZE]; - for (int i = 0; i < 5; i ++) vetor[i] = i + 1; + for (int i = 0; i < VSIZE; i ++) vetor[i] = i + 1; - print_array(vetor, 5); + print_array(vetor, VSIZE); return 0; } diff --git a/aulas/06-vetores/README.org b/aulas/06-vetores/README.org index ee4f1b0..5fee045 100644 --- a/aulas/06-vetores/README.org +++ b/aulas/06-vetores/README.org @@ -8,9 +8,8 @@ [[][Vídeo desta aula]] -Vetores, são estruturas de dados compostas por valores do mesmo tipo, o que -possibilita agrupar coleções de dados relacionados entre si em um mesmo -elemento da linguagem. +Vetores são estruturas de dados que agrupam valores do mesmo tipo na forma +de listas. - Cada valor em um vetor é um /elemento/. - O nome do vetor é o endereço do seu primeiro elemento. @@ -75,8 +74,8 @@ As diferenças entre os exemplos com vetores e com variáveis separadas são... *Semanticamente:* -- Uma variável expressa um valor na memória. -- Um vetor expressa o endereço do início de uma cadeia de valores (elementos). +- Uma variável expressa e dá significado a um valor. +- Um vetor expressa e dá significado a um conjunto relacionado de valores. *Sintaticamente:* diff --git a/aulas/06-vetores/vetores.txt b/aulas/06-vetores/vetores.txt new file mode 100644 index 0000000..38a1884 --- /dev/null +++ b/aulas/06-vetores/vetores.txt @@ -0,0 +1,27 @@ + + + VETORES + ======= + + - Um dos tipos compostos da linguagem C. + - Estruturas de dados que agrupam valores de mesmo tipo. + - O nome do vetor é o endereço de seu primeiro elemento. + - Não confunda a sintaxe de vetores com o conceito de vetor! + + + NOME + ÍNDICE (× TIPO) ==> NOME[ÍNDICE] + + vetor+0 vetor+1 vetor+2 vetor+3 vetor+4 (*) + ↓ ↓ ↓ ↓ ↓ + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+ + int vetor[4] = {1, 2, 3, 4}; ---> | 01 | 00 | 00 | 00 | 02 | 00 | 00 | 00 | 03 | 00 | 00 | 00 | 04 | 00 | 00 | 00 | + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+ + vetor = endereço na memória ↑ ↑ ↑ ↑ ↑ + Total de 16 bytes +----- 4 bytes -----+----- 4 bytes -----+----- 4 bytes -----+----- 4 bytes -----+ + ↑ ↑ + &vetor+0 &vetor+4 (*) + + + (*) Problema: acesso fora dos limites do vetor! + +