Calculations with arrays with a for loop in java

651 views Asked by At

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 foror a do while loop or with any other method?

6

There are 6 answers

1
Pritam Banerjee On BEST ANSWER

Try this:

    int pag_size = 4;
    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];

    for(int i=0;i<pag.length;i++){
           pag[i]=i;
    }

    for (int i = 0 ; i <  pag_array_size; i++){
        calc1[i] = marc[i/pag_size]* pag_size  + pag[i%pag_size];
        System.out.println(calc1[i]);
    }

Sleek and simple.

You cannot use two for loops. Instead you will have to use '%' and &.

0
davidxxx On

If I follow this pattern :

    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];

You could do a simple loop and use a modulo to reinit the index for marc and pag arrays when you have filled a page:

int pagIndex = 0;
int marcIndex = 0;
for (int i = 0; i < calc1.length; i++){
     calc1[i] = marc[marcIndex] * pag_size + pag[pagIndex++];

   if (i % pag_size == 0){
     pagIndex = 0;
     marcIndex++;
   }
}
4
CraigR8806 On

This can be done with nested for-loops like so:

int calcAccum = 0;
for(int i=0;i<marc.length;i++){
    for(int j=0;j<4;j++){
        calc1[j+calcAccum] = marc[i] * pag_size + pag[j];
    }
    calcAccum+=4;
}
0
Hawke On

You should use for loop in for loop here is some e.g(pseudocode)
for(i in marc){ for(j in pag){ //your code goes here } }

0
serge On

Try this:

for (int i = 0; i < pag.length; i++) 
    for (int j = 0; j < marc.length; j++) 
        calc1[i * pag.length + j] = marc[i] * pag.length + pag[j];

Very simple :)

0
Jorn Vernee On

You could use streams:

int pag_size = 4;

int[] pag = IntStream.range(0, pag_size).toArray();
int[] marc = {5, 6, 1, 2};      

int[] calc1 = Arrays.stream(marc)
    .map(i -> i * pag_size)
    .flatMap(i -> Arrays.stream(pag).map(i2 -> i + i2))
    .toArray();

System.out.println(Arrays.toString(calc1));

Output:

[20, 21, 22, 23, 24, 25, 26, 27, 4, 5, 6, 7, 8, 9, 10, 11]