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: -
import java.util.*;
-
-
public class MagicSquare
-
{
-
public static void isMagic (int[][] b)
-
{
-
int sum = 0;
-
System.out.print('\n' + "Sum of Columns: ");
-
for (int i =0 ; i < b.length; i++)
-
{
-
for (int j= 0; j< b[i].length; j++)
-
{
-
sum= sum+b[0][j];
-
}
-
System.out.print(sum + " ");
-
sum = 0;
-
}
-
System.out.println(" ");
-
System.out.print("Sum of Rows: ");
-
int x =0;
-
while (x < b.length)
-
{
-
for (int i =0 ; i < b.length; i++)
-
{
-
sum= sum + b[i][x];
-
}
-
System.out.print(sum + " ");
-
sum=0;
-
x++;
-
}
-
{
-
System.out.println(" ");
-
System.out.print("Sum of Diagonals: ");
-
for (int i =0 ; i < b.length; i++)
-
{
-
for (int j= 0; j< b[i].length; j++)
-
{
-
sum= sum+b[0][j];
-
}
-
-
System.out.print(sum + " ");
-
sum = 0;
-
i++;
-
}
-
}
-
}
-
-
// returns true if sq is a magic square
-
boolean isMagicTest(int [][] b, int[][] xArray)
-
{
-
int sum = 0;// the sum that all rows, columns, and diags should equal
-
for (int i = 0; i < b.length; i++)
-
{
-
sum += b[i][0];
-
}
-
-
int diag1 = 0;
-
int diag2 = 0; // the sums of the two diagonals
-
for (int i = 0; i < b.length; i++)
-
{
-
int hor = 0, vert = 0; // the sums of the current row and column
-
for (int j = 0; j < b[i].length; j++)
-
{
-
if (b[i][j] == 0) return false;
-
hor += b[i][j];
-
vert += b[j][i];
-
}
-
if (hor != sum || vert != sum)
-
return false;
-
diag1 += b[i][i];
-
diag2 += b[2-i][i];
-
}
-
if (diag1 != sum || diag2 != sum)
-
//return false;
-
System.out.println("This is not a Magic square!");
-
return false;
-
// no failures found, so we have a magic square!
-
//return true;
-
System.out.println("This is a Magic square!");
-
return true;
-
}
-
-
public static void main (String[] args)
-
{
-
int[][] xArray = {{5, 9, 1}, {3, 4, 8}, {7, 2, 6}};
-
int[][] yArray = {{1, 3, 16, 14}, {8, 15, 2, 9},
-
{13, 6, 11, 4}, {12, 10, 5, 7}};
-
int[][] zArray = {{18, 24, 5, 6, 12}, {10, 11, 17, 23, 4},
-
{22, 3, 9, 15, 16}, {14, 20, 21, 2, 8},
-
{1, 7, 13, 19, 25}};
-
isMagic(xArray);
-
//isMagic(yArray);
-
//isMagic(zArray);
-
-
}
-
}
-
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
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!
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?
can someone please help??
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
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.
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.
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: -
int sum(int b[][], int x, int y, int dx, int dy) {
-
int sum= 0;
-
for (int i= 0; i < b.length; i++, x+= dx, y+= dy)
-
sum+= b[x][y];
-
return sum;
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
by: jyck91 |
last post by:
can any one tell me?? what should i do before i strating wirtitng the
magic square programe
|
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;
|
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...
| |
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...
|
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
|
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...
|
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;
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |