1. Member
Join Date
Mar 2011
Posts
10
Rep Power
0

## 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. 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. 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 {
increase counter
}
add 1D array to 2D array
}```

4. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
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++){
//increase counter
f++;
//add 1D array to 2D array
}```

5. 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. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
Am I supposed to have a for loop inside a for loop when setting the values?

7. Did you read the pseudocode I provided above?

8. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
What I meant was, do I set the values in the inner for loop, or in the outer for loop?

9. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
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. 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. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
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. Why ask us? We didn't set the assignment!

13. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
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. oh boo hoo!

15. 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. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
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. 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. 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. Member
Join Date
Mar 2011
Posts
10
Rep Power
0
Ok, I get it that you don't know, but do you really think saying "boo hoo" isn't gonna piss me off?

20. 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?

Page 1 of 2 12 Last

#### Posting Permissions

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