Recent

Author Topic: Problem with Arrays  (Read 4095 times)

IAmTheLaw

  • Newbie
  • Posts: 4
Problem with Arrays
« on: April 24, 2014, 03:30:29 am »
Hello, i need help with a simple program about arrays, my goal is to obtain an arrangement like a matrix, but im having problems with the jump line, because i only get the result in one line, this is what i've got:

program project1;

uses
  crt;

const
  L=5;

var
  T: array[1..L,1..L] of real;
  n: real;
  i,j: integer;

begin
  write('write a number ');readln(n);
  begin
  For i:=1 to L do
   begin
    For j:=1 to L do
     begin
      T[i,j]:= n;
      write(i,',',j,' ',T[i,j]*i*j:0:2,' ');
     end;
   end;
  end;
  readln;
end.

but if i add a 'writeln' between the 'for' cicles it only shows me the las line of the array (5,1 to 5,5) but does not show me the other and i have no idea why it does do this, anyone can help me?

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: Problem with Arrays
« Reply #1 on: April 24, 2014, 04:42:15 am »
Did you try to add WriteLn after the inner loop? Like this:
Code: [Select]
program project1;

uses
  crt;

const
  L=5;

var
  T: array[1..L,1..L] of real;
  n: real;
  i,j: integer;

begin
  write('write a number ');readln(n);
  begin
  For i:=1 to L do
   begin
    For j:=1 to L do
     begin
      T[i,j]:= n;
      write(i,',',j,' ',T[i,j]*i*j:0:2,' ');
     end;
    WriteLn;  //<---- This?
   end;
  end;
  readln;
end.

Tomas Hajny

  • Moderator
  • New Member
  • *****
  • Posts: 36
Re: Problem with Arrays
« Reply #2 on: April 24, 2014, 08:58:56 am »
If the assumption mentioned in the previous post is correct (i.e. you tried adding WriteLn at the suggested place and it didn't work as expected), it might be useful to mention your platform and used compiler version in order to check if you didn't encounter some bug specific to a certain platform specific implementation of unit Crt (possibly also related to a particular older version, although I'm not immediately aware of a similar bug fixed in previous versions).

IAmTheLaw

  • Newbie
  • Posts: 4
Re: Problem with Arrays
« Reply #3 on: April 24, 2014, 04:07:10 pm »
If the assumption mentioned in the previous post is correct (i.e. you tried adding WriteLn at the suggested place and it didn't work as expected), it might be useful to mention your platform and used compiler version in order to check if you didn't encounter some bug specific to a certain platform specific implementation of unit Crt (possibly also related to a particular older version, although I'm not immediately aware of a similar bug fixed in previous versions).

I've tried what engkin said but its the same, it only shows the last line. I'm using the lazarus v1.2.2 on windows 8.1, i add a image, hope that help.

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: Problem with Arrays
« Reply #4 on: April 24, 2014, 04:30:02 pm »
Output of engkin's version is fine here in Linux console (xterm). I have no Win, only Wine and I cannot make it work in cmd.exe, don't know why.
Lazarus 2.1.0 r61214:62238 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: Problem with Arrays
« Reply #5 on: April 24, 2014, 04:44:10 pm »
Could you try to copy the output of your console to a text file? Something like:
Get the console menu (Alt+Space?) then Edit then Select All. Copy and paste it on a text file. You might see the missing lines.

Tomas Hajny

  • Moderator
  • New Member
  • *****
  • Posts: 36
Re: Problem with Arrays
« Reply #6 on: April 24, 2014, 05:53:20 pm »
Alright, some more ideas:

1) Do you get the same behaviour if you start the created EXE file from outside of Lazarus (preferably from cmd.exe)?

2) I don't use Lazarus, but I believe that it might default to creating GUI applications by default. I assume that you selected a console type application, right? If you are not sure, could you please add an explicit "{$APPTYPE CONSOLE}" just after "program project1;"? Under Win 7, the application compiled to GUI mode would actually hang, but I'm not 100% sure if Win 8.1 might not behave differently.

IAmTheLaw

  • Newbie
  • Posts: 4
Re: Problem with Arrays
« Reply #7 on: April 29, 2014, 04:41:50 pm »
So i finally found the solution, the 'writeln' has to be below the second 'for' cicle, like this

For i:=1 to L do
    For j:=1 to L do
      T[i,j]:= n*i*j;
    writeln;
  end;

 i don't know why does not work with the first form but whatever, it works; thank you all for your help.