Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- bool exists(int *array, size_t array_length, int value);
- size_t count_common(int *array_a, size_t size_a, int *array_b, size_t size_b);
- int* differences(int *array_a, size_t size_a, int *array_b, size_t size_b);
- bool multiple(int *array, size_t array_length, int value);
- size_t count_multiples(int *array_a, size_t size_a, int *array_b, size_t size_b);
- int* multiples(int *array_a, size_t size_a, int *array_b, size_t size_b);
- int main()
- {
- size_t size_a, size_b;
- printf("Insert the length of the two arrays: ");
- scanf("%d%d", &size_a, &size_b);
- int *array_a = malloc(sizeof(int) * size_a);
- int *array_b = malloc(sizeof(int) * size_b);
- // Filling A
- printf("Filling A: \n");
- for(size_t i = 0; i < size_a; i++) {
- scanf("%d", &array_a[i]);
- }
- // Filling B
- printf("Filling B: \n");
- for(size_t i = 0; i < size_b; i++) {
- scanf("%d", &array_b[i]);
- }
- int elements_found = count_common(array_a, size_a, array_b, size_b);
- int *array_found = differences(array_a, size_a, array_b, size_b);
- // Prints the result
- printf("Printing b in B but not in A: \n");
- for(size_t i = 0; i < elements_found; i++) {
- printf("%d ", array_found[i]);
- }
- int multiples_found = count_multiples(array_a, size_a, array_b, size_b);
- int *array_multiples = multiples(array_a, size_a, array_b, size_b);
- // Prints the result
- printf("\nPrinting b MOD a for each element of B and A: \n");
- for(size_t i = 0; i < multiples_found; i++) {
- printf("%d ", array_multiples[i]);
- }
- return 0;
- }
- bool exists(int *array, size_t array_length, int value) {
- for(size_t i = 0; i < array_length; i++) {
- if(array[i] == value)
- return true;
- }
- return false;
- }
- size_t count_common(int *array_a, size_t size_a, int *array_b, size_t size_b) {
- size_t elements_found = 0;
- for(size_t i = 0; i < size_b; i++) {
- if(!exists(array_a, size_a, array_b[i]))
- elements_found++;
- }
- return elements_found;
- }
- int* differences(int *array_a, size_t size_a, int *array_b, size_t size_b) {
- // Count the amount of differente values between A and B
- int elements_found = count_common(array_a, size_a, array_b, size_b);
- // array_found of the size of the elements found
- int *array_found = malloc(sizeof(int) * elements_found);
- elements_found = 0;
- for(size_t i = 0; i < size_b; i++) {
- if(!exists(array_a, size_a, array_b[i]))
- array_found[elements_found++] = array_b[i];
- }
- return array_found;
- }
- bool multiple(int *array, size_t array_length, int value) {
- for(size_t i = 0; i < array_length; i++) {
- if(value % array[i] == 0)
- return true;
- }
- return false;
- }
- size_t count_multiples(int *array_a, size_t size_a, int *array_b, size_t size_b) {
- size_t elements_found = 0;
- for(size_t i = 0; i < size_b; i++) {
- if(multiple(array_a, size_a, array_b[i]))
- elements_found++;
- }
- return elements_found;
- }
- int* multiples(int *array_a, size_t size_a, int *array_b, size_t size_b) {
- // Count the amount of differente values between A and B
- int elements_found = count_multiples(array_a, size_a, array_b, size_b);
- // array_found of the size of the elements found
- int *array_found = malloc(sizeof(int) * elements_found);
- elements_found = 0;
- for(size_t i = 0; i < size_b; i++) {
- if(multiple(array_a, size_a, array_b[i]))
- array_found[elements_found++] = array_b[i];
- }
- return array_found;
- }
Add Comment
Please, Sign In to add comment