0

I have made 2-D array to store patterns[user defined] and it's features [user can decide No of features for each pattern].I have to divide this structure into two parts TR1 and TR2 such that half features for each pattern goes in TR1 and remaining half goes in TR2. Code for the same

 int n;//No of Patterns
 int f=0;;//No of Features


 System.out.println("Enter No of Patterns:");
 n=Integer.parseInt(input.readLine());

 //2-D array to store Features
 int pattern[][]= new int[n][20];
 int tr1[][]=new int[n][20];//TR1
 int tr2[][]=new int[n][20];//TR2

//No of Features for each Pattern
 for(int i=0;i<n;i++)//NO of Features for each Pattern
 { 
     System.out.println("Enter No of Features for Pattern "+(i+1)+" : ");
     f=Integer.parseInt(input.readLine());
     pattern[i]=new int[f];
     tr1[i]=new int[f/2];//definining size of TR1 
     tr2[i]=new int[f/2];//definining size of TR2
 }

//Features of each pattern
for(int i=0;i<n;i++)
 {
    System.out.println("Enter Features for Pattern "+(i+1)+" : ");
    for(int j=0;j<pattern[i].length;j++)
    {
    pattern[i][j]=Integer.parseInt(input.readLine());

    }
 }
//Split into TR1 & TR2 
for(int i=0;i<n;i++)
 {
    for(int j=0;j<pattern[i].length/2;j++)
    {
    tr1[i][j]=pattern[i][j];
    }   
    int x=0;//Fill TR2 from 0 index
    for(int j=pattern[i].length;j<pattern[i].length;j++)
    {
    tr2[i][x]=pattern[i][j];
            x++;
    }
 }




//Print Features of each pattern
for(int i=0;i<n;i++)
 {

    for(int j=0;j<pattern[i].length;j++)
    {
    System.out.print(" "+pattern[i][j]+" ");
    }
    System.out.println();
 }

 //Print TR1
 for(int i=0;i<n;i++)
 {

    for(int j=0;j<tr1[i].length;j++)
    {
    System.out.print(" "+tr1[i][j]+" ");

    }
    System.out.println();
 }

     //Print TR2
 for(int i=0;i<n;i++)
 {

    for(int j=0;j<tr2[i].length;j++)
    {
    System.out.print(" "+tr2[i][j]+" ");

    }
    System.out.println();
 }

The issue is all the values in TR2 become 0 in output.But TR1 output is correct.

2
  • for(int j=pattern[i].length;j<pattern[i].length;j++) this line is the issue
    – DHall
    Apr 18, 2014 at 12:06
  • for(int j=pattern[i].length/2;j<pattern[i].length;j++)//done this but give array of bound exception on tr2[i][x]=pattern[i][j]; line in the code
    – GkWizZ
    Apr 18, 2014 at 12:08

1 Answer 1

0

In the for loop the condition,j < pattern[i].length is false because:

int j = pattern[i].length;

Since the condition is false for the first time the statements inside for loop is not executing at all.That's why the elements of TR2 remains uninitialized that's why elements of TR2 becomes 0. .

for(int j=pattern[i].length;j<pattern[i].length;j++)
{
  tr2[i][x]=pattern[i][j];  //Never executes
  x++;                      //Never Executes 
}

Checkout the link it'll help. java-how-to-split-a-2d-array-into-two-2d-arrays

4
  • @Ashraful Haque``for(int j=pattern[i].length/2;j<pattern[i].length;j++) This should work when int j=pattern[i].length/2 but it gives array out of bound exception
    – GkWizZ
    Apr 18, 2014 at 13:03
  • Solved :D Problem was here tr1[i]=new int[f/2]; tr2[i]=new int[f/2];// when f is odd , array out of bound error strikes
    – GkWizZ
    Apr 18, 2014 at 14:08
  • Yeah,got it.If odd what'll you do now? Apr 18, 2014 at 14:26
  • if(f%2==0) { int half=f/2; tr1[i]=new int[half];tr2[i]=new int[half];} else { int half=f/2; tr1[i]=new int[half];tr2[i]=new int[half+1];}This solved my error for odd
    – GkWizZ
    Apr 18, 2014 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.