Last active
March 31, 2026 01:03
-
-
Save vyper/eb9fa645a089280603fda145892bac2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <limits.h> | |
| #include "lista2.h" | |
| void lerVetor(int vetor[], int tamanho) | |
| { // Ex 1 | |
| for (int i = 0; i < tamanho; i++) | |
| { | |
| printf("Digite um número: "); | |
| scanf("%d", &vetor[i]); | |
| } | |
| imprimirInverso(vetor, tamanho); | |
| } | |
| void imprimirInverso(int vetor[], int tamanho) | |
| { // Ex 1 | |
| for (int i = tamanho - 1; i >= 0; i--) | |
| { | |
| printf("%d\n", vetor[i]); | |
| } | |
| } | |
| float calcularMedia(int vetor[], int tamanho) | |
| { // Ex 2 | |
| int soma = 0.0; | |
| for (int i = 0; i < tamanho; i++) | |
| { | |
| soma += vetor[i]; | |
| } | |
| return (float)soma / tamanho; | |
| } | |
| void imprimirMaiorMenor(int vetor[], int tamanho) | |
| { // Ex 3 | |
| int menor = INT_MAX; | |
| int maior = INT_MIN; | |
| for (int i = 0; i < tamanho; i++) | |
| { | |
| if (vetor[i] < menor) | |
| { | |
| menor = vetor[i]; | |
| } | |
| else if (vetor[i] > maior) | |
| { | |
| maior = vetor[i]; | |
| } | |
| } | |
| printf("Maior: %d\n", maior); | |
| printf("Menor: %d\n", menor); | |
| } | |
| int buscarElemento(int vetor[], int tamanho, int x) | |
| { // Ex 4 | |
| for (int i = 0; i < tamanho; i++) | |
| { | |
| if (vetor[i] == x) | |
| { | |
| return i; | |
| } | |
| } | |
| return -1; | |
| } | |
| int estaOrdenado(int vetor[], int tamanho) | |
| { // Ex 5 | |
| for (int i = 0; i < tamanho - 1; i++) | |
| { | |
| if (vetor[i] > vetor[i + 1]) | |
| { | |
| return 0; | |
| } | |
| } | |
| return 1; | |
| } | |
| int filtrarPares(int vOrigem[], int tamOrigem, int vDestino[]) | |
| { // Ex 6 | |
| int qtdPares = 0; | |
| for (int i = 0; i < tamOrigem; i++) | |
| { | |
| if (vOrigem[i] % 2 == 0) | |
| { | |
| vDestino[i] = vOrigem[i]; | |
| qtdPares++; | |
| } | |
| } | |
| return qtdPares; | |
| } | |
| int calcularProdutoEscalar(int v1[], int v2[], int tamanho) | |
| { // Ex 7 | |
| int produtoEscalar = 0; | |
| for (int i = 0; i < tamanho; i++) | |
| { | |
| produtoEscalar = produtoEscalar + (v1[i] * v2[i]); | |
| } | |
| return produtoEscalar; | |
| } | |
| void inverterVetorInPlace(int vetor[], int tamanho) | |
| { // Ex 8 | |
| for (int i = 0; i < tamanho / 2; i++) | |
| { | |
| vetor[i] = vetor[i] ^ vetor[tamanho - i - 1]; | |
| vetor[tamanho - i - 1] = vetor[i] ^ vetor[tamanho - i - 1]; | |
| vetor[i] = vetor[i] ^ vetor[tamanho - i - 1]; | |
| } | |
| } | |
| int contarFrequencia(int vetor[], int tamanho, int x) | |
| { // Ex 9 | |
| int frequencia = 0; | |
| for (int i = 0; i < tamanho; i++) | |
| { | |
| if (vetor[i] == x) | |
| frequencia++; | |
| } | |
| return frequencia; | |
| } | |
| float calcularMediaPonteiros(int *vetor, int tamanho) | |
| { // Ex 10 | |
| int soma = 0.0; | |
| for (int i = 0; i < tamanho; i++) | |
| { | |
| soma += *(vetor + i); | |
| } | |
| return (float)soma / tamanho; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef LISTA2_H | |
| #define LISTA2_H | |
| // Protótipos das funções solicitadas | |
| void lerVetor(int vetor[], int tamanho); // Ex 1 | |
| void imprimirInverso(int vetor[], int tamanho); // Ex 1 | |
| float calcularMedia(int vetor[], int tamanho); // Ex 2 | |
| void imprimirMaiorMenor(int vetor[], int tamanho); // Ex 3 | |
| int buscarElemento(int vetor[], int tamanho, int x); // Ex 4 | |
| int estaOrdenado(int vetor[], int tamanho); // Ex 5 | |
| int filtrarPares(int vOrigem[], int tamOrigem, int vDestino[]); // Ex 6 | |
| int calcularProdutoEscalar(int v1[], int v2[], int tamanho); // Ex 7 | |
| void inverterVetorInPlace(int vetor[], int tamanho); // Ex 8 | |
| int contarFrequencia(int vetor[], int tamanho, int x); // Ex 9 | |
| float calcularMediaPonteiros(int *vetor, int tamanho); // Ex 10 | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <math.h> | |
| #include "lista2.h" | |
| // Função auxiliar para o relatório de erros | |
| void reportar(char *ex, int condicao) | |
| { | |
| if (condicao) | |
| printf("[OK] %s passou no teste.\n", ex); | |
| else | |
| printf("[ERRO] %s falhou. Verifique a logica!\n", ex); | |
| } | |
| int main() | |
| { | |
| // int v[5] = {10, 20, 30, 40, 50}; | |
| // lerVetor(v, 5); | |
| printf("=== RELATORIO DE CORRECAO AUTOMATICA - LISTA 2 ===\n\n"); | |
| // Cenários de Teste | |
| int vOrdenado[5] = {10, 20, 30, 40, 50}; | |
| int vDesordenado[10] = {5, -2, 10, 0, 10, 8, 15, 3, 10, 7}; // Possui negativos e repetidos | |
| int vPares[10]; | |
| int vVazio[5] = {1, 3, 5, 7, 9}; // Apenas ímpares | |
| // EX 2: Media (Testando com negativos e zero) | |
| // Soma = 64 / 10 = 6.4 | |
| float media = calcularMedia(vDesordenado, 10); | |
| reportar("Exercicio 2 (Media)", fabs(media - 6.6) < 0.01); | |
| // // EX 3: Maior ou menor | |
| imprimirMaiorMenor(vDesordenado, 10); | |
| // EX 4: Busca (Testando valor repetido e inexistente) | |
| int idx1 = buscarElemento(vDesordenado, 10, 10); // O 10 aparece nos índices 2, 4 e 8 | |
| int idx2 = buscarElemento(vDesordenado, 10, 99); // Inexistente | |
| reportar("Exercicio 4 (Busca)", (idx1 != -1) && (idx2 == -1)); | |
| printf(" > Nota: Para valores repetidos, sua funcao retornou o indice %d.\n", idx1); | |
| // EX 5: Ordenacao | |
| int ord1 = estaOrdenado(vOrdenado, 5); | |
| int ord2 = estaOrdenado(vDesordenado, 10); | |
| reportar("Exercicio 5 (Ordenacao)", (ord1 == 1) && (ord2 == 0)); | |
| // EX 6: Filtro de Pares (Caso com apenas ímpares) | |
| int qtdPares = filtrarPares(vVazio, 5, vPares); | |
| reportar("Exercicio 6 (Filtro Pares)", qtdPares == 0); | |
| if (qtdPares > 0) | |
| printf(" > Cuidado: Voce encontrou pares onde nao existiam ou imprimiu lixo!\n"); | |
| // EX 7: Produto escalar | |
| int produtoEscalar = calcularProdutoEscalar(vOrdenado, vVazio, 5); | |
| reportar("Exercicio 7 (Produto Escalar)", produtoEscalar == 950); | |
| // // EX 8: Inversao In-Place (Verificando se alterou o vetor original) | |
| int vOriginal[3] = {1, 2, 3}; | |
| inverterVetorInPlace(vOriginal, 3); | |
| reportar("Exercicio 8 (In-place)", (vOriginal[0] == 3) && (vOriginal[2] == 1)); | |
| // EX 9: Frequencia (Valor repetido 3 vezes) | |
| int freq = contarFrequencia(vDesordenado, 10, 10); | |
| reportar("Exercicio 9 (Frequencia)", freq == 3); | |
| // // EX 10: Aritmetica de Ponteiros | |
| float mediaPtr = calcularMediaPonteiros(vOrdenado, 5); | |
| reportar("Exercicio 10 (Ponteiros)", fabs(mediaPtr - 30.0) < 0.01); | |
| printf("\n=== FIM DO TESTE ===\n"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment