I'm making a calculation in java with arrays here's the code, I have 3 arrays:
int pag_size;
int pag_array_size=pag_size*4;
int[] pag = new int [pag_size];
int[] marc = {5, 6, 1, 2};
int[] calc1 = new int[pag_array_size];
and I'm adding the values to pag[];
with a for loop
for(int i=0;i<pag.length;i++){
pag[i]=i;
}
I'm saving the calculations in calc1
in this case pag_size=4;
so pag_array_size=16;
so I need 16 calculation
and here is the calculations made manually:
calc1[0] = marc[0] * pag_size+ pag[0];
calc1[1] = marc[0] * pag_size + pag[1];
calc1[2] = marc[0] * pag_size + pag[2];
calc1[3] = marc[0] * pag_size + pag[3];
calc1[4] = marc[1] * pag_size + pag[0];
calc1[5] = marc[1] * pag_size + pag[1];
calc1[6] = marc[1] * pag_size + pag[2];
calc1[7] = marc[1] * pag_size + pag[3];
calc1[8] = marc[2] * pag_size + pag[0];
calc1[9] = marc[2] * pag_size+ pag[1];
calc1[10] = marc[2] * pag_size + pag[2];
calc1[11] = marc[2] * pag_size + pag[3];
calc1[12] = marc[3] * pag_size + pag[0];
calc1[13] = marc[3] * pag_size + pag[1];
calc1[14] = marc[3] * pag_size + pag[2];
calc1[15] = marc[3] * pag_size + pag[3];
and the output of the calculations is
20 21 22 23 24 25 26 27 4 5 6 7 8 9 10 11
How can I do the calculations with a for
or a do while
loop or with any other method?
Try this:
Sleek and simple.
You cannot use two for loops. Instead you will have to use '%' and
&
.