1. Write three functions for the attached program sets_of_numbers.c. The sets ar
ID: 669407 • Letter: 1
Question
1. Write three functions for the attached program sets_of_numbers.c. The sets are represented as arrays ofintegers. Assume the sets has no more than 50 elements. The zeroth element of each array has a specialmeaning: it indicates the number of elements in the set. The set elements themselves will be located inarray elements index: 1 through N. Read the program and add the following functionalities:
1) Write a function that deletes a specific element from a set.
void delete_set(int v, int a[]);
Add function declaration of delete_set after the other function declarations. Uncomment thestatements in main function that calls delete_set.
2) Write a function that calculated the set difference of set a and b, store the result in set c. The setdifference is the set of all elements that are in set a, but not in b.
void set_difference(int a[], int b[], int c[]);
Add function declaration of set_difference after the other function declarations. Uncommentthe statements in main function that calls set_difference.
3) Write a function that calculates the symmetric difference between two sets. Symmetric differencebetween two sets a and b is defined as the set of all elements in either a or b, but not both. Hint:you’ll find set_difference and union_set useful.
void sym_difference(int a[], int b[], int c[]);
Add function declaration of sym_difference after the other function declarations. Uncommentthe statements in main function that calls sym_difference.
2. Assume the + operator is not available. Write a program that takes two numbers as input and display theaddition of them. The program should include a function add (int n, int m) that will add twonumbers using only recursion and the increment and decrement operators. Either of the two numberscan be zero, positive, or negative.
Hint: add(n, m)= add(++n, --m), if m is positive, and add(n, 0)=n.
---
SET OF NUMBERS.C
#include <stdio.h>
#define MAX_SET_SIZE 50#define TRUE 1#define FALSE 0
void read_set(int a[]);void print_set(int a[]);void enter_set(int v, int a[]);int element_of(int v, int a[]);void union_set(int a[], int b[], int c[]);void intersect_set(int a[], int b[], int c[]);
/*read in two sets and calcuates the result of various operations on the sets.*/int main(){ int a[MAX_SET_SIZE+1]; int b[MAX_SET_SIZE+1]; int c[MAX_SET_SIZE+1];
read_set(a); read_set(b); printf("set A: "); print_set(a);
printf("set B: "); print_set(b); union_set(a, b, c); printf("Union of A and B: "); print_set(c);
intersect_set(a, b, c); printf("Intersection of A and B: "); print_set(c);
/* delete_set(5, a); printf("Deleting 5 from set A: "); print_set(a);
set_difference(a, b, c); printf("Set difference of A and B: "); print_set(c);
sym_difference(a, b, c); printf("Symmetric difference of A and B: "); print_set(c); */ return 0;}
/*read_set reads integes from standard input and uses enter_set to add them to
the setavoiding repeated entries. */void read_set(int a[]){ a[0]=0; //a[0] is a special elements. It indicates the number of elements inset a. int value; printf("Enter set values, ending with -1. "); scanf("%d", &value); while(value != -1){ enter_set(value, a); scanf("%d", &value); }}
/*print_set prints out the content of the set, in the traditional mathemeticalnotation,as a list of items separated by commas, surrounded by braces. */void print_set(int a[0]){ int i; printf("{"); //the opening brace for the set if(a[0]<=0) printf("There is no elements in the set. "); else{
//print elements in the set, separated by comma printf("%d", a[1]); for(i = 2; i <= a[0];i++)
printf(" ,%d", a[i]);
}
printf("} "); //the closing brace for the set}
/*enter_set enters an element in the set, avoiding repeated entries.*/void enter_set(int v, int a[]){
if(a[0] == MAX_SET_SIZE)
printf("Max set size exceeded ");
else if (!element_of(v, a)){ //calls element_of function to determine anelement is already in the set
a[0]++; a[a[0]]=v; }}
/*element_of returns 1(TRUE) or 0(FALSE) depending on whether the element is inthe set*/int element_of(int v, int a[]){ int i; for(i = 1; i <= a[0]; i++) if ( a[i] == v) return TRUE; return FALSE;}
/*union_set carries out the union operation on set a and b, storing the resultin set c.*/
void union_set(int a[], int b[], int c[]){ int i; c[0]=0; //c[0] is a special elements. It indicates the number of elements inset c. for(i=1; i<=a[0]; i++) enter_set(a[i], c);
for(i=1; i<=b[0]; i++) enter_set(b[i], c);
}
/*intersect_set carries out the intersect operation on set a and b, storing theresult in set c.*/void intersect_set(int a[], int b[], int c[]){ int i; c[0]=0; //c[0] is a special elements. It indicates the number of elements inset c. for(i=1; i<=a[0]; i++)
if(element_of(a[i], b)) enter_set(a[i], c);
}
Explanation / Answer
Firstly your code is not intended which make very difficult to understand.......
Ok Now intended code with three functions implemented...comments added too...
#include <stdio.h>
#define MAX_SET_SIZE 50#
define TRUE 1#
define FALSE 0
void read_set(int a[]);
void print_set(int a[]);
void enter_set(int v, int a[]);
int element_of(int v, int a[]);
void union_set(int a[], int b[], int c[]);
void intersect_set(int a[], int b[], int c[]);
/*read in two sets and calcuates the result of various operations on the sets.*/
int main(){
int a[MAX_SET_SIZE+1];
int b[MAX_SET_SIZE+1];
int c[MAX_SET_SIZE+1];
read_set(a);
read_set(b);
printf("set A: ");
print_set(a);
printf("set B: ");
print_set(b);
union_set(a, b, c);
printf("Union of A and B: ");
print_set(c);
intersect_set(a, b, c);
printf("Intersection of A and B: ");
print_set(c);
/* delete_set(5, a); printf("Deleting 5 from set A: "); print_set(a);
set_difference(a, b, c); printf("Set difference of A and B: "); print_set(c);
sym_difference(a, b, c); printf("Symmetric difference of A and B: "); print_set(c); */
return 0;}
void read_set(int a[]){
a[0]=0;
int value;
printf("Enter set values, ending with -1. ");
scanf("%d", &value);
while(value != -1){
enter_set(value, a);
scanf("%d", &value);
}
}
void print_set(int a[0]){
int i; printf("{"); //the opening brace for the set
if(a[0]<=0)
printf("There is no elements in the set. ");
else{
//print elements in the set, separated by comma
printf("%d", a[1]);
for(i = 2; i <= a[0];i++)
printf(" ,%d", a[i]);
}
printf("} ");
}
void enter_set(int v, int a[]){
if(a[0] == MAX_SET_SIZE)
printf("Max set size exceeded ");
else if (!element_of(v, a)){
a[0]++;
a[a[0]]=v;
}
}
int element_of(int v, int a[]){
int i;
for(i = 1; i <= a[0]; i++)
if ( a[i] == v)
return TRUE;
return FALSE;
}
void union_set(int a[], int b[], int c[]){
int i;
c[0]=0;
for(i=1; i<=a[0]; i++)
enter_set(a[i], c);
for(i=1; i<=b[0]; i++)
enter_set(b[i], c);
}
void intersect_set(int a[], int b[], int c[]){
int i;
c[0]=0;
for(i=1; i<=a[0]; i++)
if(element_of(a[i], b))
enter_set(a[i], c);
}
void delete_set(int v, int a[]){
int i;
int position = -1,c;
for(i=1; i<=a[0]; i++){ //to calculate position
if(a[i] == v){
position = i;
break;
}
}
if(position != -1){ //moving elements ...such that deleting element
for ( c = position - 1 ; c < a[0]-1 ; c++ )
a[c] = a[c+1];
}
}
void set_difference(int a[], int b[], int c[]){
int i,j,temp=0,found=0;
for(i=0;i<=a[0];i++){
for(j=0;j<a[0];j++){
if(a[i]) == b[j]{ //searching whether element is present or not
found = 1;
}
}
if(found == 0){
c[temp] = a[i]; //pushing element of a , which is not in b
temp++;
}
found = 0;
}
}
void sym_difference(int a[], int b[], int c[]){
int i,j,temp=0,found=0;
for(i=0;i<=a[0];i++){
for(j=0;j<a[0];j++){
if(a[i]) == b[j]{ //searching whether element is present or not
found = 1;
}
}
if(found == 1){
c[temp] = a[i]; //pushing element of a , which is in b
temp++;
}
found = 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.