From ab3f64f5495c9c3ee8dbea312cc5a7c9bd1b1167 Mon Sep 17 00:00:00 2001 From: Abreu Date: Fri, 20 Aug 2021 12:28:01 -0300 Subject: [PATCH] Added final exercise --- Exercícios/23/decrescente.c | 75 ++++++++++++++++++++++++++++++++++++ Exercícios/24/uniq.c | 49 +++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 Exercícios/23/decrescente.c create mode 100644 Exercícios/24/uniq.c diff --git a/Exercícios/23/decrescente.c b/Exercícios/23/decrescente.c new file mode 100644 index 0000000..3eedf11 --- /dev/null +++ b/Exercícios/23/decrescente.c @@ -0,0 +1,75 @@ +#include +#include +#include + +typedef struct { + int size, ** values; +} matrix; + +typedef struct { + int value, quantity, ** positions; +} greatest; + +greatest find_in (matrix a) { + int i; + greatest k = {0}; + + for (i = 0; i < a.size * a.size; i++) { + if (a.values[i / a.size][i % a.size] < k.value) + continue; + if (a.values[i / a.size][i % a.size] > k.value) { + k.value = a.values[i / a.size][i % a.size]; + k.quantity = 0; + } + k.positions = realloc(k.positions, (k.quantity + 1) * sizeof(k.positions)); + k.positions[k.quantity] = realloc(k.positions[k.quantity], 2 * sizeof(k.positions[0])); + k.positions[k.quantity][0] = i / a.size; + k.positions[k.quantity][1] = i % a.size; + k.quantity++; + } + return k; +} + +int main () { + int i = 0, j; + matrix a; + greatest k; + + printf("Este programa lista em ordem decrescente os valores depositados em uma matriz, e suas respectivas coordenadas.\n"); + + printf("Digite o tamano n da matriz: "); + if (!(scanf("%d", &a.size)) || a.size <= 0) { + printf("Tamanho inválido\n"); + return 1; + } + + printf("Digite, separados por espaço ou quebra de linha, os %d valores inteiros e positivos a serem depositados na matriz:\n", a.size * a.size); + a.values = malloc (a.size * sizeof(a.values)); + while (i < a.size * a.size) { + a.values[i / a.size] = malloc (a.size * sizeof(a.values[0])); + do { + if (!(scanf(" %d", &a.values[i / a.size][i % a.size])) + || a.values[i / a.size][i % a.size] < 0) { + printf("Valor inválido\n"); + return 1; + } + i++; + } while (i % a.size != 0); + } + + printf("Elemento | Linha | Coluna\n"); + printf("-------------------------\n"); + for (i = 0; i < a.size * a.size;) { + k = find_in(a); + for (j = 0; j < k.quantity; j++) { + printf("%-*d | %-*d | %-*d\n", + (int) strlen("Elemento"), k.value, + (int) strlen("Linha"), k.positions[j][0], + (int) strlen("Coluna"), k.positions[j][1] + ); + a.values[k.positions[j][0]][k.positions[j][1]] = -1; + } + i += k.quantity; + } + return 0; +} diff --git a/Exercícios/24/uniq.c b/Exercícios/24/uniq.c new file mode 100644 index 0000000..3070f8e --- /dev/null +++ b/Exercícios/24/uniq.c @@ -0,0 +1,49 @@ +#include +#include + +int main () { + int i = 0, j, k, l = 1; + char div; + float * input, * heap, * output; + + printf("Este programa recebe uma lista de valores reais e elimina repetições nesta.\n"); + + printf("Digite uma sequência de valores separadas por espaço e, ao final, pressione ENTER:\n"); + input = malloc(sizeof(input)); + do { + if (i == l - 1) { + l *= 2; + input = realloc(input, l * sizeof(input)); + } + if (!(scanf("%f%c", &input[i], &div))) { + printf("Valor inválido passado.\n"); + return 1; + } + i++; + } while(div != '\n'); + + /* Algoritmo para exclusão de repetições */ + output = malloc(i * sizeof(output)); + for (j = 0; i > 0; j++) { + l = 0; + output[j] = input[l]; + heap = malloc(i * sizeof(heap)); + + for (k = 1; k < i; k++) { + if (output[j] == input[k]) + continue; + heap[l] = input[k]; + l++; + } + + free(input); + input = heap; + i = l; + } + + printf("\nLista de valores sem repetições:\n"); + for (i = 0; i < j; i++) + printf("%.2g ", output[i]); + printf("\n"); + return 0; +}