Help with a program/arrays? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help with a program/arrays?

glaciesglacies Posts: 2Member
Hey,
I have to write a program for school which calculates the fibonacci sequence with two inputted integers, but I have to use an array and procedures in order to do it.

I think my problem is probably that I don't understand arrays properly as we've recently done them, but I can't find what's wrong with my code. It runs and the inputs/close programs work okay but it doesn't do the sequences - it just comes up with a large number every time that's like 4 hundred thousand something.

Does anybody have any idea what I could be doing wrong? Here is my code:
[link=http://pastebin.com/EfTKhLCv]http://pastebin.com/EfTKhLCv[/link]

Thanks :)

Comments

  • quikcarlxquikcarlx Hollywood, FlPosts: 26Member
    I did what I could to get your program running. I commented out lines that either are not needed or need to be changed. I tried a few numbers and it seems to do the job.
    [code]program ex27;

    uses
    sysutils;
    var
    numbers:array [1..2] of integer;
    loopcounter, loopcounter1, i, loopcounter2, startat, answer, dummy : integer;
    valid: boolean;
    input : string;

    procedure getinput; //WORKS
    begin
    //initialise
    for i := 1 to 2 do
    numbers[i]:= 0;

    for loopcounter1:=1 to 2 do
    begin
    repeat
    write('Please enter the number: ');
    readln(input);
    valid:=true;

    if length(input)=0 then
    valid:=false
    else
    begin
    if input[1]='-' then
    startat:=2
    else
    startat:=1;

    for loopcounter2:=startat to length(input) do
    begin
    if not (input[loopcounter2] in ['0'..'9']) then
    valid:=false;
    break;
    end;
    end;
    if not valid then
    writeln('this is not a valid integer.');
    until valid;
    numbers[loopcounter1]:=strtoint(input);
    end;
    end;
    (* procedure calculatefibonacci(numbers: array of integer); *)
    procedure calculatefibonacci;
    begin
    (* for loopcounter:=1 to 2 do
    dummy:=numbers[1]+numbers[2];
    write(numbers[2]);
    begin *)
    answer:=numbers[1]+numbers[2];
    writeln(answer);
    writeln;
    (* end; *)
    end;
    procedure closeprogram;
    begin
    write('Press enter to close');
    readln;
    end;

    begin
    getinput;
    (* calculatefibonacci(numbers); *)
    calculatefibonacci;
    closeprogram;
    end.[/code]
Sign In or Register to comment.