Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Reverse a 3D array?

  • 21-12-2010 9:56am
    #1
    Registered Users, Registered Users 2 Posts: 46


    Hey all,

    I've done the reverse a string before but does anyone have a suggestion for reversing this:
    ushort[,,] TempArray = new ushort[700, 9, 2049];
    
    for (j = 0; j < 1; j++) //lines
    {
        for (i = 0; i < 9; i++)       //elements
        {
             for (k = 0; k < 2048; k++) //lines
             {
                  TempArray[j, i, k] = CamData[bindex].Data[j, i, k];
             }
        }
    }
    

    (Camdata[].Data[] is already created and I am copying it into the TempArray[] to revese it)

    Any idea's appriciated.


Comments

  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭Giblet


    You have the lengths of the arrays, and CamData is full i suppose?

    Hint: 2048 - k.


  • Registered Users, Registered Users 2 Posts: 46 hugejeans


    Cheers Giblet,

    Yes CamData is full, its filled from a seperate buffer, I tried this:

    [code]
    ushort[,,] TempArray = new ushort[700, 9, 2049];
    x = 0;
    y = 0;
    z = 0;
    for (j = 1; j > 0; j--) //lines
    {
    for (i = 9; i > 0; i--) //elements
    {
    for (k = 2048; k > 0; k--) //lines
    {
    TempArray[j, i, k] = CamData[bindex].Data[x, y, z];
    z++;
    }
    y++;
    }
    x++
    }
    [\CODE]
    But getting a System.IndexOutOfRangeException, although stepping through the code in debug the index into the arrays look fine.


  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭Giblet


    for (k = 0; k < 2048; k++) //lines
             {
                  TempArray[j, i, k] = CamData[bindex].Data[1-j, 8-i, 2048 - k];
             }
    
    

    That's the basic idea, but you have some other errors in there, but they're small index ones.

    Could probably get the lengths of each dimension programmatically as well rather than hardcoding values. And remember ,Arrays are 0 based.
    So an array with a size of 9, is referenced 0-8


  • Registered Users, Registered Users 2 Posts: 46 hugejeans


    Cheers Giblet,

    I noticed the index problem scrolling through the the array values, silly mistake to make.

    how would you suggest I get the lengths of each dimension programmatically, sizeof()?


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    hugejeans wrote: »
    Cheers Giblet,

    I noticed the index problem scrolling through the the array values, silly mistake to make.

    how would you suggest I get the lengths of each dimension programmatically, sizeof()?

    I don't think sizeof() is applicable to Java. See, composite objects in Java are not aligned in memory as simply as say a load of structs ala C. I think the handiest way is to just use recursion. You could adapt something like the code below. Please note, I did not consider error checking below. Purely for illustration imo. Use at your own risk. I don't know off the top of my head how to do this in Java, but I reckon it should be somewhat similar. Maybe check out the Java/Oracle docs might have the answer?
    #include <stdio.h>
    #define LENGTH 6
    int reverse(int *ptr, int length);
    
    int main(int argc, char **argv) {
            int array[LENGTH] = { 5,4,3,2,1,0 };
            reverse(array, LENGTH-1);
            return 0; /*OS expects 0 as a sucess exit code. At least for Win/Unix*/
    }
    
    int reverse(int *ptr, int length) {
            if (length == 0) { /*Last int to print. Print it, then exit this func*/
                    printf("%d\n", ptr[length]);
                    return 0; /*Return 0 to indicate success*/
            }
            printf("%d\n", ptr[length]);
            reverse(ptr, length-1); /*Call the function again with a smaller problem set*/
            return 1; /*Assume 1 for non exit value of this function*/
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 46 hugejeans


    Thanks Naikon,

    Not Java, C#, I spose I should have mentioned that. I'm more of a C programmer so not really up to speed on the functions available in C#.

    Nice example on using recursion to reverse an array, perfect for an interview :)

    I'm trying to modify someelses code, very poorly designed code (15 thousand lines of code in one file).

    Now that I have the 3D array reversing its not exactly what I need to do, I think I only need to reverse one of the dimensions. Will have to play arround with it a bit more.

    Cheers lads.


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    hugejeans wrote: »
    Thanks Naikon,

    Not Java, C#, I spose I should have mentioned that. I'm more of a C programmer so not really up to speed on the functions available in C#.

    Nice example on using recursion to reverse an array, perfect for an interview :)

    I'm trying to modify someelses code, very poorly designed code (15 thousand lines of code in one file).

    Now that I have the 3D array reversing its not exactly what I need to do, I think I only need to reverse one of the dimensions. Will have to play arround with it a bit more.

    Cheers lads.

    No problem. Do as you wish with it, it's not there is anything copyrightable in it:pac: I don't know much about C# I admit, but it's a cousin of C, so there you go. 15K lines? Sorry to hear of your troubles:( Good luck.


  • Registered Users, Registered Users 2 Posts: 89 ✭✭tehjimmeh


    hugejeans wrote: »
    Cheers Giblet,

    I noticed the index problem scrolling through the the array values, silly mistake to make.

    how would you suggest I get the lengths of each dimension programmatically, sizeof()?
    What you want is GetLength(), which returns the length of the dimension passed to it. i.e. GetLength(0) returns the length of the first dimension, GetLength(1) returns the length of the second dimension etc.

    So to loop through a 3d array called 'arr' (where you want more control over the elements than a simple foreach), you would do:
     for(int i=0; i < arr.GetLength(0); i++)
         for(int j=0; j < arr.GetLength(1); j++)
             for(int k=0; k < arr.GetLength(2); k++)
                 .........
    

    Naikon: there is sizeof in C# (http://msdn.microsoft.com/en-us/library/eahchzkf(v=vs.71).aspx), but it can only be used in an unsafe context. The unsafe keyword in C# allows you to use pointers, like in C (http://msdn.microsoft.com/en-us/library/chfa2zb8(v=vs.71).aspx).

    But obviously, it's not something you'd be using routinely for just finding out the length of an array.


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    tehjimmeh wrote: »
    What you want is GetLength(), which returns the length of the dimension passed to it. i.e. GetLength(0) returns the length of the first dimension, GetLength(1) returns the length of the second dimension etc.

    So to loop through a 3d array called 'arr' (where you want more control over the elements than a simple foreach), you would do:
     for(int i=0; i < arr.GetLength(0); i++)
         for(int j=0; j < arr.GetLength(1); j++)
             for(int k=0; k < arr.GetLength(2); k++)
                 .........
    

    Naikon: there is sizeof in C# (http://msdn.microsoft.com/en-us/library/eahchzkf(v=vs.71).aspx), but it can only be used in an unsafe context. The unsafe
    keyword in C# allows you to use pointers, like in C (http://msdn.microsoft.com/en-us/library/chfa2zb8(v=vs.71).aspx).

    But obviously, it's not something you'd be using routinely for just finding out the length of an array.

    Cool, I didn't know C# allows pointers to be used in such a direct manner. I was under the impression that managed langs like C#/Java just outright don't allow it.


Advertisement