Java program magic square




















Viewed 37k times. These are the characteristics for writing the code: Ask the user for an odd number Create an n by n array. Follow these steps to create a magic square. Place a 1 in the middle of the first row. Subtract 1 from the row and add 1 to the column. If possible place the next number at that position. If not possible, follow these steps. If in row -1, then change to last row If in last column change to first column If blocked, then drop down to next row from original position if in the upper right corner, then drop down to next row.

Print the array I've written the code but when I run it, the program puts in all the numbers except for the number two; for some reason, my program skips over it. For example, if I put in the number 3 as the odd number, my output is: 6 1 0 3 4 5 9 7 8 0 isn't supposed to be there but the number two is.

Marv 3, 2 2 gold badges 20 20 silver badges 46 46 bronze badges. Your variable console is undefined. Chris it's probably a field of his class since his code appears to be compiling.

Chris It is in the program I just didn't include it here. As number starts at 1 , and is never decremented, the only reason why you may have 0 in your array, is that the corresponding index is never populated. That's precisely what's happening here, as the logic of the requirement, or at least the code representing it, prevents it. The 2 gets written to 2nd row, 3rd column, but is later overwritten by 5. Add a comment. Active Oldest Votes. Do you need similar project to be completed? Place an order with our service and get qualified help with assignments as soon as needed.

All you have to do is to specify your instructions and expectations. All the rest will be done by our service. Your email address will not be published. Leave a Reply Cancel reply Your email address will not be published. Also note that the question asked in the ISC Practical of explicitly mentioned that each integer appears only once. Pingback: Magic square www. The purpose of the boolean array flag is to store whether any given integer in the range 1 to n 2 has already been encountered or not.

This site uses Akismet to reduce spam. Learn how your comment data is processed. Like this: Like Loading Leave a Reply Cancel reply.



0コメント

  • 1000 / 1000