Labeling Jagged (Multi-D) Arrays

Discussion in 'C++' started by Cant Think Today, Sep 3, 2004.

  1. I have multi-dimesional arrays that can be specifed by the user, e.g

    1,2,3,4,5
    1,2,3,4,5,6,7,8,9,10
    1,2,3,4,5,6

    I think a bit of code that will iterate over these arrays to print out
    the element indices for each unique element in the N-dimensional
    array. E.g. for the above

    1:1:1
    1:1:2
    1:1:3
    1:1:4

    So what is the loop with the printf that will print the line to std???
    This is probably simple as hell but its late and I can't think!!
     
    Cant Think Today, Sep 3, 2004
    #1
    1. Advertising

  2. Cant Think Today

    Howard Guest

    "Cant Think Today" <> wrote in message
    news:...
    > I have multi-dimesional arrays that can be specifed by the user, e.g
    >
    > 1,2,3,4,5
    > 1,2,3,4,5,6,7,8,9,10
    > 1,2,3,4,5,6
    >
    > I think a bit of code that will iterate over these arrays to print out
    > the element indices for each unique element in the N-dimensional
    > array. E.g. for the above
    >
    > 1:1:1
    > 1:1:2
    > 1:1:3
    > 1:1:4
    >
    > So what is the loop with the printf that will print the line to std???
    > This is probably simple as hell but its late and I can't think!!


    I just don't see what the pattern is in that output. I also don't
    understand the question or the examples above. I see three lists. I don't
    see any multi-dimensional arrays, or see how those lists are supposed to
    "specify" multi-dimensional arrays. And how do the four colon-delimited
    lines of output relate to the three lists?

    Have you at least *attempted* to write the code you're asking us to help you
    with? (Or do you just want us to do your work?)

    -Howard
     
    Howard, Sep 3, 2004
    #2
    1. Advertising

  3. "Howard" <> wrote in message news:<kN2_c.300732$>...
    > "Cant Think Today" <> wrote in message
    > news:...
    > > I have multi-dimesional arrays that can be specifed by the user, e.g
    > >
    > > 1,2,3,4,5
    > > 1,2,3,4,5,6,7,8,9,10
    > > 1,2,3,4,5,6
    > >
    > > I think a bit of code that will iterate over these arrays to print out
    > > the element indices for each unique element in the N-dimensional
    > > array. E.g. for the above
    > >
    > > 1:1:1
    > > 1:1:2
    > > 1:1:3
    > > 1:1:4
    > >
    > > So what is the loop with the printf that will print the line to std???
    > > This is probably simple as hell but its late and I can't think!!

    >
    > I just don't see what the pattern is in that output. I also don't
    > understand the question or the examples above. I see three lists. I don't
    > see any multi-dimensional arrays, or see how those lists are supposed to
    > "specify" multi-dimensional arrays. And how do the four colon-delimited
    > lines of output relate to the three lists?
    >
    > Have you at least *attempted* to write the code you're asking us to help you
    > with? (Or do you just want us to do your work?)
    >
    > -Howard


    A 2x2 array has 4 elements, a 2x2x2 array has 8 elements, a 5x10x6
    array has ??
    The elements in a 2x2x2 array can referred to as

    1:1:1
    1:1:2
    1:2:1
    1:2:2
    2:1:1
    2:1:2
    2:2:1
    2:2:2

    Or in code you can write out the element indices like

    for (int i=1; i <= 2; i++)
    for (int j=1; j <= 2; i++)
    for (int k=1; k <= 2; k++)
    printf("%d:%d:%d\n", i, j, k);

    (Never mind the loops are 1-based, its just for the example)

    This is simple enough for arrays where you know the dimensionality
    (not the size), so a 2x2 array is a 2D array and a 3x3x3 is a 3D
    array, a 2x4x5x6 array is 4D array, etc.

    What happens when you have an N dimensional array where the size of
    each dimension may be different? What is the code to print out the
    elements in the same way? Nested for loops will not work because you
    don't know how many you need.
     
    Cant Think Today, Sep 4, 2004
    #3
  4. Cant Think Today

    Mike Wahler Guest

    "Cant Think Today" <> wrote in message
    news:...
    > "Howard" <> wrote in message

    news:<kN2_c.300732$>...
    > > "Cant Think Today" <> wrote in message
    > > news:...
    > > > I have multi-dimesional arrays that can be specifed by the user, e.g
    > > >
    > > > 1,2,3,4,5
    > > > 1,2,3,4,5,6,7,8,9,10
    > > > 1,2,3,4,5,6
    > > >
    > > > I think a bit of code that will iterate over these arrays to print out
    > > > the element indices for each unique element in the N-dimensional
    > > > array. E.g. for the above
    > > >
    > > > 1:1:1
    > > > 1:1:2
    > > > 1:1:3
    > > > 1:1:4
    > > >
    > > > So what is the loop with the printf that will print the line to std???
    > > > This is probably simple as hell but its late and I can't think!!

    > >
    > > I just don't see what the pattern is in that output. I also don't
    > > understand the question or the examples above. I see three lists. I

    don't
    > > see any multi-dimensional arrays, or see how those lists are supposed to
    > > "specify" multi-dimensional arrays. And how do the four colon-delimited
    > > lines of output relate to the three lists?
    > >
    > > Have you at least *attempted* to write the code you're asking us to help

    you
    > > with? (Or do you just want us to do your work?)
    > >
    > > -Howard

    >
    > A 2x2 array has 4 elements, a 2x2x2 array has 8 elements, a 5x10x6
    > array has ??
    > The elements in a 2x2x2 array can referred to as
    >
    > 1:1:1
    > 1:1:2
    > 1:2:1
    > 1:2:2
    > 2:1:1
    > 2:1:2
    > 2:2:1
    > 2:2:2
    >
    > Or in code you can write out the element indices like
    >
    > for (int i=1; i <= 2; i++)
    > for (int j=1; j <= 2; i++)
    > for (int k=1; k <= 2; k++)
    > printf("%d:%d:%d\n", i, j, k);
    >
    > (Never mind the loops are 1-based, its just for the example)
    >
    > This is simple enough for arrays where you know the dimensionality
    > (not the size), so a 2x2 array is a 2D array and a 3x3x3 is a 3D
    > array, a 2x4x5x6 array is 4D array, etc.
    >
    > What happens when you have an N dimensional array where the size of
    > each dimension may be different?


    Not possible with C++. You could simulate it with an
    array of pointers to different sized arrays.

    int arr1[] = {1,2,3};
    int arr2[] = {1,2,3,4,5};
    int arr3[] = {1,2};
    int *arr[] = {arr1, arr2, arr3};

    However a vector of vectors could support your idea
    directly (a vector can have a variable number of elements,
    and can tell you how many elements it has via its member
    function 'size()').

    > What is the code to print out the
    > elements in the same way? Nested for loops will not work because you
    > don't know how many you need.


    If you don't know the size of something, surely you cannot
    tell the computer what that size is. However you could print
    out the contents of my example arrays above (note the plural,
    it's not a single array), like this:

    #include <stdio.h>

    int main()
    {
    int arr1[] = {1,2,3};
    int arr2[] = {1,2,3,4,5};
    int arr3[] = {1,2};
    const int *arr[] = {arr1, arr2, arr3};

    const size_t s1 = sizeof arr1/ sizeof *arr1;
    const size_t s2 = sizeof arr2/ sizeof *arr2;
    const size_t s3 = sizeof arr3/ sizeof *arr3;
    const size_t sz = sizeof arr / sizeof *arr;
    const size_t sizes[] = {s1, s2, s3};

    size_t i = 0;
    size_t j = 0;

    for(i = 0; i < sz; ++i)
    {
    for(j = 0; j < sizes; ++j)
    printf("arr[%lu][%lu] == %d\n",
    (unsigned long)i, (unsigned long)j,
    arr[j]);

    putchar('\n');
    }

    return 0;
    }

    However, we're talking about C++, so I'd use vectors of vectors
    instead (and I'd use 'std::cout' for output, I only used 'printf()'
    because you specifically mentioned it in your original post).

    -Mike
     
    Mike Wahler, Sep 5, 2004
    #4
  5. "Mike Wahler" <> wrote in message
    news:bWs_c.488$...
    >

    <<snip>>
    > However a vector of vectors could support your idea
    > directly (a vector can have a variable number of elements,
    > and can tell you how many elements it has via its member
    > function 'size()').


    Are you saying that a vector or vectors guarantees that the data in the
    vectors in each of the enclosing vectors are continguous? That is, that
    v[2][0] follows v[1][max] in memory?
    I am thinking that this might or might not be the case with vectors, but
    certainly isn't true with ragged arrays. At least as shown in the various
    examples of allocation loops for the arrays, an array of pointers to data
    containing arrays doesn't result in contiguous data containing arrays. I
    think I can prove this if I wasn't so lazy!
    --
    Gary
     
    Gary Labowitz, Sep 5, 2004
    #5
  6. Cant Think Today

    Mike Wahler Guest

    "Gary Labowitz" <> wrote in message
    news:...
    > "Mike Wahler" <> wrote in message
    > news:bWs_c.488$...
    > >

    > <<snip>>
    > > However a vector of vectors could support your idea
    > > directly (a vector can have a variable number of elements,
    > > and can tell you how many elements it has via its member
    > > function 'size()').

    >
    > Are you saying that a vector or vectors guarantees that the data in the
    > vectors in each of the enclosing vectors are continguous?


    No, I didn't say that, and didn't see such a requirement stated by OP.

    > That is, that
    > v[2][0] follows v[1][max] in memory?


    No I didn't say that.

    > I am thinking that this might or might not be the case with vectors,


    It is within a single vector object.

    > but
    > certainly isn't true with ragged arrays.


    Well, no of course not, since C++ doens't have such things.

    >At least as shown in the various
    > examples of allocation loops for the arrays, an array of pointers to data
    > containing arrays doesn't result in contiguous data containing arrays.


    That was not the goal of my code.

    > I
    > think I can prove this if I wasn't so lazy!


    No need.

    -Mike
     
    Mike Wahler, Sep 5, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. andy

    ncsim and signal labeling

    andy, Oct 22, 2004, in forum: VHDL
    Replies:
    1
    Views:
    779
    Eyck Jentzsch
    Oct 26, 2004
  2. Chris Smith

    Map labeling in Java

    Chris Smith, Jul 30, 2004, in forum: Java
    Replies:
    0
    Views:
    472
    Chris Smith
    Jul 30, 2004
  3. noblEnds
    Replies:
    2
    Views:
    527
    Joe Kesselman
    Jun 9, 2006
  4. Cant Think Today

    Labeling Jagged Arrays

    Cant Think Today, Sep 3, 2004, in forum: C Programming
    Replies:
    0
    Views:
    317
    Cant Think Today
    Sep 3, 2004
  5. MikeL

    Error when WebMethod returns a jagged array

    MikeL, Feb 8, 2005, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    336
    MikeL
    Feb 14, 2005
Loading...

Share This Page