Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to exchange rows in a matrix Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Algorithm
Language
Delphi 2.x
Views
93
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I'm working with a matrix and I've chosen to use an Array of Array of real to do it 
(Is it the best way? I need the elements to be of real type). The problem is that I 
must change a certain line with another. For example, change the first line of the 
matrix with the second one. How do I do it quickly? I don't want to move element by 
element.

Answer:

1   program Matrices;
2   
3   {$APPTYPE CONSOLE}
4   
5   uses
6     SysUtils;
7   
8   type
9     TMatrixRow = array of Double; {preferrable to Real}
10    TMatrix = array of TMatrixRow;
11  
12  procedure MatrixExchangeRows(M: TMatrix; First, Second: Integer);
13  var
14    Help: TMatrixRow;
15  begin
16    if (First < 0) or (First > High(M)) or (Second < 0) or (Second > High(M)) then
17      Exit; {or whatever you like.}
18    {Only pointers are exchanged!}
19    Help := M[First];
20    M[First] := M[Second];
21    M[Second] := Help;
22  end;
23  
24  procedure MatrixWrite(M: TMatrix);
25  var
26    Row, Col: Integer;
27  begin
28    for Row := 0 to High(M) do
29    begin
30      for Col := 0 to High(M[Row]) do
31        write(M[Row, Col]: 10: 2);
32      Writeln;
33    end;
34    Writeln;
35  end;
36  
37  var
38    Matrix: TMatrix;
39    Row, Column: Integer;
40  
41  begin
42    Randomize;
43    SetLength(Matrix, 4, 4);
44    for Row := 0 to High(Matrix) do
45      for Column := 0 to High(Matrix[Row]) do
46        Matrix[Row, Column] := Random * 1000.0;
47    MatrixWrite(Matrix);
48    MatrixExchangeRows(Matrix, 1, 2);
49    MatrixWrite(Matrix);
50    Readln;
51  end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC