473,383 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

2 dimensional arrays as parameter

Hi,

I have a problem regarding passin 2 dimensional array into a function.
I have a[][] to pass into function f(), f is called many times and the size
of a[][] will change for each call.
I am not interested in vector implementation in std library since speed is
my major concern.

the follwoing code does not work of couse, it show the idea what I want. The
compiler comlained about lacking at least the 2nd dimnesion length.

void f(int a[][]){
for(i)for(j){
...a[i][j]
}
}

main(){
int nrow=5,ncol=4;
int a[nrow][ncol];
....//fill in a
f(a);

......
nrow = ..,ncol =...
f(a);
}

in such case, what would be the solution, thanks.
Apr 16 '07 #1
6 3201
hyena wrote:
I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many times
and
the size of a[][] will change for each call.
Both of them?
I am not interested in vector implementation in std library since
speed is my major concern.
Ah... Premature optimization, assumptions... Sure. You must know
better.
the follwoing code does not work of couse, it show the idea what I
want. The compiler comlained about lacking at least the 2nd dimnesion
length.
Well, one way around it is to pass an array of pointers, and not
a two-dimensional array of values. The other way around it is to
make the function a template and pass the array by a reference and
let the compiler figure out the sizes (if they are known at compile
time). Yet another way around it is to pass a one-dimensional array
and do all indexing yourself (instead of relying on the compiler to
figure out all offsets from the second dimension).
>
void f(int a[][]){
for(i)for(j){
...a[i][j]
}
}

main(){
int nrow=5,ncol=4;
int a[nrow][ncol];
That's not C++.
....//fill in a
f(a);

.....
nrow = ..,ncol =...
f(a);
}

in such case, what would be the solution, thanks.
I don't know "the" solution. But there are several you can try.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 16 '07 #2

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f0**********@news.datemas.de...
hyena wrote:
> I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many times
and
the size of a[][] will change for each call.

Both of them?
yes, both of them, pity.
>
>I am not interested in vector implementation in std library since
speed is my major concern.

Ah... Premature optimization, assumptions... Sure. You must know
better.
well, ....I had tried another piece of code in std realization and found it
is not very efficient compare to array version,from that comes my
impression, along with some disscussions I read.
>the follwoing code does not work of couse, it show the idea what I
want. The compiler comlained about lacking at least the 2nd dimnesion
length.

Well, one way around it is to pass an array of pointers, and not
a two-dimensional array of values. The other way around it is to
make the function a template and pass the array by a reference and
let the compiler figure out the sizes (if they are known at compile
time). Yet another way around it is to pass a one-dimensional array
and do all indexing yourself (instead of relying on the compiler to
figure out all offsets from the second dimension).
snip

I was trying to make the code simple and readable, if possible. I have tried
pass a array of pointers, still I need to give info at least about the
length of the array pointed by the pointer.

eg. f(int (*a)[]) , the compiler complains no array size info again.

and I am not familiar with c++ template concept, :). Seems "the" solution to
me may well be the last you suggested.

Thansk for your input.
Apr 16 '07 #3
hyena skrev:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f0**********@news.datemas.de...
>hyena wrote:
>> I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many times
and
the size of a[][] will change for each call.
Both of them?

yes, both of them, pity.
>>I am not interested in vector implementation in std library since
speed is my major concern.
Ah... Premature optimization, assumptions... Sure. You must know
better.

well, ....I had tried another piece of code in std realization and found it
is not very efficient compare to array version,from that comes my
impression, along with some disscussions I read.
Do you mean std::vector< std::vector< int or something else?
>
>>the follwoing code does not work of couse, it show the idea what I
want. The compiler comlained about lacking at least the 2nd dimnesion
length.
Well, one way around it is to pass an array of pointers, and not
a two-dimensional array of values. The other way around it is to
make the function a template and pass the array by a reference and
let the compiler figure out the sizes (if they are known at compile
time). Yet another way around it is to pass a one-dimensional array
and do all indexing yourself (instead of relying on the compiler to
figure out all offsets from the second dimension).
snip

I was trying to make the code simple and readable, if possible. I have tried
pass a array of pointers, still I need to give info at least about the
length of the array pointed by the pointer.
It's the sub dimensions the compiler needs length specified for in order
to calculate offset jumps when indexing. For the first dimension all it
needs to know comes from sizeof(type). It's a memory layout issue.
>
eg. f(int (*a)[]) , the compiler complains no array size info again.

and I am not familiar with c++ template concept, :). Seems "the" solution to
me may well be the last you suggested.
You could create your own wrapper:

class two_dim_array {
int ** d_array;
two_dim_array() {
int x = 3, y = 8;
d_array = new int*[x];
for(unsigned i = 0; i < x; ++i) {
d_array[i] = new int[y];
}
}
int * operator[](unsigned index) {
return d_array[index];
}
// and more like resize() and ~two_dim_array
};

Another solution is what Victor gave you:

int xs = 3, ys = 5;

int * array = new int[xs*ys];

int index(unsigned x, unsigned y) {
return array[x * ys + y];
}

--
OU
Apr 16 '07 #4

"Obnoxious User" <OU@127.0.0.1wrote in message
news:46***********************@alt.teranews.com...
hyena skrev:
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f0**********@news.datemas.de...
>>hyena wrote:
I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many
times and
the size of a[][] will change for each call.
Both of them?

