How to resolve the algorithm Symmetric difference step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Symmetric difference step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

redim shared as string Result(-1)   'represent our sets as strings;
                                'this'll do to illustrate the concept

sub sym( A() as string, B() as string )
    dim as integer ai, bi, ri
    dim as boolean add_it
    for ai = lbound(A) to ubound(A)
        add_it = true
        for bi = lbound(B) to ubound(B)
            if A(ai) = B(bi) then 
                add_it=false
                exit for    'if item is common to both lists, don't include it
            end if
        next bi
        if add_it then
            for ri = 0 to ubound(Result)
                if A(ai) = Result(ri) then 
                    add_it=false
                    exit for
                    'if item is already in the result, don't include it again
                end if
            next ri
        end if
        if add_it then
            redim preserve as string Result(0 to ubound(Result)+1)
            Result(ubound(Result)) = A(ai)
        end if

    next ai
end sub

dim as string A(0 to 3) = {"John", "Bob", "Mary", "Serena"}
dim as string B(0 to 4) = {"Jim", "Mary", "John", "Bob", "Jim"}
                             'contains a double to show code can handle it
                             

sym(A(), B())
sym(B(), A())

for i as uinteger = 0 to ubound(Result)
    print Result(i)
next i

  

You may also check:How to resolve the algorithm Sort three variables step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Soundex step by step in the BASIC programming language
You may also check:How to resolve the algorithm Wieferich primes step by step in the Ruby programming language
You may also check:How to resolve the algorithm Tau number step by step in the Cowgol programming language