1

This program is supposed to sort an array and fill it with random integers from 1 to 1000. It needs to find the maximum value and print the array. It should print the array in two ways: ARRAY OF INTEGER and ArrayType. I need help to print this array in ArrayType. I am not super familiar with Pascal and my assignment is to print this array with ArrayType as a parameter. This is my code I have so far:

PROGRAM SortingAlgorithm;

Var intArray : array[1..20] OF INTEGER;
Var i, j, index : INTEGER;

PROCEDURE Sort(intArray : ARRAY OF INTEGER; size : INTEGER);
BEGIN
   FOR i := 2 to 20 do
   BEGIN
      index := intArray[i];
      j := i;
      WHILE ((j > 1) AND (intArray[j-1] > index)) do
      BEGIN
         intArray[j] :=intArray[j-1];
         j := j - 1;
      END;
      intArray[j] := index;
   END;
END;

PROCEDURE fillArray(var a,b : integer);
VAR temp : INTEGER;
BEGIN
    temp := a;
    a := b;
    b := temp;
END;
BEGIN
  FOR i := 1 to 20 do
    intArray[i] := i;
  FOR i := 1 to 20 do
    fillArray(intArray[i],intArray[random(20)+1]);
END.

PROCEDURE findMax(intArray : ARRAY OF INTEGER; size : INTEGER);
VAR max : INTEGER;
begin
     max := 1;
     FOR i := 2 to size do
     BEGIN
         IF intArray[i] > intArray[max] THEN
         max:=i;
     END;
END;

PROCEDURE printArray(intArray : ARRAY OF INTEGER; size : INTEGER);
BEGIN
    FOR i := 1 to 20 do
        Writeln(intArray[i]);
END;

PROCEDURE printArray2();
BEGIN

END;


BEGIN
    fillArray (intArray);
    printArray (intArray);
    printArray2 (intArray);
    largestValue := findMax (intArray);
    WriteLn (‘The largest value is ‘, largestValue);
    sort (intArray);
    printArray (intArray);
END.

printArray2 is for the ArrayType parameter. This is all the information I was given for this program, I'm not sure what printing with a parameter of ArrayType means. Any help is appreciated. Thanks.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • There are many problems here. You're trying to pass the array as a parameter, which is sensible, but your functions don't expect an array, they expect a size. Your fillArray is confusing. If you're just trying to get 20 integers between 1 and 1000, then why check for duplicates? And if you're going to sort anyway, you don't need findMax. Just sort the array and take the last element. Printing the array is easy, `for i := 1 to size do writeln( intArray[i] );`. I don't know what you mean by `ArrayType`. Which Pascal is this? There are many different versions. – Tim Roberts Feb 13 '21 at 06:21
  • To comment on your actual question: *I'm not sure what printing with a parameter of `ArrayType` means*. Same here, I don't get what the meaning might be. I suggest you ask who ever gave you the assignment, to clarify. – Tom Brunberg Feb 13 '21 at 16:33
  • 1
    ...unless, the idea is that you first define a new type: `type ArrayType = array[1..20] of integer` and then replace the current occurances of `ARRAY OF INTEGER` with the new definiftion `ArrayType`. ... Does it make sense, considering what the courseware has been about lately? – Tom Brunberg Feb 13 '21 at 16:41

0 Answers0