# Merge 2 Arrays

Hi!

I'm a beginner in programming. And here I have a task, where I'm stuck.
The program should merge 2 arrays into 1.
E.g. array 1 contains 11, 14, 18, 80, 100 and array 2 contains 8, 11,11,17,22,30,55,70. The result should be 8, 11, 11, 11, 14, 17, 18, 22, 30, 55, 70, 80, 100.
For now its only printing the two array in order. Any ideas?
Thats what I have so far:

[code]

program merge (input, output);

const
GRENZE1 = 5;
GRENZE2 = 8;
GRENZE = 13; { GRENZE1 + GRENZE2 }
GRENZE1PLUS1 = 6;
GRENZE2PLUS1 = 9;
GRENZEPLUS1 = 14;
type tIndex1 = 1..GRENZE1;
tIndex2 = 1..GRENZE2;
tIndex = 1..GRENZE;
tIndex1Plus1 = 1..GRENZE1PLUS1;
tIndex2Plus1 = 1..GRENZE2PLUS1;
tIndexPlus1 = 1..GRENZEPLUS1;
tFeld1 = array [tIndex1] of integer;
tFeld2 = array [tIndex2] of integer;
tFeld = array [tIndex] of integer;
var Feld1 : tFeld1;
Feld2 : tFeld2;
Feld : tFeld;
i : tIndex1Plus1;
j : tIndex2Plus1;
k : tIndexPlus1;
p1 : integer;
p2 : integer;

begin
{ sortierte Felder einlesen }
writeln ('Bitte', GRENZE1:2, ' Werte des ersten Feldes ',
'sortiert eingeben!');
for i := 1 to GRENZE1 do
writeln ('Bitte', GRENZE2:2, ' Werte des zweiten Feldes ',
'sortiert eingeben!');
for j := 1 to GRENZE2 do

p1:=0;
p2:=0;
repeat
if (p1 < Grenze1) and (p2 < Grenze2) then
begin
if Feld1[p1] < Feld2[p2] then
begin
Feld[p1+p2]:=Feld1[p1];
inc(p1);
end else begin
Feld[p1+p2]:=Feld2[p2];
inc(p2);
end;
end else
if (p1=Grenze2) then
begin
Feld[p1+p2]:=Feld1[p1];
inc(p1);
end else
if (p1>=Grenze1) and (p2=Grenze1) and (p2>=Grenze2);

writeln ('Das Ergebnisfeld ist:');
for k := 1 to GRENZE do
write (Feld[k]:8);
writeln
end. { merge }

[/code]

• : Hi!
:
: I'm a beginner in programming. And here I have a task, where I'm stuck.
: The program should merge 2 arrays into 1.
: E.g. array 1 contains 11, 14, 18, 80, 100 and array 2 contains 8, 11,11,17,22,30,55,70. The result should be 8, 11, 11, 11, 14, 17, 18, 22, 30, 55, 70, 80, 100.
: For now its only printing the two array in order. Any ideas?
: Thats what I have so far:
:
: [code]
:
: program merge (input, output);
:
: const
: GRENZE1 = 5;
: GRENZE2 = 8;
: GRENZE = 13; { GRENZE1 + GRENZE2 }
: GRENZE1PLUS1 = 6;
: GRENZE2PLUS1 = 9;
: GRENZEPLUS1 = 14;
: type tIndex1 = 1..GRENZE1;
: tIndex2 = 1..GRENZE2;
: tIndex = 1..GRENZE;
: tIndex1Plus1 = 1..GRENZE1PLUS1;
: tIndex2Plus1 = 1..GRENZE2PLUS1;
: tIndexPlus1 = 1..GRENZEPLUS1;
: tFeld1 = array [tIndex1] of integer;
: tFeld2 = array [tIndex2] of integer;
: tFeld = array [tIndex] of integer;
: var Feld1 : tFeld1;
: Feld2 : tFeld2;
: Feld : tFeld;
: i : tIndex1Plus1;
: j : tIndex2Plus1;
: k : tIndexPlus1;
: p1 : integer;
: p2 : integer;
:
: begin
: { sortierte Felder einlesen }
: writeln ('Bitte', GRENZE1:2, ' Werte des ersten Feldes ',
: 'sortiert eingeben!');
: for i := 1 to GRENZE1 do
: writeln ('Bitte', GRENZE2:2, ' Werte des zweiten Feldes ',
: 'sortiert eingeben!');
: for j := 1 to GRENZE2 do
:
: p1:=0;
: p2:=0;
: repeat
: if (p1 < Grenze1) and (p2 < Grenze2) then
: begin
: if Feld1[p1] < Feld2[p2] then
: begin
: Feld[p1+p2]:=Feld1[p1];
: inc(p1);
: end else begin
: Feld[p1+p2]:=Feld2[p2];
: inc(p2);
: end;
: end else
: if (p1=Grenze2) then
: begin
: Feld[p1+p2]:=Feld1[p1];
: inc(p1);
: end else
: if (p1>=Grenze1) and (p2=Grenze1) and (p2>=Grenze2);
:
: writeln ('Das Ergebnisfeld ist:');
: for k := 1 to GRENZE do
: write (Feld[k]:8);
: writeln
: end. { merge }
:
: [/code]

This code is really confusing. Is there any reason it is setup this way?

[code]
program merge (input, output);
CONST
GRENZE1 = 5;
GRENZE2 = 8;

VAR
Feld1 : array [1..GRENZE1] of integer;
Feld2 : array [1..GRENZE1] of integer;
FeldT : array [1..GRENZE1+GRENZE2] of integer;
p1 : integer;
[/code]

This is much less confusing. No let's use the same code to fill the arrays, but call a procedure to sort them:

[code]
PROCEDURE SortArrays;
VAR
x, y : integer;
temp : integer;
Begin
{ Put both Arrays into one array }
For X := 1 to GRENZE1 Do FeldT[X] := Feld1[X];
For X := 1 to GRENZE2 Do FeldT[X+GRENZE1] := Feld2[X];

{ Use a ?bubble? Sort to arrange them }
For X := 1 to GRENZE1+GRENZE2-1 Do
For Y := 1 to GRENZE1+GRENZE2-1 Do
If FeldT[Y] > FeldT[Y+1] Then
Begin
Temp := FeldT[Y];
FeldT[Y] := FeldT[Y+1];
FeldT[Y+1] := Temp;
End;
End;

begin
{ sortierte Felder einlesen }
writeln ('Bitte', GRENZE1:2, ' Werte des ersten Feldes ',
'sortiert eingeben!');
for p1 := 1 to GRENZE1 do

writeln ('Bitte', GRENZE2:2, ' Werte des zweiten Feldes ',
'sortiert eingeben!');
for p1 := 1 to GRENZE2 do

SortArrays;
writeln ('Das Ergebnisfeld ist:');
for p1 := 1 to GRENZE1+GRENZE2 do
write (FeldT[p1]:8);
writeln
end. { merge }
[/code]