Added final exercise
This commit is contained in:
parent
72394fb7a2
commit
ab3f64f549
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue