diff --git a/aulas/06-vetores/01-media-vars.c b/aulas/06-vetores/01-media-vars.c index 869f7f0..120b568 100644 --- a/aulas/06-vetores/01-media-vars.c +++ b/aulas/06-vetores/01-media-vars.c @@ -1,6 +1,7 @@ #include #include + // Função variádica (número indeterminado de argumentos)... float vf_avg(int count, ...); @@ -8,8 +9,6 @@ float vf_avg(int count, ...); float cf_avg(float a, float b, float c, float d); - - int main(void) { float nota1 = 7.5; @@ -35,9 +34,10 @@ int main(void) { } - - - +// Função comum... +float cf_avg(float a, float b, float c, float d) { + return (a + b + c + d) / 4; +} // Função variádica... float vf_avg(int count, ...) { @@ -53,7 +53,3 @@ float vf_avg(int 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/testes.c b/aulas/06-vetores/testes.c new file mode 100644 index 0000000..61f595e --- /dev/null +++ b/aulas/06-vetores/testes.c @@ -0,0 +1,34 @@ +#include + +#define NBIM 4 + +float favg(float *arr, int size) { + float sum = 0; + for (int i = 0; i < size; i++) { + sum += arr[i]; + } + return sum / size; +} + +int main(void) { + + float notas[NBIM]; + + notas[0] = 7.5; + notas[1] = 9.0; + notas[2] = 8.3; + notas[3] = 8.6; + + float avg = favg(notas, NBIM); + + puts("============"); + + for (int i = 0; i < NBIM; i++) + printf("Bim. %d: %.1f\n", i + 1, notas[i]); + + puts("------------"); + printf("Média : %.1f\n", avg); + puts("============"); + + return 0; +} diff --git a/aulas/06-vetores/vetores.txt b/aulas/06-vetores/vetores.txt index 38a1884..81ee2b0 100644 --- a/aulas/06-vetores/vetores.txt +++ b/aulas/06-vetores/vetores.txt @@ -19,7 +19,7 @@ vetor = endereço na memória ↑ ↑ ↑ ↑ ↑ Total de 16 bytes +----- 4 bytes -----+----- 4 bytes -----+----- 4 bytes -----+----- 4 bytes -----+ ↑ ↑ - &vetor+0 &vetor+4 (*) + &vetor+0 &vetor+1 (*) (*) Problema: acesso fora dos limites do vetor!