Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with a program/arrays? Posted by glacies on 22 Nov 2012 at 7:38 AM
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:
http://pastebin.com/EfTKhLCv

Thanks :)
Report
Re: Help with a program/arrays? Posted by quikcarlx on 29 Nov 2012 at 1:52 PM
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.
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.




 

Recent Jobs