Recent

Author Topic: [SOLVED]Why isn't my array working?  (Read 4544 times)

svy249

  • Newbie
  • Posts: 4
[SOLVED]Why isn't my array working?
« on: December 21, 2017, 06:41:02 pm »
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.
« Last Edit: December 21, 2017, 07:32:16 pm by svy249 »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Why isn't my array working?
« Reply #1 on: December 21, 2017, 06:45:15 pm »
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,
« Last Edit: December 21, 2017, 07:01:37 pm by molly »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Why isn't my array working?
« Reply #2 on: December 21, 2017, 06:45:50 pm »
x.....
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Why isn't my array working?
« Reply #3 on: December 21, 2017, 06:46:47 pm »
x.....

Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$H+}{$I-}{$endif}
  2. uses crt;
  3. var i,k,j,bx,by,yy,xx,v,v2: integer;
  4. x: array [1..5] of Integer;
  5. y: array [1..5] of Integer;
  6. h: array [1..5] of Real;
  7. begin
  8. randomize;
  9. For i:=1 to 5 do
  10.         x[i]:=random(40);  // there's your bug
  11. For i:=1 to 5 do
  12.         y[j]:=random(40);
  13. end.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Why isn't my array working?
« Reply #4 on: December 21, 2017, 06:50:53 pm »
The TS should got a compile-time error, didn't he?
Maybe he simply ignored that message.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Why isn't my array working?
« Reply #5 on: December 21, 2017, 06:56:23 pm »
The 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  :-X

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

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Why isn't my array working?
« Reply #6 on: December 21, 2017, 06:57:23 pm »
The 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"
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Why isn't my array working?
« Reply #7 on: December 21, 2017, 06:59:04 pm »
The 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  :-X
Yes. X. His code does not compile... try copy paste.. should be
Code: Pascal  [Select][+][-]
  1. x[i]
"arraystest.pas(11,12) Error: Incompatible types: got "LongInt" expected "Array[1..5] Of LongInt""
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Why isn't my array working?
« Reply #8 on: December 21, 2017, 07:00:26 pm »
I shall edit my first reply to emphasis on using code-tags  :P

I usually quote first in such cases.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Why isn't my array working?
« Reply #9 on: December 21, 2017, 07:03: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.
;D


svy249

  • Newbie
  • Posts: 4
Re: Why isn't my array working?
« Reply #10 on: December 21, 2017, 07:03:42 pm »
Thank you. Everything wasn't working because of the "j" in the second array.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Why isn't my array working?
« Reply #11 on: December 21, 2017, 07:04:10 pm »
The error message was very clear, it even has line number and column position.
Beginner's common error: too lazy to read.
;D
Pots and kettles are usually my domain  :P

svy249

  • Newbie
  • Posts: 4
Re: Why isn't my array working?
« Reply #12 on: December 21, 2017, 07:06:16 pm »
Now I have an another problem.
Code: Pascal  [Select][+][-]
  1. program Sp;
  2. uses crt;
  3. var i,bx,by,yy,xx,v,v2,s: integer;
  4. var f: real;
  5. x: array [1..5] of Integer;{ìàññèâ èêñîâ}
  6. y: array [1..5] of Integer;{ìàññèâ èãðèêîâ}
  7. h: array [1..5] of Real;{ìàññèâ âåêòîðîâ}
  8. begin
  9. randomize;
  10. For i:=1 to 5 do
  11.         x[i]:=random(40);
  12. For i:=1 to 5 do
  13.         y[i]:=random(40);
  14. readln(bx);
  15. readln(by);
  16. For i:=1 to 5 do
  17. begin
  18.     if (x[i] > bx) and (y[i] > by) then
  19.        begin
  20.        v:=(x[i] - bx);
  21.        v2:=(y[i] - by);
  22.         h[i]:= sqrt(v*v + v2*v2);
  23.        end
  24.     else if (x[i] < bx) and (y[i] > by) then
  25.        begin
  26.        v:=(bx - x[i]);
  27.        v2:=(y[i] - by);
  28.         h[i]:= sqrt(v*v + v2*v2);
  29.        end
  30.     else if (x[i] < bx) and (y[i] < by) then
  31.        begin
  32.        v:=(bx - x[i]);
  33.        v2:=(by - y[i]);
  34.         h[i]:= sqrt(v*v + v2*v2);
  35.        end
  36.     else if ( x[i] > bx) and (y[i] < by) then
  37.        begin
  38.        v:=(x[i] - bx);
  39.      v2:=(by - y[i]);
  40.         h[i]:= sqrt(v*v + v2*v2);
  41.        end;
  42. end;
  43. For i:=1 to 5 do
  44.     if h[i] > f then
  45.        begin
  46.         f:=h[i];
  47.         s:=i ;
  48.        end;
  49. writeln(f);
  50. writeln('The boat number ',s);
  51. end.
  52.  
It always returns the first boat. I don't know why.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Why isn't my array working?
« Reply #13 on: December 21, 2017, 07:11:24 pm »
The 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  :-X
Yes. X. His code does not compile... try copy paste.. should be
Code: Pascal  [Select][+][-]
  1. 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

  • Hero Member
  • *****
  • Posts: 11916
Re: Why isn't my array working?
« Reply #14 on: December 21, 2017, 07:18:27 pm »
Code: Pascal  [Select][+][-]
  1.         x[i]:=random(40);  // there's your bug
  2.  
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...

 

TinyPortal © 2005-2018