Last active
September 4, 2016 13:21
-
-
Save eduardojunio/756fa2fa9748af9aed2d41c7abb088c5 to your computer and use it in GitHub Desktop.
263 - Interseção entre listas The Huxley
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> | |
#define TAMANHO 20 | |
void lerArray(int tamanho, int array[]); | |
void ordernarCrescente(int tamanho, int array[]); | |
void removerRepetidos(int tamanho, int array[]); | |
int existeNasArrays(int numero, int tamanho, int array1[], int array2[]); | |
void interArrays(int tamanho, int array1[], int array2[]); | |
int main() { | |
int array1[TAMANHO+1], array2[TAMANHO+1]; | |
lerArray(TAMANHO, array1); | |
lerArray(TAMANHO, array2); | |
ordernarCrescente(TAMANHO, array1); | |
ordernarCrescente(TAMANHO, array2); | |
removerRepetidos(TAMANHO, array1); | |
removerRepetidos(TAMANHO, array2); | |
interArrays(TAMANHO, array1, array2); | |
return 0; | |
} | |
void lerArray(int tamanho, int array[]) { | |
int i; | |
for (i = 0; i < tamanho; i++) { | |
scanf("%d", &array[i]); | |
} | |
} | |
void ordernarCrescente(int tamanho, int array[]) { | |
int i, j, k; | |
for (i = 0; i < tamanho; i++) { | |
for (j = i + 1; j < tamanho; j++) { | |
if (array[i] > array[j]) { | |
k = array[j]; | |
array[j] = array[i]; | |
array[i] = k; | |
} | |
} | |
} | |
} | |
void removerRepetidos(int tamanho, int array[]) { | |
int i, j, k; | |
for (i = 0; i < tamanho; i++) { | |
for (j = i + 1; j < tamanho;) { | |
if (array[j] == array[i]) { | |
for (k = j; k < tamanho; k++) { | |
array[k] = array[k + 1]; | |
} | |
tamanho--; | |
} else { | |
j++; | |
} | |
} | |
} | |
} | |
int existeNasArrays(int numero, int tamanho, int array1[], int array2[]) { | |
int i, j = 0, k = 0; | |
for (i = 0; i < tamanho; i++) { | |
if (array1[i] == numero) { | |
j++; | |
} | |
if (array2[i] == numero) { | |
k++; | |
} | |
} | |
if (j > 0 && k > 0) { | |
return 1; | |
} | |
return 0; | |
} | |
void interArrays(int tamanho, int array1[], int array2[]) { | |
int i, j = 0; | |
for (i = 0; i < tamanho; i++) { | |
if (existeNasArrays(array1[i], tamanho, array1, array2)) { | |
printf("%d\n", array1[i]); | |
j++; | |
} | |
} | |
if (j == 0) { | |
printf("VAZIO\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment