john smith
john smith

Reputation: 21

How to fill the cell of 2D arrays by fixing the columns and go through rows

I have one 2D array like this

share [] [] = new share [shares][m+1]; 

when

int m= secret.length;

and where

byte [] secret = one.readFile () ;

so m has the size of my size in bytes Now when i go through columns of 2D array and each column represent one byte of my file so let say if file text is hello column one represent h , second column represent e and so on

The rows represent shares which mean if i put to main as value of shares as 7 we will have 7 rows. Now what i want to do is something like that

for (byte coeff: secret ) { // so now i know i am fixing rows 

for (int j=0 ; j<shares; j++ ) { // now i am itereating through rows and my column is fixed 

} // close inner loop

} // close loop which go through rows

now what i want to do and i am not able to do is i want to fill each cell of 2D array

and i wanted to use

share [j] [//here i do not hot what to put] = (byte) evaluate(share[j][0], a);

where (byte) evaluate(share[j][0], a); is the value of polynomial i want to put on each cell of 2D array and i want to fill it as i mentioned above by fixing columns and go through rows . H. If someone can help me with that would be perfect .

Upvotes: 0

Views: 458

Answers (2)

john smith
john smith

Reputation: 21

It was really helpful for me. Now I have done a few changes to my method.

public byte[][] createShares(byte[] secret, int n, int k, Random rnd) {

    //n is the number of  how many parts each byte of the file will be shared
   // k is the number needed to reconstruct each byte of the file
  //byte[] secret is the file itslef , in our case will be each byte of the file

byte[][] share = new byte[n][m + 1];


System.out.println("This is the two dimensional array i also send you to photo but is not filled" +Arrays.deepToString(share));

       for (int j=0;j<m;j++) { // fix column

          byte coeff=secret[j]; 

        System.out.println("Fix the column for each of the byte of file" +coeff); // this will take bytes of files one by one

       // this one will take the coefficients of our polynom . Let say as it is in main method k=3 than this will produce
       // a vector of {a0,a1,a2} where a0=secret;

        byte[] a = null;


        byte[] a = new byte[k]; // the coefficients of the polynomail

        int l = a.length; // how coefficients we will have 

        System.out.println ("The number of coefficients of polynomial are : " + l);

        rnd.nextBytes(a);

        a[0]=coeff; // coeff is our byte so it is our secret we will share it will take for each iteration h,e,l,l,o in decimal value

       System.out.println ("The secret is:" +a[0]); // print each byte of file in decimal

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

       for (int u=0; u<n;u++)
       share[u][0] = (byte) (u + 1);


       // now we are in the position when we are giving values to each row . They will take values 1,2,...8
       //for the moment we have to fix value of the first row which is n =1
       // now we are in the position of first row and first column and now we must calculate the polynomial value
        share [i][j]= (byte) evaluate (share[u][0],a);

       // then when we will finish the code we will put up the cycle   for (int i=0;i<n;i++) will come in action and now we will be
       // in the position of first byte (column) and second row and will calculate again the polynomial


       }

       }

return share;
}

Upvotes: 0

red-goblin
red-goblin

Reputation: 41

So if I understand your problem correctly, you want to iterate over the array by first going through each column and then going through each row. And you want to set the value of each element of the array as evaluate(share[j][0], a).

If so, you can use the following logic to set the value of each element of the array:

for(int i=0; i<m+1; i++) { // iterating over columns
    for(int j=0; j<shares; j++) { //iterating over rows
        share[j][i] = evaluate(share[j][0], a);
    }
}

Referring back to the comment by the postee, the outermost for loop is iterating over each occurrence of a byte in the array share. As per the original comment, int m = secret.length where byte[] secret = one.readLine(). Effectively, this means int m = one.readLine().length.

So when I say for(int i=0; i<m+1; i++) {...}, I am saying for each occurrence of the byte in the file (defined by the value of the variable i), execute the inner for loop.

You can replace the outermost for loop with for(byte coeff: secret) {...}, and it will have the same effect of looping the same number of times. The second approach is called enhanced for statement. But with the second approach, there is an overhead of determining the position of coeff in the array secret. And you need that to set the value of the array (I see your comment [//here i do not hot what to put] :)). The first approach will easily resolve this issue.

In my humble opinion, your approach of looping over the array is very useful if you want to use the value of the element in that array at that position. In this case, you are really only interested in the positional value of the element, and you will be better off with the first approach.

Upvote my answer if it helped :)

Upvotes: 1

Related Questions