Kyle Oliva Kyle Oliva - 6 months ago 88
Pascal Question

Why am I still getting the exitcode 201?

I did some searching saying it's about storing a value where it doesn't fit but I don't how it happened in my code. I checked my code a bunch of times but I'm still getting this. I did some searching saying it's about storing a value where it doesn't fit but I don't how it happened in my code. I checked my code a bunch of times but I'm still getting this.

program ACT2;

uses crt;

var
inputs : array[1..5] of integer;
index1 : integer;
choice : char;
toPrint : integer;

function getSUM(var inputArray : array of integer) : integer;
var
SUM, sumIndex : integer;

begin
SUM := 0;
sumIndex := 1;

repeat
SUM := SUM + inputArray[sumIndex];
sumIndex := sumIndex + 1;
until (sumIndex > 5);

getSUM := SUM;
end;


begin

clrscr;

for index1 := 1 to 5 do
begin
write('Input Integer[', index1); write(']: ');
readln(inputs[index1]);
end;

clrscr;

write('Integers:');

for index1 := 1 to 5 do
begin
write(' ', inputs[index1]);
end;

writeln(''); writeln(''); writeln('');

writeln('[1] SUM');
writeln('[2] AVERAGE');

writeln('');

write('INPUT: ');

readln(choice);


if(choice = '1') then
toPrint := getSUM(inputs);

writeln(toPrint);
readkey;
end.

Answer Source

The other two answers have correctly identified the cause of your problem, which is that you are using two different array types with different bases, 1 versus 0.

However, you could have avoided this problem in the first place by declaring your own integer array type and then using it consistently, as in

type
  TInputArray = array[1..5] of Integer;
var
    inputs : TInputArray; //array[1..5] of integer;
    index1 : integer;
    choice : char;
    toPrint : integer;

//function getSUM(var inputArray : array of integer) : integer;
function getSUM(var inputArray : TInputArray) : integer;
var
    SUM, sumIndex : integer;

begin

    SUM := 0;
    sumIndex := Low(inputArray);

    repeat
      SUM := SUM + inputArray[sumIndex];
      sumIndex := sumIndex + 1;
    until (sumIndex > High(inputArray));

    getSUM := SUM;
end;

Notice that the integer 5 only occurs once in this version of the program, and the use of the standard Low and High functions means that there is only one place that 5 needs to be changed if you want to alter the size of the array. In fact, it might be better to define the number of elements as a constant and use the constant in the array declaration, as in

const NumOfElements = 5;
type
  TInputArray = array[1..NumOfElements] of Integer;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download