Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    shaq is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Arrow 2D array program

    I have to make a program and I'm not familiar with 2D arrays. I need to take a 1D array convert it to a 2D array. I'm taking a string whose length is a square, and initialize the characters into a 1D array, then initialize them into a 2D array, like a square.

    So this is what I need to do:

    Java Code:
            // a[0][0] <-- ch[0] <-- '1'
            // a[0][1] <-- ch[1] <-- '2'
            // a[0][2] <-- ch[2] <-- '3'
            // a[1][0] <-- ch[3] <-- '4'
            // a[1][1] <-- ch[4] <-- '5'
            // a[1][2] <-- ch[5] <-- '6'
            // a[2][0] <-- ch[6] <-- '7'
            // a[2][1] <-- ch[7] <-- '8'
            // a[2][2] <-- ch[8] <-- '9'
    It needs to output like this:

    123
    456
    789

    or if it was a square of 4:

    1234
    5678
    9123


    It's not a school project or anything, so it's not cheating. I'm just trying to learn java and this is giving me trouble because I don't have the answer to the problem.
    Last edited by shaq; 03-30-2011 at 01:40 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    One thing that helped me understand 2d arrays was to realize that an array is just an object filled with objects. So a 2 d array is just an array where each spot is a new array.

    What have you tried to solve this? We won't give you the answers here but we will help you to understand.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    You will need to use nested loops. Also a 2D array is simply a 1D array of arrays.
    Java Code:
    create 2D array
    intialise a counter
    loop {
        create a new 1D array
        loop {
            add counter to array
            increase counter
        }
        add 1D array to 2D array
    }

  4. #4
    shaq is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    I'm so confused. This is what I have so far:

    Java Code:
    //create 2D array
            char [] ch = d.toCharArray();
            char a[][] = new char[n][n];
           
            //intialise a counter
            int f = 0;
            //loop {
            for (int i = 0; i < n*n; n++){
                //add counter to array
                //increase counter
                f++;
                //add 1D array to 2D array
            }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    and???

    Do you plan on asking a question? Please don't ask "What do I do next?" or something similar. If you ask a specific question you will get a specific answer.

  6. #6
    shaq is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Am I supposed to have a for loop inside a for loop when setting the values?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Did you read the pseudocode I provided above?

  8. #8
    shaq is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    What I meant was, do I set the values in the inner for loop, or in the outer for loop?

  9. #9
    shaq is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Can you at least tell me if I have these loops set up right?

    n is the square root of the length of the original string. (also the length of the 1D array)

    Java Code:
    for (int i = 0; i < n*n; i++){
                for (int r = 0; r < n; r++){ 
                    
                }
            }

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Once again read the pseudocode. It might be slightly wrong as I misread what your were trying to do. Where does it say to add something to the array?

  11. #11
    shaq is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Ok, I got what I wanted to do working. I just have one more question. I uploaded a pic of the program, and I honestly have no clue what it wants me to do. I'm not asking for help with how to program it, I just don't understand the logic behind these patterns. Could someone possibly explain this a little better? Like what determines the direction in each "cell"?

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Why ask us? We didn't set the assignment!

  13. #13
    shaq is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Why answer if you aren't gonna help?

    Who the hell else am I supposed to ask? It's a problem from a contest and I didn't know how to do it, so now I'm trying to figure it out...

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    oh boo hoo!

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are being rude, he is simply saying it's not our assignment, we don't know the answer necessarily. Perhaps part of the challenge is understanding the question. How log did you take to think about the question?

  16. #16
    shaq is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    I don't expect you to know, but if you do then I would just appreciate a better explanation. I've been reading the paper all day. None of my friends get it, my teacher doesn't get it, I don't know what else to do.

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I honestly don't quite understand it either. Perhaps I'll think about it a bit and see if I can understand it, however; by getting obnoxious you probably lost a lot of attention. Maybe someone who could figure it out won't post and help you out.

    Try looking for a pattern.

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    How can I provide a better explanation than "I don't know because I didn't write the assignment. Go and ask the person who did"?

    As for being rude, you are the first person to use unacceptable language which I'm sure goes against the forum rules you agreed to.

  19. #19
    shaq is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Ok, I get it that you don't know, but do you really think saying "boo hoo" isn't gonna piss me off?

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by shaq View Post
    Ok, I get it that you don't know, but do you really think saying "boo hoo" isn't gonna piss me off?
    The problem is, you are here to get help, junky is not. If you want help you need to be nice. What does cursing him out accomplish? Perhaps he was being rude, but by you retaliating you only hurt yourself.

    That being said, where did you find this assignment? Is there anywhere you can get some advice from the person hosting the contest?

Page 1 of 2 12 LastLast

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  2. Help with an Array program please?
    By EternalSolitude in forum New To Java
    Replies: 3
    Last Post: 10-30-2008, 02:39 AM
  3. Replies: 2
    Last Post: 05-05-2008, 01:56 PM
  4. Array program help
    By adelgado0723 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  5. Need help with my 1st array program
    By Phobos0001 in forum New To Java
    Replies: 5
    Last Post: 03-22-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •