Search Unity

Finding sum of first row of Vector3 jagged array in C#?

Discussion in 'Scripting' started by JovanD, Apr 15, 2016.

  1. JovanD

    JovanD

    Joined:
    Jan 5, 2014
    Posts:
    205
    Okay so i have jagged array let's say vectors[z][x] and i want to sum up first row in to sum[x] how do i do it?

    Code (CSharp):
    1. for (int z = 0; z < vectors.Length; z++)
    2.         {  
    3.                 sum += vectors[z];        
    4.         }
    I tried writing it like that, but i get
    error CS0019: Operator `+=' cannot be applied to operands of type `UnityEngine.Vector3[]' and `UnityEngine.Vector3[]'
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    you're accessing the array of arrays, rather than the first row of it... first row is at index 0.

    Code (csharp):
    1.  
    2. for (int i = 0; i < vectors[0].Length; i++)
    3. {
    4.     sum += vectors[0][i];
    5. }
    6.  
    Or rather, generalized into a function:

    Code (csharp):
    1.  
    2. Vector3 SumRow(Vector3 row)
    3. {
    4.     Vector3 sum;
    5.     for(int i = 0; i < row.Length; i++)
    6.         sum += row[i];
    7.     return sum;
    8. }
    9.  
    10.  
    11. void Foo()
    12. {
    13.     Vector3 sum1 = SumRow(vectors[0]); //sum first row
    14.     Vector3 sum2 = SumRow(vectorw[1]); //sum second row
    15. }
    16.  
     
  3. JovanD

    JovanD

    Joined:
    Jan 5, 2014
    Posts:
    205
    Okay guess i mixed up what row means, i sort of meant the first level or dimension, i need to sort of sum up all the rows i guess?
    All the sub arrays are same lenght and they are the ones i need summed up in to one array, so i don't have arrays of arrays, but just arrays, if that makes sense?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    I have no idea what you're attempting to describe...

    which is probably why it's hard for you to code it.

    I've found that if you have a hard time explaining to another programmer what you want done, you're going to have an even harder time explaining it to a computer.

    Furthermore, this is also why I think jagged arrays are usually a pretty bad design. They create convolutions that are hard to imagine in ones head (multi-dimensional arrays aren't as bad... but still are flawed). I honestly, in 10 years of dev, never found a true need for jagged arrays in my work.
     
  5. JovanD

    JovanD

    Joined:
    Jan 5, 2014
    Posts:
    205
    Managed to get it working like this, hope it helps some one.
    Code (CSharp):
    1. for (int i = 0; i < ChangedPos[0].Length; i++)
    2.         {
    3.             Vector3 sum = new Vector3(0,0,0);
    4.             for (int e = 0; e < ChangedPos.Length; e++)
    5.             {
    6.                 sum += ChangedPos[e][i];
    7.             }
    8.             objects[i].transform.position = sum + storedPos[i];
    9.         }
    I wouldn't use jagged arrays if i didn't have to, but i think this is most efficient design, or with least lines of code.
    I have array of objects that influence another array of objects, so i need to test whole array against each member of another array, that's why i thought jagged arrays would fit the job.
     
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    If all your rows are the same length then your array is not really "jagged". Jagged means you have an array of arrays that are different lengths. Your code above would fail if it were actually jagged; you'd need to swap the for loops like this to make it work:
    Code (csharp):
    1. for (int e = 0; e < ChangedPos.Length; e++) {
    2.     for (int i=0; i < ChangedPos[e].Length; i++) {
     
    JovanD likes this.
  7. JovanD

    JovanD

    Joined:
    Jan 5, 2014
    Posts:
    205
    For some reason i thought array of arrays are called jagged arrays(being noob is probably a reason :D)
    Didn't really even pay attention to the meaning of the name, but now it makes sense why they're called like that.
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    No, it's still called jagged when you have an array of arrays. Because the arrays are still capable of being different lengths, even if they aren't different lengths in your specific use case. It's intended to just be a shorten name for "array of arrays" which is really cumbersome to say.

    The name is opposed to a multidimensional array, where any given dimension must be flat.