Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: JAVA MAGIC SQUARE

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAVA MAGIC SQUARE

    Thanks in advance! I have been working on this for hours and can't seem to get it right. I added the directions as an attachment. I can't seem to get B working. any ideas as to how to fix it? thanks!

    import java.util.Scanner;
     
    public class OutsideLab5 {
      public static Scanner sc=new Scanner(System.in);
      public static void main(String[] args) {
     
        System.out.println(createArithmeticSeq());
        matricize(new int [16], new int [4][4]);
     
      }
        public static String createArithmeticSeq() {
          int first, second;
          int[] arrayA=new int[16];
          first=0;
          second=0;
          System.out.println("Please input two numbers. \n The second will be subtracted \n from the first.");
          System.out.print("Please input the first number: ");
          first=sc.nextInt();
          arrayA[0]=first;
          System.out.print("Please input the second number: ");
          second=sc.nextInt();
     
          for(int i=0;i < 15;i++) {
            arrayA[i+1]=arrayA[i]+second; }
          for(int i=0;i<16;i++) {
            System.out.print(arrayA[i]+" "); }
           return "";
          }
        public static String matricize(int arrayB[], int arrayC[][]) {
          for(int i=0;i<4;i++) {
            for(int j=0;j<4;j++) {
              arrayC[i][j]=arrayB[i*4+j];
           arrayB=new int[16];
           arrayC=new int[4][4];
            }
     
        }
          return "";
        }}
    Attached Files Attached Files
    Last edited by hiimjoey11; December 7th, 2010 at 02:45 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JAVA MAGIC SQUARE

    Almost nobody is going to want to open up an attachment just because you are too lazy to ask a specific question. Call us lazy, but why would you want to make it harder for somebody to help you?

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA MAGIC SQUARE

    Good point. Sorry about that. Here are the directions Magic Square:
    a. Write a method createArithmeticSeq that prompts the user to input two numbers, first and diff. The method then creates a one-dimensional array of 16 elements ordered in an arithmetic sequence. It also outputs the arithmetic sequence. For example, if first=21 and diff =5, the arithmetic sequence is: 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96.

    b. Write a method matricize that takes a one-dimensional array of 16 elements and a two-dimensional array of 4 rows, and 4 columns as parameters. This method puts the elements of the one-dimensional array into the two-dimensional array. For example, if A is the one dimensional array created in part A, and B is a two dimensional arrray, then after putting the elements of A into B, the array B is:
    21 26 31 36
    41 46 51 56
    61 66 71 76
    81 86 91 96

    Part a is working fine, i just need help with part b. Thanks again!

  4. #4
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: JAVA MAGIC SQUARE

    Quote Originally Posted by hiimjoey11 View Post
    Good point. Sorry about that. Here are the directions Magic Square:
    a. Write a method createArithmeticSeq that prompts the user to input two numbers, first and diff. The method then creates a one-dimensional array of 16 elements ordered in an arithmetic sequence. It also outputs the arithmetic sequence. For example, if first=21 and diff =5, the arithmetic sequence is: 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96.

    b. Write a method matricize that takes a one-dimensional array of 16 elements and a two-dimensional array of 4 rows, and 4 columns as parameters. This method puts the elements of the one-dimensional array into the two-dimensional array. For example, if A is the one dimensional array created in part A, and B is a two dimensional arrray, then after putting the elements of A into B, the array B is:
    21 26 31 36
    41 46 51 56
    61 66 71 76
    81 86 91 96

    Part a is working fine, i just need help with part b. Thanks again!
    Have you tried anything? Part b can be accomplished with 2 for loops, and one statement inside the loops. It is very simple.

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA MAGIC SQUARE

    Yeah in my code as posted above i tried this :

    public static String matricize(int arrayB[], int arrayC[][]) {
    for(int i=0;i<4;i++) {
    for(int j=0;j<4;j++) {
    arrayC[i][j]=arrayB[i*4+j];
    arrayB=new int[16];
    arrayC=new int[4][4];
    }

    }
    return "";
    }}


    the problem is, i can't get it to print out in my main..

  6. #6
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: JAVA MAGIC SQUARE

    that is almost right... I would take out the last 2 lines that are creating new int arrays, since they are overwriting what was passed in (as a general rule, parameters of methods should not be assigned in the method).

    Also, the return type of the method should be void.

    To print it the code is almost the same. You use the same two for loops. Instead of assigning the value at [i][j], you print the value at [i][j]. After the inner loop, print a new line.

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA MAGIC SQUARE

    Ah worked perfect thanks. Now part c says: Write a method reverseDiagonal that revers both diagonals of a two-dimensional array. For example, if the two-dimensional array is as show in part (b), after reversing the diagonals, the two dimensional array is:
    96 26 31 81
    41 71 66 56
    61 51 46 76
    36 86 91 21

    I'm not really sure how to go about doing this. Maybe using a loop to pick out each number? sorry I am kind of new to java and our teacher kind of threw this at us.

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA MAGIC SQUARE

    I think i am on the right track. But it isn't working right. this is what i have so far:
        public static int [][] reverseDiagonal(int arrayReverse[][]) {
          int s=0;
          int temp;
     
          for (int row = 0; row < 4/2; row++){
            temp=arrayReverse[row][row];
            arrayReverse[row][row]=arrayReverse[4-1-row][4-1-row];
            arrayReverse[4-1-row][4-1-row]=temp;
            System.out.println(temp);
            }
    }

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JAVA MAGIC SQUARE

    Quote Originally Posted by hiimjoey11 View Post
    I think i am on the right track. But it isn't working right.
    What isn't working about it? What do you expect it to do? What does it do instead? Try stepping through it with a debugger and checking what each line does against what you think each line should do. Where's your SSCCE?

  10. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA MAGIC SQUARE

        public static int [][] reverseDiagonal(int arrayReverse[][]) {
          int s=0;
          int temp,temp1;
     
          for (int row = 0; row < 4/2; row++){
            temp=arrayReverse[row][row];
            arrayReverse[row][row]=arrayReverse[4-1-row][4-1-row];
            arrayReverse[4-1-row][4-1-row]=temp;
            System.out.println(arrayReverse[row][row]); }
          for(int row=0;row<4/2;row++) {
            temp1=arrayReverse[row][4-1-row];
            arrayReverse[row][4-1-row]=arrayReverse[4-1-row][row];
            arrayReverse[4-1-row][row]=temp1;
            System.out.println(arrayReverse[row][row]+" ");
          }

    I am trying to reverse the diagonals of the two dimensional array...but for some reason it only switches two numbers and prints them out. It doesn't go the entire way, or replace them.

  11. #11
    Member vigneshwaran's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    35
    My Mood
    Cheerful
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: JAVA MAGIC SQUARE

     ...code removed
    Last edited by copeg; November 28th, 2012 at 12:41 AM. Reason: removed spoonfed code

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JAVA MAGIC SQUARE

    @vigneshwaran, please don't resurrect a two year old post, especially to spoonfeed code. Recommend you read the forum FAQ and the following:
    http://www.javaprogrammingforums.com...n-feeding.html

  13. The Following User Says Thank You to copeg For This Useful Post:

    vigneshwaran (November 28th, 2012)

Similar Threads

  1. Latin square logic
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2010, 09:38 AM
  2. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 06:18 PM
  3. Magic square class (modular math problem)
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 30th, 2010, 10:56 AM
  4. Magic Squares, input confusion
    By bengiles89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 08:40 PM
  5. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM