Write one dimensional array into two dimensional array

2022-02-22
2022-02-25
  • Vijolica555 - 2022-02-22

    Hi guys,

    I would appreciate advice on how to write one dimensional array into two dimensional array.

    I have one dimensional array (lets name it aWhole) and the length of this array is known. I need to separate this array in smaller groups so I divide length of one dimensional array with arbitrarily no. of groups and got new array aDivided with the lengths: Width and Groups. Anyone has an idea how to write first array into second one?
    Length is not necessary divided with Groups (in that case we round it to get whole no. of Width but idk what we do with the rest part of aWhole).

    VAR
    aWhole : ARRAY [0..Main.Length] OF REAL;
    aDivided : ARRAY [0..Main.Width,0..Main.Groups] OF REAL;
    END_VAR
    
     
  • sgronchi - 2022-02-24

    You have two options.

    First, you can do the "safe way" with two FOR cycles (assuming Main.Length + 1 = (Main.Width + 1) * (Main.Groups + 1) based on how you declared the two arrays)

    FOR i := 0 TO Main.Width DO
        FOR j := 0 TO Main.Groups DO
            aDivided[i, j] := aWhole[i * (Main.Groups+1) + j];
        END_FOR
    END_FOR
    

    Second, you can use CAA Memory Library functions like

    MEM.MemMove(pSource := ADR(aWhole[0]), pDestination := ADR(aDivided[0,0]), uiNumberOfBytes := Main.Length+1);
    
     
    • Vijolica555 - 2022-02-25

      Thank you for suggestion. I managed to write 1D array into 2D as needed.

       
  • peterned - 2022-02-25

    I think you can simply declare both at the same fixed memory address, e.g.

    aWhole AT %MB100: ARRAY [0..Main.Length] OF REAL;
    aDivided AT %MB100: ARRAY [0..Main.Width,0..Main.Groups] OF REAL;
    

    and use whichever you want, a change in one will automatically be reflected in the other.

     

    Last edit: peterned 2022-02-25

Log in to post a comment.