Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C language!! I appreciate your help!! Page of 4 -ZOOM + Project 3, Program Desig

ID: 3888777 • Letter: C

Question

C language!! I appreciate your help!!

Page of 4 -ZOOM + Project 3, Program Design 1. Sets of numbers can be represented using array of Os and 1s. The idea is that a [i]!-0 if i is in the set, and a [1] 0 if it is not. For example, the array a [10] = { 0, 0, 1, 0, 1, 1, 0, 0, 0, 0 would represent the set 12, 4, 5 because a [2], a41, and a [51 have the value 1, and everywhere else a contains zeros. Since the array has a fixed bound, say N, the values in the set are restricted to the range 0...N-1 Write a Cprogram that reads in two sets of numbers A and B, and calculates and print their difference of set A and B:A- B, complement of set A: A and complement of set B: B. A-Bis the set of elements that appear in A but not in B, and that A is the set of every element that is not in A. The values in the sets are restricted to the range 0... 9 1) 2) Name your program set_operations.c The program will read in the number of element in the first set, for example, 4, then read in the numbers in the set, for example, 3 6 8 9. The repeat for the second set. The two sets do not necessarily are of the same size. The sets are stored using arrays of Os and 1s as described above. Calculate the difference of A and B, and complements of the two sets and display the result 3) 4) 5) Sample input/output: Please enter the number of elements in set A: 3 Enter the numbers in set A: 3 5 8 Please enter the number of elements in set B: 4 Enter the numbers in set B: 7 5 93 Output: The difference of set A and B is: 8 The complementf set A is: 0 1 2 4 6 7 9 The complement of set Bis: 0 1246 8

Explanation / Answer

#include<stdio.h>

void printDiff(int *a, int *b) {
   int i;
   for (i = 0; i < 10; i++)
       if (a[i] == 1 && b[i] == 0)
           printf("%d ", i);
}

void printComplement(int *a) {
   int i;
   for (i = 0; i < 10; i++)
       if (a[i] == 0)
           printf("%d ", i);
}

int main() {
   int *a, *b;
   int size_a, size_b;
   int i, tmp;
   a = calloc(10, sizeof(int));
   b = calloc(10, sizeof(int));
   printf("Please enter size of set A: ");
   scanf("%d", &size_a);
   printf("Enter the items in set A: ");
   for (i = 0; i < size_a; i++) {
       scanf("%d", &tmp);
       a[tmp] = 1;
   }
   printf("Please enter size of set B: ");
   scanf("%d", &size_b);
   printf("Enter the items in set A: ");
   for (i = 0; i < size_b; i++) {
       scanf("%d", &tmp);
       b[tmp] = 1;
   }

   printf("The differece of set A and set B is: ");
   printDiff(a, b);
   printf(" ");
   printf("The complement of set A is: ");
   printComplement(a);
   printf(" ");
   printf("The complement of set B is: ");
   printComplement(b);
   printf(" ");   
}

Here you go champ!