yes, both of them, pity.
>>>I am not interested in vector implementation in std library since
speed is my major concern.
Ah... Premature optimization, assumptions... Sure. You must know
better.

well, ....I had tried another piece of code in std realization and found
it is not very efficient compare to array version,from that comes my
impression, along with some disscussions I read.

Do you mean std::vector< std::vector< int or something else?
I remeber it was std::vector< std::vector< double or float type. with
many loops(no random access, just going through the entire array) and
passing around to other functions, my impression is that this constainer
version runs much slower then the simple array version. But I did not
seriously try to compare them and quickly turn back to normal array
solutions.
>>
>>>the follwoing code does not work of couse, it show the idea what I
want. The compiler comlained about lacking at least the 2nd dimnesion
length.
Well, one way around it is to pass an array of pointers, and not
a two-dimensional array of values. The other way around it is to
make the function a template and pass the array by a reference and
let the compiler figure out the sizes (if they are known at compile
time). Yet another way around it is to pass a one-dimensional array
and do all indexing yourself (instead of relying on the compiler to
figure out all offsets from the second dimension).
snip

I was trying to make the code simple and readable, if possible. I have
tried pass a array of pointers, still I need to give info at least about
the length of the array pointed by the pointer.

It's the sub dimensions the compiler needs length specified for in order
to calculate offset jumps when indexing. For the first dimension all it
needs to know comes from sizeof(type). It's a memory layout issue.
yeap.
>>
eg. f(int (*a)[]) , the compiler complains no array size info again.

and I am not familiar with c++ template concept, :). Seems "the" solution
to me may well be the last you suggested.

You could create your own wrapper:

class two_dim_array {
int ** d_array;
two_dim_array() {
int x = 3, y = 8;
d_array = new int*[x];
for(unsigned i = 0; i < x; ++i) {
d_array[i] = new int[y];
}
}
int * operator[](unsigned index) {
return d_array[index];
}
// and more like resize() and ~two_dim_array
};

Another solution is what Victor gave you:

int xs = 3, ys = 5;

int * array = new int[xs*ys];

int index(unsigned x, unsigned y) {
return array[x * ys + y];
}

--
OU
Both looks quite suitable to my problem. I think I will follow what Victor
has suggested and just copy ur code here:).

thanks for the answers and the codes.
Apr 16 '07 #5
hyena wrote:
Hi,

I have a problem regarding passin 2 dimensional array into a function.
I have a[][] to pass into function f(), f is called many times and the size
of a[][] will change for each call.
I am not interested in vector implementation in std library since speed is
my major concern.
I think the valarray is performing enough :)
the follwoing code does not work of couse, it show the idea what I want. The
compiler comlained about lacking at least the 2nd dimnesion length.

void f(int a[][]){
for(i)for(j){
...a[i][j]
}
}

main(){
int nrow=5,ncol=4;
int a[nrow][ncol];
ok, given that this is an error, I'm wondering how you are actually
allocating this memory. Do you really need allocation in the stack?
Otherwise, you can go for a int** if you want to keep the double
dimensions, or a int* in which you access the elements as n*i+j, n being
the number of columns in your matrix.
....//fill in a
f(a);

.....
nrow = ..,ncol =...
oh, well, what is this? you need to reallocate a as well.
f(a);
}

in such case, what would be the solution, thanks.

another possibility is to use some library such matrix ublas included in
boost, the matrix definition is clean and very optimized. You can also
choose the storage model in order to maximize the performances in the
vectorial operations.

Of course, the objects have to be given to the functions by reference.

Remember anyway that if you need the performances the worst solution is
to start optimizing without knowing well where the things have to be
optimized. For example, probably the vector has got a little bit of
overhead for some operations (but for the elements access and so on it's
usually not the case), but a matrix allocation is as expensive as
thousands of vector element accesses.

Regards,

Zeppe

Apr 16 '07 #6
hyena <as@hut.atwrote:
>
"Obnoxious User" <OU@127.0.0.1wrote in message
news:46***********************@alt.teranews.com...
>hyena skrev:
>>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f0**********@news.datemas.de...
hyena wrote:
I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many
times and
the size of a[][] will change for each call.
Both of them?

yes, both of them, pity.

I am not interested in vector implementation in std library since
speed is my major concern.
Ah... Premature optimization, assumptions... Sure. You must know
better.

well, ....I had tried another piece of code in std realization and found
it is not very efficient compare to array version,from that comes my
impression, along with some disscussions I read.

Do you mean std::vector< std::vector< int or something else?

I remeber it was std::vector< std::vector< double or float type. with
many loops(no random access, just going through the entire array) and
passing around to other functions, my impression is that this constainer
version runs much slower then the simple array version. But I did not
seriously try to compare them and quickly turn back to normal array
solutions.
In your vector functions, if you were not passing the vectors by
reference, then it was probably copying the vectors many more times than
it needed to. In many cases, vectors and arrays have equal performance.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: iop | last post by:
Hello there, I'd like to "parse" an entire multi-dimension array like this : APP APP without knowing "framework" or "config" or anything passed as variables... 'cause it's simple to call...
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
0
by: volx | last post by:
Hello all: What is the proper way to implement in MC++ a web service which accepts a multi dimensional array as a parameter to one of its methods? This does compile: double...
16
by: rguti | last post by:
Hi, How do I create a two dimensional array? I have created a one dimensional doing this: Dim laFields As ArrayList = New ArrayList How about to do a 2 dimensional?
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
22
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.