mirror of
https://gitlab.com/blau_araujo/cblc.git
synced 2025-05-10 10:36:37 -03:00
Compare commits
2 commits
59e6ed1399
...
7560d3a58c
Author | SHA1 | Date | |
---|---|---|---|
7560d3a58c | |||
ba9e894b15 |
7 changed files with 165 additions and 38 deletions
65
aulas/06-vetores/01-media-arr.c
Normal file
65
aulas/06-vetores/01-media-arr.c
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// 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]);
|
||||||
|
}
|
||||||
|
|
59
aulas/06-vetores/01-media-vars.c
Normal file
59
aulas/06-vetores/01-media-vars.c
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
// 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("Bim. 1: %.1f\n", nota1);
|
||||||
|
printf("Bim. 2: %.1f\n", nota2);
|
||||||
|
printf("Bim. 3: %.1f\n", nota3);
|
||||||
|
printf("Bim. 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;
|
||||||
|
}
|
|
@ -1,23 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
|
@ -1,16 +1,15 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(void) {
|
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);
|
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);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define VSIZE 5
|
||||||
|
|
||||||
void print_array(int v[], int size) {
|
void print_array(int v[], int size) {
|
||||||
for (int i = 0; i < size; i++)
|
for (int i = 0; i < size; i++)
|
||||||
|
@ -7,11 +8,11 @@ void print_array(int v[], int size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,8 @@
|
||||||
|
|
||||||
[[][Vídeo desta aula]]
|
[[][Vídeo desta aula]]
|
||||||
|
|
||||||
Vetores, são estruturas de dados compostas por valores do mesmo tipo, o que
|
Vetores são estruturas de dados que agrupam valores do mesmo tipo na forma
|
||||||
possibilita agrupar coleções de dados relacionados entre si em um mesmo
|
de listas.
|
||||||
elemento da linguagem.
|
|
||||||
|
|
||||||
- Cada valor em um vetor é um /elemento/.
|
- Cada valor em um vetor é um /elemento/.
|
||||||
- O nome do vetor é o endereço do seu primeiro 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:*
|
*Semanticamente:*
|
||||||
|
|
||||||
- Uma variável expressa um valor na memória.
|
- Uma variável expressa e dá significado a um valor.
|
||||||
- Um vetor expressa o endereço do início de uma cadeia de valores (elementos).
|
- Um vetor expressa e dá significado a um conjunto relacionado de valores.
|
||||||
|
|
||||||
*Sintaticamente:*
|
*Sintaticamente:*
|
||||||
|
|
||||||
|
|
27
aulas/06-vetores/vetores.txt
Normal file
27
aulas/06-vetores/vetores.txt
Normal file
|
@ -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!
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue