473,549 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Magic Square program run amuck!

KoreyAusTex
36 New Member
I am pretty new at programming and need some feedback as to why this program is not working??? It was a pretty big undertaking for me but I can't seem to figure out what is wrong:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class MagicSquare
  4. {
  5.     public static void isMagic (int[][] b)
  6.     {
  7.        int sum = 0;
  8.        System.out.print('\n' + "Sum of Columns: ");
  9.        for (int i =0 ; i < b.length; i++)
  10.         {
  11.         for (int j= 0; j< b[i].length; j++)
  12.         {
  13.             sum= sum+b[0][j];
  14.         }
  15.             System.out.print(sum + " ");
  16.             sum = 0;
  17.         }
  18.         System.out.println(" ");
  19.         System.out.print("Sum of Rows: ");
  20.         int x =0;
  21.        while (x < b.length)
  22.        {
  23.        for (int i =0 ; i < b.length; i++)
  24.         {
  25.             sum= sum + b[i][x];
  26.         }
  27.             System.out.print(sum + " ");
  28.             sum=0;
  29.             x++;
  30.         }
  31.         {
  32.             System.out.println(" ");
  33.             System.out.print("Sum of Diagonals: ");
  34.             for (int i =0 ; i < b.length; i++)
  35.             {
  36.                 for (int j= 0; j< b[i].length; j++)
  37.                 {
  38.                     sum= sum+b[0][j];
  39.                 }
  40.  
  41.                 System.out.print(sum + " ");
  42.                 sum = 0;
  43.                 i++;
  44.             }      
  45.         }
  46.     }
  47.  
  48.     // returns true if sq is a magic square 
  49.     boolean isMagicTest(int [][] b, int[][] xArray) 
  50.     {
  51.         int sum = 0;// the sum that all rows, columns, and diags should equal
  52.         for (int i = 0; i < b.length; i++) 
  53.         { 
  54.             sum += b[i][0]; 
  55.         }
  56.  
  57.         int diag1 = 0;
  58.         int diag2 = 0; // the sums of the two diagonals
  59.         for (int i = 0; i < b.length; i++)
  60.         {
  61.             int hor = 0, vert = 0; // the sums of the current row and column
  62.             for (int j = 0; j < b[i].length; j++)
  63.             {
  64.                 if (b[i][j] == 0) return false;
  65.                 hor += b[i][j];
  66.                 vert += b[j][i];
  67.             }
  68.             if (hor != sum || vert != sum) 
  69.                 return false;
  70.             diag1 += b[i][i];
  71.             diag2 += b[2-i][i];
  72.          }
  73.          if (diag1 != sum || diag2 != sum) 
  74.             //return false;
  75.             System.out.println("This is not a Magic square!");
  76.             return false;
  77.          // no failures found, so we have a magic square!
  78.          //return true;
  79.          System.out.println("This is a Magic square!");
  80.          return true;
  81.         }   
  82.  
  83.     public static void main (String[] args)
  84.     {
  85.         int[][] xArray = {{5, 9, 1}, {3, 4, 8}, {7, 2, 6}};
  86.         int[][] yArray = {{1, 3, 16, 14}, {8, 15, 2, 9},
  87.         {13, 6, 11, 4}, {12, 10, 5, 7}};
  88.         int[][] zArray = {{18, 24, 5, 6, 12}, {10, 11, 17, 23, 4},
  89.              {22, 3, 9, 15, 16}, {14, 20, 21, 2, 8},
  90.                      {1, 7, 13, 19, 25}};
  91.         isMagic(xArray);
  92.         //isMagic(yArray);
  93.         //isMagic(zArray);
  94.  
  95.     }
  96. }
  97.  
Jul 21 '07 #1
8 3707
JosAH
11,448 Recognized Expert MVP
edit: all the spoonfeeding by me deleted; the OP got all the help already in
Sun's Java forum but choose to ignore it.

Jos
Jul 21 '07 #2
prometheuzz
197 Recognized Expert New Member
I am pretty new at programming and need some feedback as to why this program is not working??? It was a pretty big undertaking for me but I can't seem to figure out what is wrong:

...
Why did you not reply to the reactions in your thread at Sun's forum?
http://forum.java.sun.com/thread.jsp...readID=5197489

You're a time waster!
Jul 21 '07 #3
KoreyAusTex
36 New Member
Why did you not reply to the reactions in your thread at Sun's forum?
http://forum.java.sun.com/thread.jsp...readID=5197489

You're a time waster!
Because I am not sure what you are talking about, I just wrote this program last night and today, I looked at the link but I am not sure what you mean? How am I a timewaster?
Jul 22 '07 #4
KoreyAusTex
36 New Member
can someone please help??
Jul 22 '07 #5
JosAH
11,448 Recognized Expert MVP
Because I am not sure what you are talking about, I just wrote this program last night and today, I looked at the link but I am not sure what you mean? How am I a timewaster?
I compared the source code in this thread with the source code in the thread
mentioned by Prometheuzz: most of the erroneous logic is the same, most of
the variable names are the same and the test magic triangles are the same.

If conclude that both source code texts were written by you; you received good
help in that other forum. You decided to post your question here again. Why?
Our answer would have been the same as you got in that other forum. Even the
same people hang around in both the forums as you can see.

kind regards,

Jos
Jul 22 '07 #6
KoreyAusTex
36 New Member
I compared the source code in this thread with the source code in the thread
mentioned by Prometheuzz: most of the erroneous logic is the same, most of
the variable names are the same and the test magic triangles are the same.

If conclude that both source code texts were written by you; you received good
help in that other forum. You decided to post your question here again. Why?
Our answer would have been the same as you got in that other forum. Even the
same people hang around in both the forums as you can see.

kind regards,

Jos

By the way why don't you get in touch with the other kid and ask him, you talk to him and I can guarantee YOU ARE WRONG.
Jul 22 '07 #7
prometheuzz
197 Recognized Expert New Member
By the way why don't you get in touch with the other kid and ask him, you talk to him and I can guarantee YOU ARE WRONG.
Ok, that could be true. And if it is, I apologize. You have to understand that this does happen quite a lot and that it is frustrating if you spend time answering someone but that person doesn't even look at it because he posted the same thing else where.
But you needn't have made such a big fuss about it: I merely said you were waisting other people's time (if it was you indeed who cross posted this), which is IMO not such a big deal. If you had only explained yourself like you explained yourself in the feedback message all would be OK, and I'm sure you would have received some good advice. Unfortunately you also used quite some foul language in your feedback response which does not help you a lot. Perhaps someone (other than me) would like to help you on this.

Good luck.
Jul 22 '07 #8
JosAH
11,448 Recognized Expert MVP
Ok, I'll help you out a bit then, but there'll be less spoonfeeding; suppose you
start at a postion x,y in a square matrix. This x,y position is at the border of
the matrix. Also suppose you 'walk' in the direction dx, dy; you can find the sum
of the numers on that traversal walk like this:

Expand|Select|Wrap|Line Numbers
  1. int sum(int b[][], int x, int y, int dx, int dy) {
  2.    int sum= 0;
  3.    for (int i= 0; i < b.length; i++, x+= dx, y+= dy)
  4.       sum+= b[x][y];
  5.    return sum;
  6. }
  7.  
If you understand this little method you'll also understand that it can compute
sums of rows, sums of columns and the sums of the two diagonals. All you
have to do is feed it the correct x, y, dx and dy values.

Check whether or not those sums are equal. If they are the square is a magic square.

kind regards,

Jos
Jul 22 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
4725
by: winnerpl | last post by:
Hey guys I'm trying to get a magic square but I'm stuck with it printing out only 1's and 0's in random places. Hope you experts can can provide some input on what I'm doing wrong. #include <stdio.h> #define MAX 20 int x; int size_request(void); //asks the user to enter the size x size square.
1
10526
by: shaveta | last post by:
pls guide me to write a program in c to generate a magic square of size n*n , where n is odd A magic square is an n*n matrix of integer from 1 to n^2, where n is odd, such that the sum of every row, column and diagonal is same. The rule is - Start with 1 in the middle of the first row; then go up and left , assigning nos. in increasing...
4
1815
by: jyck91 | last post by:
can any one tell me?? what should i do before i strating wirtitng the magic square programe
2
2564
by: jyck91 | last post by:
i have done the magic square: #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 13 main() { FILE *fp; int i, j, n, row, column;
5
2206
by: magic man | last post by:
I need help ... I have very rudimentary VB skills. I am working on a topographical model of a magic square. I consider each cell in the square to be a solid structure to the height specified by the value in the square. Then conceptually I pour water on the structure and wish to see where the water collects in "lakes" on the structure. What...
1
3902
by: magic man | last post by:
I am 50 years old ...and am working physical models of the math structure called a magic square .. for my own interest. My present problem is this. I have a topograhical model for the square ... where each cell in the square is a solid structure to the height specified by the value in that cell. Then conceptually I pour water on top of the...
1
4163
by: manju01 | last post by:
in the below program we can generate magic square of size 3-160 but i want to print the output like for magic size n ************************ * * * * * * 5 * 8 * 7 * 6 * ************************ that is in grid
4
3404
by: inferi9 | last post by:
Hi, I am working in a program caals magic square and it must be done only with loops and user definied funcations,I will tell you about my code and where my problem, the main is only calls the other funcations,The first funcation makes a squence which the user input where it begins and the differents between easch elements the size of this one...
6
4155
by: Blue sky | last post by:
Hi ,I think the follow program is right in logical But why the compiler output :"square:declared identifier" #include<stdio.h> #include<math.h> int main() { double x1;
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.