Google Answers Logo
View Question
 
Q: visual basic 6 magic square test ( No Answer,   3 Comments )
Question  
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 ?

Request for Question Clarification by xemion-ga on 02 Sep 2002 23:24 PDT
An easier way of doing this might be some advanced Excel macro.  Would
you consider such an answer?  That allows the basic Visual Basic math
computations without creating a whole data entry interface.

xemion-ga

Clarification of Question by makossa-ga on 03 Sep 2002 03:31 PDT
No, I need to work in visual basic 6. What I am looking for is the
correct syntax needed to work through a for..next loop that
will add the rows, columns, and diagonals of a magic square,  and
compare it against a boolean, to determine if the square is
truely magic or not.

Clarification of Question by makossa-ga on 03 Sep 2002 03:34 PDT
I will actually be entering magic squares into this test, one at a
time to test them.

Clarification of Question by makossa-ga on 03 Sep 2002 14:30 PDT
Thank you, but I don't even know what delphi is. I am teaching myself
visual basic 6. I have no other programming languages.I guess I'm
looking for help on a fairly empirical level, in VB6.
Answer  
There is no answer at this time.

Comments  
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

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy