# 2D array program

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-30-2011, 01:35 AM
shaq
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:

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.
• 03-30-2011, 01:42 AM
sunde887
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.
• 03-30-2011, 01:44 AM
Junky
You will need to use nested loops. Also a 2D array is simply a 1D array of arrays.
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 }```
• 03-30-2011, 02:13 AM
shaq
I'm so confused. This is what I have so far:

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         }```
• 03-30-2011, 02:17 AM
Junky
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.
• 03-30-2011, 02:18 AM
shaq
Am I supposed to have a for loop inside a for loop when setting the values?
• 03-30-2011, 02:30 AM
Junky
Did you read the pseudocode I provided above?
• 03-30-2011, 02:34 AM
shaq
What I meant was, do I set the values in the inner for loop, or in the outer for loop?
• 03-30-2011, 02:41 AM
shaq
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)

Code:

```for (int i = 0; i < n*n; i++){             for (int r = 0; r < n; r++){                             }         }```
• 03-30-2011, 03:29 AM
Junky
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?
• 03-30-2011, 03:51 AM
shaq
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"?
http://i56.tinypic.com/wl36o.jpg
• 03-30-2011, 04:02 AM
Junky
Why ask us? We didn't set the assignment!
• 03-30-2011, 04:07 AM
shaq
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...
• 03-30-2011, 04:08 AM
Junky
oh boo hoo!
• 03-30-2011, 04:21 AM
sunde887
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?
• 03-30-2011, 04:26 AM
shaq
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.
• 03-30-2011, 04:32 AM
sunde887
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.
• 03-30-2011, 04:36 AM
Junky
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.
• 03-30-2011, 12:01 PM
shaq
Ok, I get it that you don't know, but do you really think saying "boo hoo" isn't gonna piss me off?
• 03-30-2011, 12:32 PM
sunde887
Quote:

Originally Posted by shaq
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?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last