### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook (preview only)

### Author Topic: Problem with arrays  (Read 321 times)

#### imekon

• New Member
• Posts: 11
##### Problem with arrays
« on: August 25, 2019, 06:50:22 pm »
I'm having problems with arrays:

Code: Pascal  [Select]
1. unit matrix;
2.
3. {\$mode objfpc}{\$H+}
4.
5. interface
6.
7. uses
8.   Classes, SysUtils;
9.
10. type
11.   FLOAT = single;
12.
13.   { TMatrix2x2 }
14.
15.   TMatrix2x2 = class
16.   private
17.     _data: array [0..1, 0..1] of FLOAT;
18.     procedure Clear;
19.   public
20.     constructor Create;
21.     constructor Create(aData: array of FLOAT);
22.     function ToString: string; override;
23.   end;
24.
25.   { TMatrix3x3 }
26.
27.   TMatrix3x3 = class
28.   private
29.     _data: array [0..2, 0..2] of FLOAT;
30.     procedure Clear;
31.   public
32.     constructor Create;
33.     constructor Create(aData: array of FLOAT);
34.     function ToString: string; override;
35.
36.     class function SubMatrix(matrix: TMatrix3x3; row, column: integer): TMatrix2x2;
37.   end;
38.
39.
40. implementation
41.
42. { TMatrix2x2 }
43.
44. procedure TMatrix2x2.Clear;
45. var
46.   row, column: integer;
47.
48. begin
49.   for row := 0 to 1 do
50.     for column := 0 to 1 do
51.       _data[row, column] := 0;
52. end;
53.
54. constructor TMatrix2x2.Create;
55. begin
56.   Clear;
57. end;
58.
59. constructor TMatrix2x2.Create(aData: array of FLOAT);
60. var
61.   row, column: integer;
62.
63. begin
64.   if Length(aData) <> 4 then exit;
65.
66.   for row := 0 to 1 do
67.     for column := 0 to 1 do
68.       _data[row, column] := aData[column + row * 2];
69. end;
70.
71. function TMatrix2x2.ToString: string;
72. begin
73.   result := Format('%1.2f, %1.2f, %1.2f, %1.2f',
74.                     [_data[0, 0], _data[1, 0],
75.                      _data[0, 1], _data[1, 1]]);
76.  end;
77.
78. { TMatrix3x3 }
79.
80. procedure TMatrix3x3.Clear;
81. var
82.   row, column: integer;
83.
84. begin
85.   for row := 0 to 2 do
86.     for column := 0 to 2 do
87.       _data[row, column] := 0;
88. end;
89.
90. constructor TMatrix3x3.Create;
91. begin
92.   Clear;
93. end;
94.
95. constructor TMatrix3x3.Create(aData: array of FLOAT);
96. var
97.   row, column: integer;
98.
99. begin
100.   if Length(aData) <> 9 then exit;
101.
102.   for row := 0 to 2 do
103.     for column := 0 to 2 do
104.       _data[row, column] := aData[column + 3 * row];
105. end;
106.
107. function TMatrix3x3.ToString: string;
108. begin
109.   result := Format('%1.2f, %1.2f, %1.2f, %1.2f, %1.2f, %1.2f, %1.2f, %1.2f, %1.2f',
110.                    [_data[0, 0], _data[0, 1], _data[0, 2],
111.                     _data[1, 0], _data[1, 1], _data[1, 2],
112.                     _data[2, 0], _data[2, 1], _data[2, 2]]);
113.
114. end;
115.
116. class function TMatrix3x3.SubMatrix(matrix: TMatrix3x3; row, column: integer
117.   ): TMatrix2x2;
118. var
119.   sub: array [0..3] of FLOAT;
120.   value: FLOAT;
121.   i, x, y: integer;
122.
123. begin
124.   i := 0;
125.   for y := 0 to 2 do
126.   begin
127.     if y <> row then
128.     begin
129.        for x := 0 to 2 do
130.          if x <> column then
131.          begin
132.            value := matrix._data[row, column];
133.            sub[i] := value;
134.            inc(i);
135.          end;
136.     end;
137.   end;
138.   result := TMatrix2x2.Create(sub);
139. end;
140.
141. end.
142.

When I use this with this program I get the wrong result:
Code: Pascal  [Select]
1. program test;
2.
3. uses matrix;
4.
5. var
6.   source: TMatrix3x3;
7.   output: TMatrix2x2;
8.
9. begin
10.   source := TMatrix3x3.Create([1, 5, 0,
11.                               -3, 2, 7,
12.                                0, 6, -3]);
13.   output := TMatrix3x3.SubMatrix(source, 0, 2);
14.
15.   WriteLn('The correct answer is: -3, 2, 0, 6');
16.   WriteLn(output.ToString);
17.   source.Free;
18.   output.Free;
19.
20.   WriteLn('press RETURN...');
22. end.
23.

The output is as follows:

The correct answer is: -3, 2, 0, 6
0.00, 0.00, 0.00, 0.00
press RETURN...

Somehow the function SubMatrix is not transferring the data from the 3x3 matrix to the size 4 array.

Any ideas?

#### ASerge

• Hero Member
• Posts: 1393
##### Re: Problem with arrays
« Reply #1 on: August 25, 2019, 07:17:02 pm »
Code: Pascal  [Select]
1. ...
2. class function TMatrix3x3.SubMatrix(matrix: TMatrix3x3; row, column: integer): TMatrix2x2;
3. ...
4.            value := matrix._data[row, column];
5. ...
6.
Code: Pascal  [Select]
1. value := matrix._data[y, x];

#### imekon

• New Member
• Posts: 11
##### Re: Problem with arrays
« Reply #2 on: August 25, 2019, 10:48:53 pm »
Thanks.

I couldn't see that for the trees.

Now if I can figure out why the order of the output is wrong now!

The correct answer is: -3, 2, 0, 6
-3.00, 0.00, 2.00, 6.00
press RETURN...

#### ASerge

• Hero Member
• Posts: 1393
##### Re: Problem with arrays
« Reply #3 on: August 26, 2019, 04:26:39 pm »
Now if I can figure out why the order of the output is wrong now!
The correct answer is: -3, 2, 0, 6
-3.00, 0.00, 2.00, 6.00
Because the ToString method outputs data in that order, from top to bottom, then from left to right.

#### imekon

• New Member
• Posts: 11
##### Re: Problem with arrays
« Reply #4 on: August 27, 2019, 12:27:58 pm »
Thanks again.