[SOLVED]Why isn't my array working? lazarus / 16-Databases ---------------------------------------------------------------------- svy249 · 2017-12-21 17:41 program Sp; uses crt; var i,k,j,bx,by,yy,xx,v,v2: integer; x: array [1..5] of Integer; y: array [1..5] of Integer; h: array [1..5] of Real; begin randomize; For i:=1 to 5 do         x:=random(40); For i:=1 to 5 do         y[j]:=random(40); end. ---------------------------------------------------------------------- molly · 2017-12-21 17:45 Always put source-code between code tags. What exactly is not working for you ? IT compiles fine, although i'm unsure why you used j as index for array y  edit: emphasis on using code-tags, ---------------------------------------------------------------------- Thaddy · 2017-12-21 17:45 x..... ---------------------------------------------------------------------- Thaddy · 2017-12-21 17:46 Quote from: Thaddy on December 21, 2017, 06:45:50 pmx..... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$ifdef fpc}{$mode objfpc}{$H+}{$I-}{$endif}uses crt;var i,k,j,bx,by,yy,xx,v,v2: integer;x: array [1..5] of Integer;y: array [1..5] of Integer;h: array [1..5] of Real;beginrandomize;For i:=1 to 5 do        x[i]:=random(40);  // there's your bugFor i:=1 to 5 do      y[j]:=random(40);end. ---------------------------------------------------------------------- Handoko · 2017-12-21 17:50 The TS should got a compile-time error, didn't he? Maybe he simply ignored that message. ---------------------------------------------------------------------- molly · 2017-12-21 17:56 Quote from: Handoko on December 21, 2017, 06:50:53 pmThe TS should got a compile-time error, didn't he? I'm very curious to know why one might think that. Seems i'm missing something obvious  Code: [Select]Compiling H:\lazarus-forums\39375\test.pas test.pas(13,11) Warning: Variable "j" does not seem to be initialized test.pas(4,7) Note: Local variable "k" not used test.pas(4,11) Note: Local variable "bx" not used test.pas(4,14) Note: Local variable "by" not used test.pas(4,17) Note: Local variable "yy" not used test.pas(4,20) Note: Local variable "xx" not used test.pas(4,23) Note: Local variable "v" not used test.pas(4,25) Note: Local variable "v2" not used test.pas(5,1) Note: Local variable "x" is assigned but never used test.pas(6,1) Note: Local variable "y" is assigned but never used test.pas(7,1) Note: Local variable "h" not used ? Oh... code-tags ? ---------------------------------------------------------------------- Thaddy · 2017-12-21 17:57 Quote from: Handoko on December 21, 2017, 06:50:53 pmThe TS should got a compile-time error, didn't he? Maybe he simply ignored that message. arraystest.pas(11,12) Error: Incompatible types: got "LongInt" expected "Array[1..5] Of LongInt" ---------------------------------------------------------------------- Thaddy · 2017-12-21 17:59 Quote from: molly on December 21, 2017, 06:56:23 pmQuote from: Handoko on December 21, 2017, 06:50:53 pmThe TS should got a compile-time error, didn't he? I'm very curious to know why one might think that. Seems i'm missing something obvious  Yes. X. His code does not compile... try copy paste.. should be Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i x[i]"arraystest.pas(11,12) Error: Incompatible types: got "LongInt" expected "Array[1..5] Of LongInt"" ---------------------------------------------------------------------- molly · 2017-12-21 18:00 I shall edit my first reply to emphasis on using code-tags  I usually quote first in such cases. ---------------------------------------------------------------------- Handoko · 2017-12-21 18:03 Quote from: Thaddy on December 21, 2017, 06:59:04 pm"arraystest.pas(11,12) Error: Incompatible types: got "LongInt" expected "Array[1..5] Of LongInt"" The error message was very clear, it even has line number and column position. Beginner's common error: too lazy to read. ---------------------------------------------------------------------- svy249 · 2017-12-21 18:03 Thank you. Everything wasn't working because of the "j" in the second array. ---------------------------------------------------------------------- molly · 2017-12-21 18:04 Quote from: Handoko on December 21, 2017, 07:03:04 pmThe error message was very clear, it even has line number and column position. Beginner's common error: too lazy to read. Pots and kettles are usually my domain ---------------------------------------------------------------------- svy249 · 2017-12-21 18:06 Now I have an another problem. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Sp;uses crt;var i,bx,by,yy,xx,v,v2,s: integer;var f: real;x: array [1..5] of Integer;{ìàññèâ èêñîâ}y: array [1..5] of Integer;{ìàññèâ èãðèêîâ}h: array [1..5] of Real;{ìàññèâ âåêòîðîâ}beginrandomize;For i:=1 to 5 do        x[i]:=random(40);For i:=1 to 5 do        y[i]:=random(40);readln(bx);readln(by);For i:=1 to 5 dobegin    if (x[i] > bx) and (y[i] > by) then       begin       v:=(x[i] - bx);     v2:=(y[i] - by);        h[i]:= sqrt(v*v + v2*v2);       end    else if (x[i] < bx) and (y[i] > by) then       begin       v:=(bx - x[i]);       v2:=(y[i] - by);        h[i]:= sqrt(v*v + v2*v2);       end    else if (x[i] < bx) and (y[i] < by) then       begin       v:=(bx - x[i]);       v2:=(by - y[i]);        h[i]:= sqrt(v*v + v2*v2);       end    else if ( x[i] > bx) and (y[i] < by) then       begin       v:=(x[i] - bx);     v2:=(by - y[i]);        h[i]:= sqrt(v*v + v2*v2);       end;end;For i:=1 to 5 do    if h[i] > f then       begin        f:=h[i];        s:=i ;       end;writeln(f);writeln('The boat number ',s);end. It always returns the first boat. I don't know why. ---------------------------------------------------------------------- molly · 2017-12-21 18:11 Quote from: Thaddy on December 21, 2017, 06:59:04 pmQuote from: molly on December 21, 2017, 06:56:23 pmQuote from: Handoko on December 21, 2017, 06:50:53 pmThe TS should got a compile-time error, didn't he? I'm very curious to know why one might think that. Seems i'm missing something obvious  Yes. X. His code does not compile... try copy paste.. should be Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i x[i]"arraystest.pas(11,12) Error: Incompatible types: got "LongInt" expected "Array[1..5] Of LongInt"" Tis almost x-mas so i went the xtra mile ---------------------------------------------------------------------- wp · 2017-12-21 18:18 Quote from: Thaddy on December 21, 2017, 06:46:47 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         x[i]:=random(40);  // there's your bug No bug. He did not use code tags in the very first post. Therefore the index "[ i]" is interpreted as the beginning of italic formatting. P.S. Molly, maybe you posted the same answer - I just did not understand it... ---------------------------------------------------------------------- molly · 2017-12-21 18:22 Quote from: wp on December 21, 2017, 07:18:27 pmP.S. Molly, maybe you posted the same answer - I just did not understand it... I simply used less words to express the problem. "Use code-tags" should be obvious for seasoned posters and a hint for newbies. @svy249: Also here i'm unable to 'see' the problem. i used 1 and 2 as well as 10 and 20 for input which resulted int he answer 3 and 2. Not one. ---------------------------------------------------------------------- Handoko · 2017-12-21 18:23 Thanks for clearing the misunderstanding. Now, time for serious. Not sure what TS want to do but I found this is bad: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     if h[i] > f then The variable "f" hasn't been initialized. Also the variable "s". ---------------------------------------------------------------------- molly · 2017-12-21 18:25 That is indeed a hint from compiler that shouldn't be ignored Handoko. ---------------------------------------------------------------------- Handoko · 2017-12-21 18:29 Tested on Lazarus, global integer variables that are not initialized have value = 0. So that if-then always returns false true. ---------------------------------------------------------------------- svy249 · 2017-12-21 18:32 Thanks.    Random just messed with me and I got the same result over and over again. ---------------------------------------------------------------------- molly · 2017-12-21 18:40 Quote from: Handoko on December 21, 2017, 07:29:20 pmTested on Lazarus, global integer variables that are not initialized have value = 0. So that if-then always returns false true. Sorry to be such a pita but a small correction (in basic you are correct): Code: [Select]  for i:=1 to 5     do if h[i] > f then     begin       f:=h[i];       s:=i ;     end; So on first iteration that statement holds while it doesn't for the follow up iterations (ofc depending on what values are stored inside h array). @svy249: using random(ize) can indeed sometimes mess with your expectations... in case you are unsure then simply write out the values so that you are able to see them.