Anonymous Anonymous - 23 days ago
90 0

No description

Pascal

Feldverarbeitung

PROGRAM FeldVerarbeitung;

FUNCTION IsElementOf(VAR element : INTEGER; VAR a : ARRAY OF INTEGER) : BOOLEAN;

VAR i : INTEGER;

BEGIN

  IsElementOf := FALSE;

  FOR i := Low(a) TO High(a) DO BEGIN
    IF (element = a[i]) THEN BEGIN
      IsElementOf := TRUE;
      BREAK;
    END;
  END;
END;

PROCEDURE Merge(a1, a2: ARRAY OF INTEGER; VAR a3: ARRAY OF INTEGER; VAR n3: INTEGER);

VAR i,n : INTEGER;
    maxLength : INTEGER;
    minLength : INTEGER;

BEGIN
maxLength := High(a1);

IF High(a2) > maxLength THEN
  maxLength := High(a2);

  minLength := Low(a1);

IF Low(a2) < minLength THEN
  minLength := Low(a2);

n := 0;

FOR i := minLength TO maxLength DO BEGIN
    IF(i >= Low(a1)) AND (i <= High(a1)) THEN BEGIN
      IF NOT(IsElementOf(a1[i], a2)) THEN BEGIN
        a3[n] := a1[i];
        n := n + 1; 
      END;
    END;

    IF(i >= Low(a2)) AND (i <= High(a2)) THEN BEGIN
      IF NOT(IsElementOf(a2[i], a1)) THEN BEGIN
        a3[n] := a2[i];
        n := n + 1; 
      END;
    END;
    
END;

n3 := n;

END;

PROCEDURE PrintArray(a: ARRAY OF INTEGER; printLength : INTEGER);

VAR i : INTEGER;
BEGIN
    FOR i := Low(a) TO printLength DO BEGIN
        Write('  ', a[i]);
    END;
    WriteLn();
END;

VAR a1 : ARRAY [0..5] OF INTEGER;
    a2 : ARRAY [0..3] OF INTEGER;
    a3 : ARRAY [0..10] OF INTEGER;
    n3 : INTEGER;
BEGIN

a1[0] := 2;
a1[1] := 4;
a1[2] := 4;
a1[3] := 10;
a1[4] := 15;
a1[5] := 15;

a2[0] := 3;
a2[1] := 4;
a2[2] := 5;
a2[3] := 10;

PrintArray(a1, 5);
PrintArray(a2, 3);

Merge(a1,a2, a3, n3);

PrintArray(a3, n3 - 1);

END.
Comments