D
D
Denis D.2014-11-08 20:44:13
Delphi
Denis D., 2014-11-08 20:44:13

How to insert elements of an unsorted array into a sorted one in Pascal?

I am a student. For 5 weeks I have not been able to pass the Delphi lab due to constantly popping up errors.
The essence of the problem:
There is an array of integers A(n+m) sorted in descending order and an unsorted array B(m), n<=300, m<=200. You need to write a program that inserts the elements of array B into array A so that A remains ordered.

I already have the code, but it works unpredictably: sometimes it works, sometimes it doesn't.
My code (with regular arrays):

program Lab5Console;

{$APPTYPE CONSOLE}

uses
  SysUtils;

const
  N_const = 300;
  M_const = 200;

var
  A: array[1..N_const+M_const] of integer;
  B: array[1..M_const] of integer;
  n,m,i,j,last_greater: integer;

procedure sortA(len: integer);
var i,j,temp: integer;
begin
  temp:=0;
  for i := len+1 downto 1 do begin
    for j:=0 to i-1 do
      if A[j] < A[j+1] then begin
        temp:=A[j];
        A[j]:=a[j+1];
        A[j+1]:=temp;
      end;
  end;
end;

procedure writeArray(Arr:array of integer; len:integer);
begin
  for i := 0 to len-1 do begin
    write(Arr[i]:4);
    if (i mod 15 = 0) and not (i = 0) then writeln;
  end;
end;

procedure insertIntoA(num, id, len: integer);
var i: integer;
begin
   for i := len downto id do
        A[i+1] := A[i];
   A[id] := num;
end;

begin
  try
     writeln('Hello! Its Lab5Console.');
     writeln('--- --- ---');

     repeat
        write('Insert N: '); readln(n);
     until n<300;
     repeat
        write('Insert M: '); readln(m);
     until m<200;

     writeln('Array A:');
     i:=1;
     for i:=1 to n do begin
       A[i]:=random(10);
     end;
     sortA(n);
     writeArray(A,n);

     writeln; writeln('Array B:');
     i:=1;
     for i:=1 to m do begin
       B[i]:=random(10);
     end;
     writeArray(B, m);

     for i := 1 to m do begin
       last_greater := 1;
       for j := 1 to n do begin
         if A[j] >= B[i] then
           last_greater := j+1;
       end;
       insertIntoA(B[i], last_greater, n+1);
     end;
     writeln; writeln('Result array:');
     writeArray(A,n+m);

     readln; readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Code with dynamic arrays:
program Lab5Console2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  DArr = array of integer;

var
  A,B: DArr;
  n,m,i,j,last_greater: integer;

procedure WriteArr(Arr: DArr);
begin
   for i := 0 to length(Arr)-1 do begin
     write(Arr[i]:4);
     if (i mod 15 = 0) and not (i=0) then writeln;
   end;
   writeln;
end;

function BubbleSort(Arr: DArr): DArr;
var i,j,temp: integer;
begin
  for i := 0 to length(Arr)-2 do
    for j := 0 to length(Arr)-i-1 do begin
      if Arr[j] < Arr[j+1] then begin
        temp    := Arr[j];
        Arr[j]  := Arr[j+1];
        Arr[j+1]:= temp;
      end;
    end;
    BubbleSort:= Arr;
end;

function InsertAfter(Arr: DArr; number, id: integer): DArr;
var i: integer;
begin
  for i := length(Arr)-1 downto id do
    Arr[i+1]:= Arr[i];
  Arr[id]:= number;
  InsertAfter:= Arr;
end;

begin
  try
    writeln('Hello! Its Lab5Console.');
    writeln('--- --- ---');

    // Enter N and M - arrays length
    repeat
      write('Insert N: '); readln(n);
    until n<300;
    SetLength(A,n);
    repeat
      write('Insert M: '); readln(m);
    until m<200;
    SetLength(B, m);

    // Generate A
    writeln('Array A:');
    for i := 0 to length(A)-1 do
      A[i]:=random(10);
    WriteArr(A);

    writeln('Array A after sort:');
    A:= BubbleSort(A); // Sort A
    WriteArr(A);

    // Generate B
    writeln('Array B:');
    for i := 0 to length(B)-1 do
      B[i]:=random(10);
    WriteArr(B);

    SetLength(A, n+m);

    for i := 0 to m-1 do begin
      last_greater:=0;
      for j := 0 to n-1 do
        if A[j] >= B[i] then last_greater:= j+1;
      A:= InsertAfter(A, B[i], last_greater);
    end;

    write('Result array:');
    WriteArr(A);

    readln;
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Help write a working program, or point out an error in the code. Which code is better, closer to the truth?
I will be grateful!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2014-11-08
@denysd

You are confused with the beginning and end of arrays.
In pascal, it is customary to number the elements of arrays from 1. SetLength creates an array that is numbered from 0.
You have a loop where you read from 0 - check whether you always specify the lower bound of the array correctly.
For universality, you need to use the High and Low functions, which indicate the lower and upper elements of the array:

For i := Low(Arr) to High(Arr) do
Begin
    write(Arr[i]:4);
End;

Further, of course, there are many more comments on the style and code.
For example, the assignment says that n<=300, and you are checking for n<300. There are places where you over-initialize variables.
In general, the task is not just for programming, but for algorithms.
Your method is working, but not very optimal. For example, to solve the problem, it is enough to simply assign array B to the end of array A, and then sort everything.
The task in this formulation, as it were, hints at the study of the merge sort algorithm.

J
jcmvbkbc, 2014-11-08
@jcmvbkbc

I haven't been able to pass the Delphi lab for 5 weeks now because of constantly popping up errors.

Means no luck.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question