473,748 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Labeling Jagged (Multi-D) Arrays

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
5 2554

"Cant Think Today" <ca************ *@yahoo.com> wrote in message
news:e0******** *************** ***@posting.goo gle.com...
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
"Howard" <al*****@hotmai l.com> wrote in message news:<kN******* *************@b gtnsc05-news.ops.worldn et.att.net>...
"Cant Think Today" <ca************ *@yahoo.com> wrote in message
news:e0******** *************** ***@posting.goo gle.com...
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

"Cant Think Today" <ca************ *@yahoo.com> wrote in message
news:e0******** *************** **@posting.goog le.com...
"Howard" <al*****@hotmai l.com> wrote in message news:<kN******* *************@b gtnsc05-news.ops.worldn et.att.net>...
"Cant Think Today" <ca************ *@yahoo.com> wrote in message
news:e0******** *************** ***@posting.goo gle.com...
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
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:bW******** *******@newsrea d3.news.pas.ear thlink.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

"Gary Labowitz" <gl*******@comc ast.net> wrote in message
news:w5******** ************@co mcast.com...
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:bW******** *******@newsrea d3.news.pas.ear thlink.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
989
by: Cant Think Today | last post by:
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
2726
by: James dean | last post by:
I done a test and i really do not know the reason why a jagged array who has the same number of elements as a multidimensional array is faster here is my test. I assign a value and do a small calculation. Even if i initialise the jagged array inside the function it is still much faster. Are these results correct?. If i put the initialisation loop in the constructor its ridiculously faster but even here its 4 times faster...is this correct?...
3
2559
by: James dean | last post by:
I have created algorithms in C# unsafe code and have fixed the arrays in memory for optimum performance. I use multidimensional arrays rather than jagged arrays. The algorithms i use usually read a bitmap file sequentially so no random accessing different parts of the array. I know in microsoft site it says to use jagged arrays but in this case with reading a sequential file the performance difference will not be as good as a...
1
1271
by: Chris Wood | last post by:
I need to do a variable argument list in Managed C++, where each argument is a double array. I believe that this is impossible, because to do so I would need to make a jagged matrix of doubles, which is not supported. Does anyone know if this is impossible, or if there is some other way to do this? Using System.Array rather than double __gc is not an option for me. Thanks -- Chris
3
2199
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string multidimensionalArray = new string
1
10313
by: xllx.relient.xllx | last post by:
Hi, I have two questions: 1.)Is it true that an rectangular array is really just an single dimensional array that lets itself be treated as a multi-dimensional array? For example the following declaration: int rectangular = new int;
3
4035
by: alcabo | last post by:
Hello, I'd like to improve several critical routines involving arrays (vectors and matrices)... How are arrays stored in memory? Row major or column major? (Like in C or like Fortran?)
5
10354
by: TS | last post by:
is there some code somewhere that does this? i have a jagged array that is not jagged, it has an equal number of rows and columns in each array so it should convert but want to get the code to do it somewhere. thanks
17
7253
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.