mirror of
https://gitlab.com/blau_araujo/cblc.git
synced 2025-05-11 02:56:35 -03:00
Compare commits
No commits in common. "7560d3a58c9ac22468520308d32995411d8fd42b" and "59e6ed1399883691d2ece7c497a51b7fc58753ec" have entirely different histories.
7560d3a58c
...
59e6ed1399
7 changed files with 38 additions and 165 deletions
|
@ -1,65 +0,0 @@
|
||||||
#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]);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,59 +0,0 @@
|
||||||
#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;
|
|
||||||
}
|
|
23
aulas/06-vetores/01-media.c
Normal file
23
aulas/06-vetores/01-media.c
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#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,15 +1,16 @@
|
||||||
#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
|
||||||
|
|
||||||
// 4 elementos x 4 bytes = 16 bytes
|
puts("Saltos pelo tipo dos elementos (4 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,6 +1,5 @@
|
||||||
#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++)
|
||||||
|
@ -8,11 +7,11 @@ void print_array(int v[], int size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int vetor[VSIZE];
|
int vetor[5];
|
||||||
|
|
||||||
for (int i = 0; i < VSIZE; i ++) vetor[i] = i + 1;
|
for (int i = 0; i < 5; i ++) vetor[i] = i + 1;
|
||||||
|
|
||||||
print_array(vetor, VSIZE);
|
print_array(vetor, 5);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,9 @@
|
||||||
|
|
||||||
[[][Vídeo desta aula]]
|
[[][Vídeo desta aula]]
|
||||||
|
|
||||||
Vetores são estruturas de dados que agrupam valores do mesmo tipo na forma
|
Vetores, são estruturas de dados compostas por valores do mesmo tipo, o que
|
||||||
de listas.
|
possibilita agrupar coleções de dados relacionados entre si em um mesmo
|
||||||
|
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.
|
||||||
|
@ -74,8 +75,8 @@ As diferenças entre os exemplos com vetores e com variáveis separadas são...
|
||||||
|
|
||||||
*Semanticamente:*
|
*Semanticamente:*
|
||||||
|
|
||||||
- Uma variável expressa e dá significado a um valor.
|
- Uma variável expressa um valor na memória.
|
||||||
- Um vetor expressa e dá significado a um conjunto relacionado de valores.
|
- Um vetor expressa o endereço do início de uma cadeia de valores (elementos).
|
||||||
|
|
||||||
*Sintaticamente:*
|
*Sintaticamente:*
|
||||||
|
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
|
|
||||||
|
|
||||||
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