By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 3,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

Labeling Jagged (Multi-D) Arrays

P: n/a
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!!
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Cant Think Today" <ca*************@yahoo.com> wrote in message
news:e0**************************@posting.google.c om...
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


Jul 22 '05 #2

P: n/a
"Howard" <al*****@hotmail.com> wrote in message news:<kN********************@bgtnsc05-news.ops.worldnet.att.net>...
"Cant Think Today" <ca*************@yahoo.com> wrote in message
news:e0**************************@posting.google.c om...
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.
Jul 22 '05 #3

P: n/a

"Cant Think Today" <ca*************@yahoo.com> wrote in message
news:e0*************************@posting.google.co m...
"Howard" <al*****@hotmail.com> wrote in message news:<kN********************@bgtnsc05-news.ops.worldnet.att.net>...
"Cant Think Today" <ca*************@yahoo.com> wrote in message
news:e0**************************@posting.google.c om...
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[i]; ++j)
printf("arr[%lu][%lu] == %d\n",
(unsigned long)i, (unsigned long)j,
arr[i][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
Jul 22 '05 #4

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:bW***************@newsread3.news.pas.earthlin k.net...
<<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
Jul 22 '05 #5

P: n/a

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:w5********************@comcast.com...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:bW***************@newsread3.news.pas.earthlin k.net...
<<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
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.