Labeling Jagged (Multi-D) Arrays

  • Thread starter Cant Think Today
  • Start date
C

Cant Think Today

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!!
 
H

Howard

Cant Think Today said:
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
 
C

Cant Think Today

Howard said:
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.
 
M

Mike Wahler

Cant Think Today said:
"Howard" <[email protected]> wrote in message

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
 
G

Gary Labowitz

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!
 
M

Mike Wahler

Gary Labowitz said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top