Results 1 to 6 of 6

Thread: Array Problem!!! Won't call getarraylength

  1. #1
    Join Date
    Oct 2006
    Location
    I'm a figment of your imagination
    Posts
    422
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Array Problem!!! Won't call getarraylength

    SCAR Code:
    program CrackCodes;

    var
      PrimeArray : array [0..1] of longint;
      TestNumber : longint;
      Z          : longint;
    procedure Initscript;
    begin
      PrimeArray[0]:= 2; //Array needs a start value (start prime #);
      PrimeArray[1]:=3; //W00t! see above.
      TestNumber:= 3; //Ini-value, just for fun :).
      Writeln('Starting Script...');
    end;

    function GetPrimeNumber(testnumber : longint) : longint;
    var
      i, maxlength  : longint;
      isPrimeNumber : boolean;
    begin
      Maxlength:= getarraylength(PrimeArray);
      i:= 0; //Give i an initial start value
      isPrimeNumber:= true; //Just assume it is one
      repeat
      if (Testnumber mod PrimeArray[i] = 0) then
      begin
        isPrimeNumber:= false;
        result:= -1;
      end else
          result:= TestNumber;
      i:= i+1;
      until((i = maxlength)or(isPrimeNumber = false));
    end;

    procedure AddPrimeToArray(prime : longint);
    begin
      setarraylength(PrimeArray, getarraylength(PrimeArray)+1);
      PrimeArray[getarraylength(PrimeArray)]:= prime;
    end;

    begin
      InitScript;
      repeat
      z:= GetPrimeNumber(TestNumber);
      If not(z=-1) then
      begin
        AddPrimeToArray(z);
        writeln(inttostr(z));
      end;
      until(false);
       TestNumber:=Testnumber+1
    end.

    I get this error:

    [Runtime Error] : Could not call proc in line 20 in script [removed file path for privacy reasons, no includes so it IS in the file I posted]

    Line 20 reads:
    Code:
    Maxlength:= getarraylength(PrimeArray);
    Basically, the script is supposed to generate prime numbers. Eventually it will run a type-error because the datarange of a longint isn't that high, but right now I'm not caring 'bout all the other errors (some are quite obvious). Someone gimme a hand with the runtime error here please? I need help within the next 48 hrs or I'm gonna drop this project *holds abort button in his hand*.

    It's been a while... but I'm BACK!!!

  2. #2
    Join Date
    Sep 2006
    Posts
    5,219
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    I had problems with arrays too. I forgot how I solved them though. This may help you figure it out.

    SCAR Code:
    program primefinder2;

    var
    numberofprimes, maxprime, currentprime, currentnumber : integer;
    prime: array of integer;

    begin
      cleardebug;
      currentnumber := 5;
      numberofprimes := 3;
      maxprime := 1;
      setarraylength(prime,4);
      prime[1] := 2;
      prime[2] := 3;
      prime[3] := 5;
      repeat
        currentnumber := currentnumber +2;
        if(((prime[maxprime+1])*(prime[maxprime+1]))<=currentnumber)then
          maxprime := maxprime+1;
        currentprime :=1;
      repeat
        if (currentprime<=maxprime) then
          if ( not((currentnumber mod prime[currentprime])= 0))then
            currentprime := currentprime+1;
      until (((maxprime+1=currentprime)) or ((currentnumber mod prime[currentprime])=0));
      if(not((currentnumber mod prime[currentprime])=0)) then
        begin
        numberofprimes :=numberofprimes+1
        setarraylength(prime, numberofprimes+1)
        prime[numberofprimes]:= currentnumber
        cleardebug
        writeln(inttostr(currentnumber)+'        '+inttostr(numberofprimes))
        wait(10);
        end;
       until (currentnumber>=100); //can be currentnumber or numberofprimes >= something
    end.

  3. #3
    Join Date
    Feb 2006
    Location
    New Zealand
    Posts
    1,330
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Its because if you delclare the array length when you declare the variable there is no need to use getarraylength for example.

    SCAR Code:
    var
      array:array[1..2] of integer;

    Why would you need to call getarraylength for that? you already know that it can only be 2. As you set it.

    Getarraylength can only be used it you set the length with setarraylength.

  4. #4
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    You'll have to do

    A : Array of integer;, without the [0..1]

    Then call

    SetArrayLength(A, 2);



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  5. #5
    Join Date
    Oct 2006
    Location
    I'm a figment of your imagination
    Posts
    422
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Pyro View Post
    Its because if you delclare the array length when you declare the variable there is no need to use getarraylength for example.

    SCAR Code:
    var
      array:array[1..2] of integer;

    Why would you need to call getarraylength for that? you already know that it can only be 2. As you set it.

    Getarraylength can only be used it you set the length with setarraylength.
    The first time it runs it, it obviously doesn't need to find it, but it will increase the array by one every single time it runs it and thus eventually, IT DOESNT know the arraylength. And I didn't want to bother setting a variable to count the number of items in the array.

    Anyhow, thanks for all your support! I really appreciate it! This was actually supposed to be just a fun little script, but anyways... THANKS A LOT YOU ALL!!

    It's been a while... but I'm BACK!!!

  6. #6
    Join Date
    Feb 2006
    Location
    New Zealand
    Posts
    1,330
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Well if your using setarraylength then it should be able to use getarraylength

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem with array
    By marpis in forum C/C++ Help and Tutorials
    Replies: 1
    Last Post: 12-05-2008, 09:10 PM
  2. function GetLength- similar to Length and GetArrayLength
    By Cazax in forum Research & Development Lounge
    Replies: 14
    Last Post: 03-17-2008, 07:19 PM
  3. Problem with array...
    By Dude in forum OSR Help
    Replies: 7
    Last Post: 08-24-2007, 11:48 AM
  4. array problem
    By endr1x in forum OSR Help
    Replies: 0
    Last Post: 01-27-2007, 11:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •