How do i initialize/fill my 2-D array?

Page 1 of 1

3 Replies - 311 Views - Last Post: 01 March 2013 - 01:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313912&amp;s=1546193ba35e6a0c09c4c298a433b164&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 mfaizi

Reputation: 0
• Posts: 3
• Joined: 28-February 13

How do i initialize/fill my 2-D array?

Posted 28 February 2013 - 10:22 PM

Hi, this is a homework problem. I do not understand how to fill each column separatly. There are 24 rows and two columns. The left column is supposed to hold multiples of pi/2, and the right column is supposed to hold the tan of the multiple. Here is my coding, it fills both columns with multiples of pi/2. I am a beginner so the simplest answers are appreciated, thank you
```public class ArrayFunc {

[public static void main(String[] args) {
double [][] array = new double[24][2];

for(int i=0; i<24; i++){
for(int j =0; j<2; j++){

array[i][j] = (i*(Math.PI/2)) ;

System.out.print(array[i][j] + "\t");
}
System.out.println();

}
}

}
```

This post has been edited by Atli: 28 February 2013 - 10:24 PM
Reason for edit:: Added [code]...[/code] tags.

Is This A Good Question/Topic? 0

Replies To: How do i initialize/fill my 2-D array?

#2 CasiOo

• D.I.C Lover

Reputation: 1490
• Posts: 3,317
• Joined: 05-April 11

Re: How do i initialize/fill my 2-D array?

Posted 28 February 2013 - 11:42 PM

Easy enough
Just simply remove the nested loop
You know what should be in index 0 and 1
```public class ArrayFunc {
public static void main(String[] args) {
double[][] array = new double[24][2];

for(int i=0; i<array.length; i++) {
array[i][0] = i * (Math.PI/2);
array[i][1] = //tan of the multiple
System.out.print(array[i][0] + "\t");
System.out.print(array[i][1] + "\t");
System.out.println();
}
}
}

```

#3 cassiopeia

Reputation: 10
• Posts: 86
• Joined: 03-April 11

Re: How do i initialize/fill my 2-D array?

Posted 28 February 2013 - 11:45 PM

i post a bit late from CasiOo. since the answer is same, so i remove mine.

This post has been edited by cassiopeia: 28 February 2013 - 11:54 PM

#4 mfaizi

Reputation: 0
• Posts: 3
• Joined: 28-February 13

Re: How do i initialize/fill my 2-D array?

Posted 01 March 2013 - 01:19 PM

CasiOo, on 28 February 2013 - 11:42 PM, said:

Easy enough
Just simply remove the nested loop
You know what should be in index 0 and 1 />
```public class ArrayFunc {
public static void main(String[] args) {
double[][] array = new double[24][2];

for(int i=0; i<array.length; i++) {
array[i][0] = i * (Math.PI/2);
array[i][1] = //tan of the multiple
System.out.print(array[i][0] + "\t");
System.out.print(array[i][1] + "\t");
System.out.println();
}
}
}

```

CasiOo, on 28 February 2013 - 11:42 PM, said:

Easy enough
Just simply remove the nested loop
You know what should be in index 0 and 1 />
```public class ArrayFunc {
public static void main(String[] args) {
double[][] array = new double[24][2];

for(int i=0; i<array.length; i++) {
array[i][0] = i * (Math.PI/2);
array[i][1] = //tan of the multiple
System.out.print(array[i][0] + "\t");
System.out.print(array[i][1] + "\t");
System.out.println();
}
}
}

```

Thanks CasiOo this was very helpful