#1x9 to 3x3 array

1 messages · Page 1 of 1 (latest)

still kestrel
#

I feel like I'm so close but I'm missing something. currently stumped any help?

public class Feb15HWQ2{
public static void main(String args[])
{
int A [] = {1,2,3,4,5,6,7,8,9};
int B [][] = new int [3][3];

  int i=0,k,j;

  for(k=0;k<3;k++)  
     for(j=0;j<3;j++)
     {
        B[k][j] = A[k*3 + j];
        k++;
     }
     
  for(k=0;k<3;k++)
  {
     for(j=0;j<3;j++)
        System.out.print(B[k][j]+" ");
     System.out.println(); 
  }

}
}

Current output:
1 0 0
0 5 0
0 0 9

Output I need:
1 2 3
4 5 6
7 8 9

austere cairnBOT
#

This post has been reserved for your question.

Hey @still kestrel! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

little cove
#

If i was you, i would just use one for loop and use modules when accessing the 2d array’s index

still kestrel
#

i tried but my professor wants us to do it this wayfor some reason. we need the like 3 to 4 for loops. its annoying. i thought i had it but its giving me 0s on the spots where i need the numbers

#

can you see the error in my current program?

#

or how to fix it

dusk oracle
#

do not do k++ in the second for loop

#

try it

still kestrel
#

thank you lmao. that worked.

#

i knew i was close