0

I'm trying to make an encryption program where the user enters a message and then converts the "letters into numbers".

For example the user enters a ABCD as his message. The converted number would be 1 2 3 4 and the numbers are stored into a one dimensional integer array. What I want to do is be able to put it into a 2x2 matrix with the use of two dimensional arrays.

Here's a snippet of my code:

       int data[] = new int[] {10,20,30,40};

       *for(i=0;i<2;i++)
        {
            for(j=0;j<2;j++)
            {
                for (int ctr=0; ictr<data.length(); ictr++){
                a[i][j] = data[ctr];}
            }
        }

I know there's something wrong with the code but I am really lost.

How do I output it as the following?

10 20 
30 40

(instead of just 10,20,30,40)

Jason C
  • 38,729
  • 14
  • 126
  • 182
Christine
  • 13
  • 5

4 Answers4

1

Here's one way of doing it. It's not the only way. Basically, for each cell in the output, you calculate the corresponding index of the initial array, then do the assignment.

int data[] = new int[] {10, 20, 30, 40, 50, 60};
int width = 3;
int height = 2;

int[][] result = new int[height][width];

for(int i = 0; i < height; i++) {
    for(int j = 0; j < width; j++) {
        result[i][j] = data[i * width + j];
    }
}
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
1

Seems like you want to output a 2xn matrix while still having the values stored in a one-dimensional array. If that's the case then you can to this:

Assume the cardinality m of your set of values is known. Then, since you want it to be 2 rows, you calculate n=ceil(m/2), which will be the column count for your 2xn matrix. Note that if m is odd then you will only have n-1 values in your second row.

Then, for your array data (one-dimension array) which stores the values, just do

   for(i=0;i<2;i++)    // For each row
   {
       for(j=0;j<n;j++)    // For each column, 
                           // where index is baseline+j in the original one-dim array
       {
           System.out.print(data[i*n+j]);
       }
   }

But make sure you check the very last value for an odd cardinality set. Also you may want to do Integer.toString() to print the values.

StoneBird
  • 1,900
  • 13
  • 12
0

Your code is close but not quite right. Specifically, your innermost loop (the one with ctr) doesn't accomplish much: it really just repeatedly sets the current a[i][j] to every value in the 1-D array, ultimately ending up with the last value in the array in every cell. Your main problem is confusion around how to work ctr into those loops.

There are two general approaches for what you are trying to do here. The general assumption I am making is that you want to pack an array of length L into an M x N 2-D array, where M x N = L exactly.

The first approach is to iterate through the 2D array, pulling the appropriate value from the 1-D array. For example (I'm using M and N for sizes below):

for (int i = 0, ctr = 0; i < M; ++ i) {
    for (int j = 0; j < N; ++ j, ++ ctr) {
        a[i][j] = data[ctr];
    }
} // The final value of ctr would be L, since L = M * N.

Here, we use i and j as the 2-D indices, and start ctr at 0 and just increment it as we go to step through the 1-D array. This approach has another variation, which is to calculate the source index explicitly rather than using an increment, for example:

for (int i = 0; i < M; ++ i) {
    for (int j = 0; j < N; ++ j) {
        int ctr = i * N + j;
        a[i][j] = data[ctr];
    }
}

The second approach is to instead iterate through the 1-D array, and calculate the destination position in the 2-D array. Modulo and integer division can help with that:

for (int ctr = 0; ctr < L; ++ ctr) {
    int i = ctr / N;
    int j = ctr % N;
    a[i][j] = data[ctr];
}

All of these approaches work. Some may be more convenient than others depending on your situation. Note that the two explicitly calculated approaches can be more convenient if you have to do other transformations at the same time, e.g. the last approach above would make it very easy to, say, flip your 2-D matrix horizontally.

Jason C
  • 38,729
  • 14
  • 126
  • 182
0
check this solution, it works for any length of data

public class ArrayTest 
    {
        public static void main(String[] args) 
        {
            int data[] = new int[] {10,20,30,40,50};
            int length,limit1,limit2;
            length=data.length;
            if(length%2==0)
            {
                limit1=data.length/2;
                limit2=2;
            }
            else
            {
                limit1=data.length/2+1;
                limit2=2;
            }

            int data2[][] = new int[limit1][limit2];

            int ctr=0;

            //stores data in 2d array
             for(int i=0;i<limit1;i++)
             {
                 for(int j=0;j<limit2;j++)
                 {
                     if(ctr<length)
                     {
                         data2[i][j] = data[ctr];
                         ctr++;
                     }
                     else
                     {
                         break;
                     }
                 }
             }

             ctr=0;
             //prints data from 2d array
             for(int i=0;i<limit1;i++)
             {
                 for(int j=0;j<limit2;j++)
                 {
                     if(ctr<length)
                     {
                         System.out.println(data2[i][j]);
                         ctr++;
                     }
                     else
                     {
                         break;
                     }
                 }
             }
        }

    }