How to resolve the algorithm Symmetric difference step by step in the C programming language
How to resolve the algorithm Symmetric difference step by step in the C programming language
Table of Contents
Problem Statement
Given two sets A and B, compute
( A ∖ B ) ∪ ( B ∖ A ) .
{\displaystyle (A\setminus B)\cup (B\setminus A).}
That is, enumerate the items that are in A or B but not both. This set is called the symmetric difference of A and B. In other words:
( A ∪ B ) ∖ ( A ∩ B )
{\displaystyle (A\cup B)\setminus (A\cap B)}
(the set of items that are in at least one of A or B minus the set of items that are in both A and B). Optionally, give the individual differences (
A ∖ B
{\displaystyle A\setminus B}
and
B ∖ A
{\displaystyle B\setminus A}
) as well.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Symmetric difference step by step in the C programming language
Source Code Explanation
Program 1
This program calculates the set difference and symmetric difference between two lists of strings.
- Header Files:
stdio.h
,string.h
: Standard I/O and string manipulation functions.
- Data Structures:
- Constant character arrays (
A
,B
): Represent the two lists of strings. LEN(x)
: Macro to calculate the length of an array.
- Constant character arrays (
- Functions:
uniq(const char *x[], int len)
: Removes duplicate items from a string array.in_set(const char *const x[], int len, const char *match)
: Checks if a string is present in a string array.show_diff(const char *const x[], int lenx, const char *const y[], int leny)
: Prints the elements inx
that are not present iny
.show_sym_diff(const char *const x[], int lenx, const char *const y[], int leny)
: Prints the elements that are present in eitherx
ory
but not in both.
- Main Function:
- Calls
uniq
on both lists to remove duplicates. - Calculates and prints the set difference (
A \ B
,B \ A
) and symmetric difference (A △ B
) using theshow_diff
andshow_sym_diff
functions.
- Calls
Program 2
This program also calculates the set difference and symmetric difference, but it uses a different approach:
- Header Files:
assert.h
,stdio.h
,string.h
,stdlib.h
: Asserting, I/O, string manipulation, and memory allocation functions.
- Constants and Variables:
- Constant strings (
john
,bob
,jim
, etc.): Define the elements of the sets. - String arrays (
setA
,setB
): Represent the two sets.
- Constant strings (
- Function:
symmdiff(int *sym_size, EsdFunction f, const char *setA[], int setAsize, const char *setB[], int setBsize)
: Calculates either the set difference or symmetric difference based on thef
parameter.isSet(const char *list[], int lsize)
: Checks if a list of strings is a set (unique elements).printSet(const char *set[], int ssize)
: Prints a set of strings.
- Main Function:
- Ensures that the input sets are valid (unique elements).
- Calculates and prints set difference (
A - B
,B - A
) and symmetric difference (A △ B
) using thesymmdiff
function.
Source code in the c programming language
#include <stdio.h>
#include <string.h>
const char *A[] = { "John", "Serena", "Bob", "Mary", "Serena" };
const char *B[] = { "Jim", "Mary", "John", "Jim", "Bob" };
#define LEN(x) sizeof(x)/sizeof(x[0])
/* null duplicate items */
void uniq(const char *x[], int len)
{
int i, j;
for (i = 0; i < len; i++)
for (j = i + 1; j < len; j++)
if (x[j] && x[i] && !strcmp(x[i], x[j])) x[j] = 0;
}
int in_set(const char *const x[], int len, const char *match)
{
int i;
for (i = 0; i < len; i++)
if (x[i] && !strcmp(x[i], match))
return 1;
return 0;
}
/* x - y */
void show_diff(const char *const x[], int lenx, const char *const y[], int leny)
{
int i;
for (i = 0; i < lenx; i++)
if (x[i] && !in_set(y, leny, x[i]))
printf(" %s\n", x[i]);
}
/* X ^ Y */
void show_sym_diff(const char *const x[], int lenx, const char *const y[], int leny)
{
show_diff(x, lenx, y, leny);
show_diff(y, leny, x, lenx);
}
int main()
{
uniq(A, LEN(A));
uniq(B, LEN(B));
printf("A \\ B:\n"); show_diff(A, LEN(A), B, LEN(B));
printf("\nB \\ A:\n"); show_diff(B, LEN(B), A, LEN(A));
printf("\nA ^ B:\n"); show_sym_diff(A, LEN(A), B, LEN(B));
return 0;
}
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char *mary="Mary";
const char *bob="Bob";
const char *jim="Jim";
const char *john="John";
const char *serena="Serena";
const char *setA[] = {john,bob,mary,serena};
const char *setB[] = {jim,mary,john,bob};
#define XSET(j) j, (sizeof(j)/sizeof(*j))
#define TALLOC(n,typ) malloc(n*sizeof(typ))
typedef enum {
esdDIFFERENCE,
esdSYMMETRIC } EsdFunction;
/** * * * * * * * * * * * * * * * * * * * *
* return value is difference or symmetric difference set
* its size is returned in sym_size
* f determinse whether it is a symmetric difference, or normal difference
* * * * * * * * * * * * * * * * * * * * **/
const char ** symmdiff( int *sym_size, EsdFunction f, const char *setA[], int setAsize, const char *setB[], int setBsize)
{
int union_size;
int max_union_size;
int diff_size;
const char **union_set;
const char **diff_set;
int *union_xor;
int ix, ixu;
max_union_size = setAsize + setBsize;
union_set = TALLOC(max_union_size, const char *);
union_xor = TALLOC(max_union_size, int);
/* I'm assuming here that setA has no duplicates,
* i.e. is a set in mathematical sense */
for (ix=0; ix<setAsize; ix++) {
union_set[ix] = setA[ix];
union_xor[ix] = 1;
}
diff_size = union_size = setAsize;
for (ix=0; ix<setBsize; ix++) {
for (ixu=0; ixu<union_size; ixu++) {
if (union_set[ixu] == setB[ix]) break;
}
if (ixu < union_size) { /* already in union */
union_xor[ixu] = 1-union_xor[ixu];
diff_size--;
}
else { /* not already in union -add */
if (f == esdSYMMETRIC) {
union_set[ixu] = setB[ix];
union_xor[ixu] = 1;
union_size++;
diff_size++;
}
}
}
/* Put results in symdiff set */
diff_set = TALLOC(diff_size, const char *);
ix = 0;
for (ixu=0; ixu<union_size; ixu++) {
if (union_xor[ixu]) {
if (ix == diff_size) {
printf("Short of space in diff_set\n");
exit(1);
}
diff_set[ix] = union_set[ixu];
ix++;
}
}
*sym_size = diff_size;
free(union_xor);
free(union_set);
return diff_set;
}
/* isSet tests that elements of list are unique, that is, that the list is a
* mathematical set. The uniqueness test implemented here is strcmp. */
int isSet(const char *list[], int lsize)
{
int i, j;
const char *e;
if (lsize == 0) {
return 1;
}
for (i = lsize-1; i>0; i--) {
e = list[i];
for (j = i-1; j>=0; j--) {
if (strcmp(list[j], e) == 0) {
return 0;
}
}
}
return 1;
}
void printSet (const char *set[], int ssize)
{
int ix;
printf(" = {");
for (ix=0;ix<ssize; ix++) {
printf( "%s ", set[ix]);
}
printf("}\n");
}
int main()
{
const char **symset;
int sysize;
/* Validate precondition stated by task, that inputs are sets. */
assert(isSet(XSET(setA)));
assert(isSet(XSET(setB)));
printf ("A symmdiff B");
symset = symmdiff( &sysize, esdSYMMETRIC, XSET(setA), XSET(setB));
printSet(symset, sysize);
free(symset);
printf ("A - B");
symset = symmdiff( &sysize, esdDIFFERENCE, XSET(setA), XSET(setB));
printSet(symset, sysize);
printf ("B - A");
symset = symmdiff( &sysize, esdDIFFERENCE, XSET(setB), XSET(setA));
printSet(symset, sysize);
free(symset);
return 0;
}
You may also check:How to resolve the algorithm Higher-order functions step by step in the Klingphix programming language
You may also check:How to resolve the algorithm Random number generator (device) step by step in the Phix programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the AWK programming language
You may also check:How to resolve the algorithm FTP step by step in the Kotlin programming language
You may also check:How to resolve the algorithm SOAP step by step in the F# programming language