![]() |
|
|
| Subject:
visual basic 6 magic square test
Category: Computers > Programming Asked by: makossa-ga List Price: $10.00 |
Posted:
02 Sep 2002 20:39 PDT
Expires: 05 Sep 2002 21:42 PDT Question ID: 61115 |
How would I set up a test to determine if a magic square was actually magic or not using visual basic 6 ? | |
| |
| |
| |
|
|
| There is no answer at this time. |
|
| Subject:
Re: visual basic 6 magic square test
From: gw-ga on 03 Sep 2002 08:25 PDT |
Here's a Delphi solution which might help you get started.
type
TMatrixRow = array of Integer;
TMatrix = array of TMatrixRow;
function IsMagicSquare(const Matrix: TMatrix): Boolean;
var
I: Integer; // row index
J: Integer; // column index
Sum: Integer; // ideal sum of cells in any row, column, or main diagonal
Temp: Integer; // actual sum of cells
N: Integer; // number of rows
begin
Result := True;
N := Length(Matrix);
// matrix must be square
for I := 0 to High(Matrix) do
if (Length(Matrix[I]) <> N) then
begin
Result := False;
Break;
end;
Sum := N * (N * N + 1) div 2;
// check row sums
if (Result) then
for I := 0 to (N - 1) do
begin
Temp := 0;
for J := 0 to (N - 1) do
Inc(Temp, Matrix[I, J]);
if (Temp <> Sum) then
begin
Result := False;
Break;
end;
end;
// check column sums
if (Result) then
for J := 0 to (N - 1) do
begin
Temp := 0;
for I := 0 to (N - 1) do
Inc(Temp, Matrix[I, J]);
if (Temp <> Sum) then
begin
Result := False;
Break;
end;
end;
// check diagonal #1
if (Result) then
begin
Temp := 0;
for I := 0 to (N - 1) do
Inc(Temp, Matrix[I, I]);
if (Temp <> Sum) then
Result := False;
end;
// check diagonal #2
if (Result) then
begin
Temp := 0;
for I := 0 to (N - 1) do
Inc(Temp, Matrix[I, N - I - 1]);
if (Temp <> Sum) then
Result := False;
end;
end;
procedure Demo;
var
Matrix: TMatrix;
begin
SetLength(Matrix, 3, 3);
Matrix[0, 0] := 8;
Matrix[0, 1] := 1;
Matrix[0, 2] := 6;
Matrix[1, 0] := 3;
Matrix[1, 1] := 5;
Matrix[1, 2] := 7;
Matrix[2, 0] := 4;
Matrix[2, 1] := 9;
Matrix[2, 2] := 2;
if (IsMagicSquare(Matrix)) then
ShowMessage('Yes')
else
ShowMessage('No');
end; |
| Subject:
Re: visual basic 6 magic square test
From: gw-ga on 03 Sep 2002 08:30 PDT |
In my example code, you can replace the one occurence of High(Matrix) with (N - 1) to make it clearer. |
| Subject:
Re: visual basic 6 magic square test
From: mikkol-ga on 03 Sep 2002 08:48 PDT |
I found your question quite interesting so I tried it out... here's my
solution.. tested on 3x3 matrix but not guaranteed to work on higher
ones tho (it should be). Also I've written it on ASP (VBScript) but I
guess VB should be the same.
function checkMagicSquare (mx, dimension)
dim compareSum,correctSum, i
correctSum = dimension * (dimension * dimension + 1) / 2
' first check to make sure the size of the array is correct
checkMagicSquare = ((ubound(mx, 1) = dimension - 1) AND
(ubound(mx, 2) = dimension - 1))
if checkMagicSquare then
' Check the fist diagonal
compareSum = 0
for i = 0 to dimension - 1
compareSum = compareSum + mx(i,i)
next
checkMagicSquare = (compareSum = correctSum)
end if
if checkMagicSquare then
' Check the second diagonal
compareSum = 0
for i = 0 to dimension - 1
compareSum = compareSum + mx(i,dimension - 1 - i)
next
checkMagicSquare = (compareSum = correctSum)
end if
if checkMagicSquare then
' Check each row
for i = 0 to dimension - 1
compareSum = 0
if not checkMagicSquare then exit for
'If the previous test fails, ignore the rest of the tests
for j = 0 to dimension - 1
compareSum = compareSum + mx(i,j)
next
checkMagicSquare = (compareSum = correctSum)
next
end if
if checkMagicSquare then
' Check each column
for i = 0 to dimension - 1
compareSum = 0
if not checkMagicSquare then exit for
'If the previous test fails, ignore the rest of the tests
for j = 0 to dimension - 1
compareSum = compareSum + mx(j,i)
next
checkMagicSquare = (compareSum = correctSum)
next
end if
end function
Dim mx3x3(2,2)
mx3x3(0,0) = 8
mx3x3(0,1) = 1
mx3x3(0,2) = 6
mx3x3(1,0) = 3
mx3x3(1,1) = 5
mx3x3(1,2) = 7
mx3x3(2,0) = 4
mx3x3(2,1) = 9
mx3x3(2,2) = 2 |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |