Kyle Oliva
Kyle Oliva

Reputation: 45

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.

Upvotes: 0

Views: 140

Answers (3)

MartynA
MartynA

Reputation: 30755

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;

Upvotes: 2

LU RD
LU RD

Reputation: 34947

You are making a mistake in your function GetSum.

The argument var inputArray : array of integer is an open array that is indexed from zero.

See Run-time errors:

201 Range check error

If you compiled your program with range checking on, then you can get this error in the following cases:

* An array was accessed with an index outside its declared range.
* Trying to assign a value to a variable outside its range (for instance an enumerated type).

Here you are using an index outside of the declared range.

Make your loop something like this:

for sumIndex := 0 to High(inputArray) do ...

That will make the function generic, independent of the array size.

Upvotes: 2

gammatester
gammatester

Reputation: 1141

In the function statement

function getSUM(var inputArray : array of integer) : integer;

the inputArray is on open array (see the FPC reference manual 14.4.5, Open array parameters). The index bounds are 0 to 4, better use low(inputArray) and high(inputArray). In your repeat loop you use inputArray[5], where index 5 is out of range.

Upvotes: 2

Related Questions