1. Write three functions for the attached program sets_of_numbers.c. The sets ar
ID: 669380 • 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.
Explanation / Answer
#include<stdio.h>
void delete_set(int v, int a[])
{
int i,j;
for( i=0;i<50;i++)
{
if(a[i]==v)
{
for(j=i;j<50;j++)
{
a[i]=a[i+1];
}
}
}
}
void set_difference(int a[], int b[], int c[])
{
int i;
for(i=0;i<50;i++)
{
c[i]=a[i]-b[i];
}
}
void sym_difference(int a[], int b[], int c[])
{
int i;
for(i=0;i<50;i++)
{
c[i]=a[i];
}
}
void main()
{
int a[]={1,4,5,7,2,6,4,4,7,8};
int b[]={2,4,5,6,7,8,1,2,3,4};
int c[50];
delete_set(5, a);
set_difference(a, b, c);
sym_difference(a,b, c);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.