Print

Print


On Fri, 25 Jul 2008, Lloyd Jefferson wrote:

> Write a program that will read in two arrays of integers representing the
> elements of two sets from two different user-specified input files, and
> calculate both the union and the intersection of the two sets. Use arrays to
> contain the input sets, and also to build both the union and the intersection.
> Note that the input sets may not be sorted in order, so your algorithm must
> work regardless of the order in which set elements are entered.
>
> Test your program on tww files inputA.dat and inputB.dat, containing the
> following two sets
>
> inputA=0, 1, -3, 5, -11, 6, 8, 11, 17, 15
> inputB=0, -1, 3, 7, -6, 16, 5, 12, 21

What about the following, simple but straight forward solution?

################################################################
    Program U_I
!
!  Determine union and interdection of two sets of integers.
!                                      Roland Schilling,  26-07-08
!
    integer        :: A(10)=[0, 1, -3, 5, -11, 6, 8, 11, 17, 15]
    integer        :: B(9) =[0, -1, 3, 7, -6, 16, 5, 12, 21]
    integer        :: AuB(20), AiB(10), i, k, nu, ni
!
    nu=size(A)
    ni=0
    AuB(1:nu)=A
    do k=1,size(B)
      if (any(B(k) == A)) then
        ni=ni+1
        AiB(ni)=B(k)
      else
        nu=nu+1
        AuB(nu)=B(k)
      end if
    end do
    write(*,*) AuB(1:nu)
    write(*,*) AiB(1:ni)
    end
#############################################################

Hope that helps,
Roland


+----------------------------------------------------------------------+
|  Roland Schilling                      Home-Office: +49(89)32929-670 |
|  Max-Planck-Institut für Gravitationsphysik                          |
|  -- Albert-Einstein-Institut --                                      |
|  D-85748 Garching                           E-mail:   [log in to unmask] |
|  Germany                           http://www.geo600.uni-hannover.de |
+----------------------------------------------------------------------+
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html