array merge

Hi,
I was trying to write a program that takes two integer arrays that are already sorted and merges them into one array. I have written the program and the result is almost correct. But the only thing that does not work is that in the first array the last integer is not carried in the end array. For example if the first array is (1, 2, 3) and the second array is (2, 4, 4, 5, 6), then I expect the end array to be
(1, 2, 2, 3, 4, 4, 5, 6), but instead I get (1, 2, 2, 4, 4, 5, 6).
I just cant find what the problem is. Can anybody help me with this?
I use Dev-Pascal if that matters in any way...
I would appreciate any advice...thank you in advance
Here is the code that I wrote:
*********
program Merge(input, output);

type
tIndex1 = 1..3;
tIndex2 = 1..5;
tEndIndex = 1..8;
tArray1 = array[tIndex1] of integer;
tArray2 = array[tIndex2] of integer;
tEndArray = array[tEndIndex] of integer;

const
LIM1 = 3;
LIM2 = 5;
LIM3 = 8;
firstArray : tArray1 = (1, 2, 3);
secondArray : tArray2 = (2, 4, 4, 5, 6);

var
endArray : tEndArray;
cursorF: tIndex1;
cursorS : tIndex2;
cursorG : tEndIndex;
item : integer;
Key : char;

begin
cursorF := 1;
cursorS := 1;
cursorG := 1;

while cursorG < LIM3 do
begin
cursorG := cursorG + 1;
if cursorF = LIM1 then
begin
item := secondArray[cursorS];
cursorS := cursorS +1
end
else
if cursorS = LIM2 then
begin
item := firstArray[cursorF];
cursorF := cursorF + 1
end
else
if firstArray[cursorF] <= secondArray[cursorS] then
{}
begin
item := firstArray[cursorF];
cursorF := cursorF + 1;
end
else
begin
item := secondArray[cursorS];
cursorS := cursorS + 1;
end;

endArray[cursorG] := item
end;
for cursorG := 2 to LIM3 do
writeln(endArray[cursorG]);
read(Key)
end.

Comments

  • : Hi,
    : I was trying to write a program that takes two integer arrays that are already sorted and merges them into one array. I have written the program and the result is almost correct. But the only thing that does not work is that in the first array the last integer is not carried in the end array. For example if the first array is (1, 2, 3) and the second array is (2, 4, 4, 5, 6), then I expect the end array to be
    : (1, 2, 2, 3, 4, 4, 5, 6), but instead I get (1, 2, 2, 4, 4, 5, 6).
    : I just cant find what the problem is. Can anybody help me with this?
    : I use Dev-Pascal if that matters in any way...
    : I would appreciate any advice...thank you in advance
    : Here is the code that I wrote:
    : *********
    : program Merge(input, output);
    :
    : type
    : tIndex1 = 1..3;
    : tIndex2 = 1..5;
    : tEndIndex = 1..8;
    : tArray1 = array[tIndex1] of integer;
    : tArray2 = array[tIndex2] of integer;
    : tEndArray = array[tEndIndex] of integer;
    :
    : const
    : LIM1 = 3;
    : LIM2 = 5;
    : LIM3 = 8;
    : firstArray : tArray1 = (1, 2, 3);
    : secondArray : tArray2 = (2, 4, 4, 5, 6);
    :
    : var
    : endArray : tEndArray;
    : cursorF: tIndex1;
    : cursorS : tIndex2;
    : cursorG : tEndIndex;
    : item : integer;
    : Key : char;
    :
    : begin
    : cursorF := 1;
    : cursorS := 1;
    : cursorG := 1;
    :
    : while cursorG < LIM3 do
    : begin
    : cursorG := cursorG + 1;
    : if cursorF = LIM1 then
    : begin
    : item := secondArray[cursorS];
    : cursorS := cursorS +1
    : end
    : else
    : if cursorS = LIM2 then
    : begin
    : item := firstArray[cursorF];
    : cursorF := cursorF + 1
    : end
    : else
    : if firstArray[cursorF] <= secondArray[cursorS] then
    : {}
    : begin
    : item := firstArray[cursorF];
    : cursorF := cursorF + 1;
    : end
    : else
    : begin
    : item := secondArray[cursorS];
    : cursorS := cursorS + 1;
    : end;
    :
    : endArray[cursorG] := item
    : end;
    : for cursorG := 2 to LIM3 do
    : writeln(endArray[cursorG]);
    : read(Key)
    : end.
    :
    You stop with adding the first array, if the CursorF is set at the last element. You should stop, if the CursorF is beyond the last element. This way it will also check the last element. The same goes for the CursorS.
  • >>>>
    You stop with adding the first array, if the CursorF is set at the last element. You should stop, if the CursorF is beyond the last element. This way it will also check the last element. The same goes for the CursorS.
    >>>>
    Thank you very much...it works now


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories