I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this
In the .h file
int *x;
in a initialize function:
x = new int[$Row_Length];
Now I need the x to be able to point to a multidimensional array
I would ahve been fine, with something like
int (*x)[Column_Length] = new int [Row_Length][Column_Length];
But since my variable x needs to be declated in the header file, I am
having some problems compiling. Can some one explain me how to declare
and initialize?
Thank you 9 4449
Slain wrote:
I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this
In the .h file
int *x;
in a initialize function:
x = new int[$Row_Length];
Now I need the x to be able to point to a multidimensional array
I would ahve been fine, with something like
int (*x)[Column_Length] = new int [Row_Length][Column_Length];
But since my variable x needs to be declated in the header file, I am
having some problems compiling. Can some one explain me how to declare
and initialize?
Have you tried looking in the FAQ?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Slain writes:
I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this
In the .h file
int *x;
in a initialize function:
x = new int[$Row_Length];
Now I need the x to be able to point to a multidimensional array
I would ahve been fine, with something like
int (*x)[Column_Length] = new int [Row_Length][Column_Length];
But since my variable x needs to be declated in the header file, I am
having some problems compiling. Can some one explain me how to declare
and initialize?
In C++ proper, you should be using std::vector, rather than old-fashioned C
arrays. Your declaration should be:
std::vector<std::vector<int x;
Then:
x.resize(4);
Four rows.
x[0].resize(3);
x[1].resize(3);
x[2].resize(3);
x[3].resize(3);
Each row is initialized to a three-element array, giving you an
two-dimensional array of four rows and three columns, accessed as:
x[row][col];
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEABECAAYFAkkjfRwACgkQx9p3GYHlUOKDHwCeJwQYSOVEbf iojPBtqZ94r76F
ccAAnjwui67mZglsnMpXaDzAwsqZU1LB
=yV1Z
-----END PGP SIGNATURE-----
On Nov 18, 9:14 pm, Slain <Slai...@gmail.comwrote:
I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this
In the .h file
int *x;
in a initialize function:
x = new int[$Row_Length];
Now I need the x to be able to point to a multidimensional array
I would ahve been fine, with something like
int (*x)[Column_Length] = new int [Row_Length][Column_Length];
But since my variable x needs to be declated in the header file, I am
having some problems compiling. Can some one explain me how to declare
and initialize?
Thank you
Use a vector of vectors instead, much easier and it brings many
dividends (its dynamic, easy to extend - like providing iterators to
the interface). Instead of dealing with heap allocated pointers that
blindly point to whatever in an exception-unsafe way, deal with
objects instead.
// array2d.h, missing include guard
#include <vector>
class Array2D
{
typedef std::vector< int VecN;
// members
std::vector< VecN m_vvn; // 2D array
public:
// ctors
Array2D(std::size_t, std::size_t);
Array2D(std::size_t, std::size_t, const int);
// member functions
std::size_t size() const { return m_vvn.size(); }
VecN operator[](std::size_t index) { return m_vvn[index]; }
void display() const;
};
// array2d.cpp
#include <iostream>
#include <algorithm>
#include <iterator>
#include "array2d.h"
Array2D::Array2D(std::size_t rows, std::size_t cols)
: m_vvn(rows, std::vector< int >(cols)) { }
Array2D::Array2D(std::size_t rows, std::size_t cols, const int n)
: m_vvn(rows, std::vector< int >(cols, n)) { }
void Array2D::display() const
{
typedef std::vector< VecN >::const_iterator VIter;
for(VIter it = m_vvn.begin(); it != m_vvn.end(); ++it)
{
std::copy( (*it).begin(),
(*it).end(),
std::ostream_iterator< int >(std::cout, " ") );
std::cout << std::endl;
}
}
// main.cpp
#include "array2d.h"
int main()
{
Array2D a2d(4, 4, 99);
std::cout << "rows: " << a2d.size() << std::endl;
std::cout << "columns: " << a2d[0].size() << std::endl;
a2d.display();
}
/*
rows: 4
columns: 4
99 99 99 99
99 99 99 99
99 99 99 99
99 99 99 99
*/
On Nov 19, 2:14*am, Slain <Slai...@gmail.comwrote:
I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this
In the .h file
int *x;
No need to modify the declaration. Multidimensional arrays in C++ are
stored as one-dimensional arrays anyway.
What you need to modify is how you calculate one-dimensional index.
in a initialize function:
x = new int[$Row_Length];
$Row_Length - is that a shell or Perl variable in here? ;)
Now I need the x to be able to point to a multidimensional array
I would ahve been fine, with something like
int (*x)[Column_Length] = new int [Row_Length][Column_Length];
But since my variable x needs to be declated in the header file, I am
having some problems compiling. Can some one explain me how to declare
and initialize?
You allocate your two-dimensional array like this:
int* x = new int[Row_Length * Column_Length];
And index into it like this:
int row, col; // initialised elsewhere
// access an element at x[row][col]
int& elem = x[row * Column_Length + col];
--
Max
On Nov 19, 10:12 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 19, 2:14 am, Slain <Slai...@gmail.comwrote:
I need to convert a an array to a multidimensional one.
Since I need to wrok with existing code, I need to modify a
declaration which looks like this
In the .h file
int *x;
Note that if this .h file is included in more than one file,
you'll get undefined behavior (and normally, multiple definition
errors when linking).
No need to modify the declaration. Multidimensional arrays in
C++ are stored as one-dimensional arrays anyway.
That's only true in the most superficial sense. You can't
access a multidimensional array as a one-dimensional array; the
two are different things.
(Formally speaking, of course, C++ doesn't have multidimensional
arrays. But it allows arrays of any type, including array
types, and an array of arrays works pretty much like a
multidimensional array for most things.)
What you need to modify is how you calculate one-dimensional
index.
in a initialize function:
x = new int[$Row_Length];
$Row_Length - is that a shell or Perl variable in here? ;)
Or a typo; he uses Row_Length without the $ later.
Now I need the x to be able to point to a multidimensional
array I would ahve been fine, with something like
int (*x)[Column_Length] = new int [Row_Length][Column_Length];
But since my variable x needs to be declated in the header
file, I am having some problems compiling. Can some one
explain me how to declare and initialize?
You allocate your two-dimensional array like this:
int* x = new int[Row_Length * Column_Length];
And index into it like this:
int row, col; // initialised elsewhere
// access an element at x[row][col]
int& elem = x[row * Column_Length + col];
That's not a two dimensional array; that's just a method of
simulating one. While there are definitely cases where this
approach is recommended (or even necessisary), if his dimensions
(or at least Column_Length) is a constant, he can also write:
extern int (*x)[ Column_Length ] ;
in the header, and use
int (*x)[ Column_Length ] = new[ Row_Length ][ Column_Length ] ;
to initialize it.
Of course, a better solution might be to define a Matrix class,
and use that. With an implementation based on std::vector.
(Probably a one dimensional vector, calculating the indexes as
you described.)
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
On Nov 19, 9:47*am, James Kanze <james.ka...@gmail.comwrote:
On Nov 19, 10:12 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 19, 2:14 am, Slain <Slai...@gmail.comwrote:
I need to convert a an array to a multidimensional one.
Since I need to wrok with existing code, I need to modify a
declaration which looks like this
In the .h file
int *x;
Note that if this .h file is included in more than one file,
you'll get undefined behavior (and normally, multiple definition
errors when linking).
No need to modify the declaration. Multidimensional arrays in
C++ are stored as one-dimensional arrays anyway.
That's only true in the most superficial sense. *You can't
access a multidimensional array as a one-dimensional array; the
two are different things.
Yes, you can:
int(*x)[Cols] = new int[Rows][Cols];
// let's represent it as a plain array
int* y = x[0] + 0;
(Formally speaking, of course, C++ doesn't have multidimensional
arrays. *But it allows arrays of any type, including array
types, and an array of arrays works pretty much like a
multidimensional array for most things.)
In C++ one-dimensional and multi-dimensional arrays are different
names for the same thing - a contiguous block of memory. Using
language constructs one can view that block as a one-dimensional or
multi-dimensional array. Nevertheless, it is still fundamentally the
same thing in C and C++.
--
Max
On Nov 19, 5:22*am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 19, 9:47*am, James Kanze <james.ka...@gmail.comwrote:
On Nov 19, 10:12 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 19, 2:14 am, Slain <Slai...@gmail.comwrote:
I need to convert a an array to a multidimensional one.
Since I need to wrok with existing code, I need to modify a
declaration which looks like this
In the .h file
int *x;
Note that if this .h file is included in more than one file,
you'll get undefined behavior (and normally, multiple definition
errors when linking).
No need to modify the declaration. Multidimensional arrays in
C++ are stored as one-dimensional arrays anyway.
That's only true in the most superficial sense. *You can't
access a multidimensional array as a one-dimensional array; the
two are different things.
Yes, you can:
* * int(*x)[Cols] = new int[Rows][Cols];
* * // let's represent it as a plain array
* * int* y = x[0] + 0;
(Formally speaking, of course, C++ doesn't have multidimensional
arrays. *But it allows arrays of any type, including array
types, and an array of arrays works pretty much like a
multidimensional array for most things.)
In C++ one-dimensional and multi-dimensional arrays are different
names for the same thing - a contiguous block of memory. Using
language constructs one can view that block as a one-dimensional or
multi-dimensional array. Nevertheless, it is still fundamentally the
same thing in C and C++.
--
Max- Hide quoted text -
- Show quoted text -
Thanks Max and James and the others!!!
We don't ahve STL's :(
I would eprsonally want to go ahead with the single dimensional
approach which max suggested, but that is becase I am an electrical
engineer. I think since this fits into existing code, I would have it
access elements much like a two dimensional array. It would be much
easier for the future person dealing with it.
The Row_Length was just means to provide that a fixed value will go in
there.
I think I will try James approach. Thank you guys!!! I might come abck
with more questions :)
Thanks a lot
On Nov 19, 11:22 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 19, 9:47 am, James Kanze <james.ka...@gmail.comwrote:
On Nov 19, 10:12 am, Maxim Yegorushkin
<maxim.yegorush...@gmail.comwrote:
On Nov 19, 2:14 am, Slain <Slai...@gmail.comwrote:
I need to convert a an array to a multidimensional one.
Since I need to wrok with existing code, I need to modify a
declaration which looks like this
In the .h file
int *x;
Note that if this .h file is included in more than one file,
you'll get undefined behavior (and normally, multiple
definition errors when linking).
No need to modify the declaration. Multidimensional arrays
in C++ are stored as one-dimensional arrays anyway.
That's only true in the most superficial sense. You can't
access a multidimensional array as a one-dimensional array;
the two are different things.
Yes, you can:
int(*x)[Cols] = new int[Rows][Cols];
// let's represent it as a plain array
int* y = x[0] + 0;
I'm not quite sure what the + 0 is doing there; it changes
absolutely nothing. But all you've got is still a pointer to
the first element of x[0]; an expression like y[Rows+1] is
undefined behavior.
(Formally speaking, of course, C++ doesn't have
multidimensional arrays. But it allows arrays of any type,
including array types, and an array of arrays works pretty
much like a multidimensional array for most things.)
In C++ one-dimensional and multi-dimensional arrays are
different names for the same thing - a contiguous block of
memory.
That's simply false. In C++, arrays have a type; they're not
just a block of (raw) memory. And that type includes the
dimension.
Using language constructs one can view that block as a
one-dimensional or multi-dimensional array.
Not without invoking undefined behavior.
Nevertheless, it is still fundamentally the same thing in C
and C++.
That's true. The C standard was carefully written to allow
bounds checking. I only know of one implementation which ever
did it, but the standard is clear; it's legal, and anything that
would cause a bounds check error is undefined behavior.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Nov 20, 8:43*am, James Kanze <james.ka...@gmail.comwrote:
On Nov 19, 11:22 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 19, 9:47 am, James Kanze <james.ka...@gmail.comwrote:
On Nov 19, 10:12 am, Maxim Yegorushkin
<maxim.yegorush...@gmail.comwrote:
On Nov 19, 2:14 am, Slain <Slai...@gmail.comwrote:
I need to convert a an array to a multidimensional one.
Since I need to wrok with existing code, I need to modify a
declaration which looks like this
In the .h file
int *x;
Note that if this .h file is included in more than one file,
you'll get undefined behavior (and normally, multiple
definition errors when linking).
No need to modify the declaration. Multidimensional arrays
in C++ are stored as one-dimensional arrays anyway.
That's only true in the most superficial sense. *You can't
access a multidimensional array as a one-dimensional array;
the two are different things.
Yes, you can:
* * int(*x)[Cols] = new int[Rows][Cols];
* * // let's represent it as a plain array
* * int* y = x[0] + 0;
I'm not quite sure what the + 0 is doing there;
It is a shortcut for:
int* y = &x[0][0];
it changes absolutely nothing. *But all you've got is still a pointer to
the first element of x[0];
You are right that x[0] is sufficient.
an expression like y[Rows+1] is undefined behavior.
It is just meaningless.
(Formally speaking, of course, C++ doesn't have
multidimensional arrays. *But it allows arrays of any type,
including array types, and an array of arrays works pretty
much like a multidimensional array for most things.)
In C++ one-dimensional and multi-dimensional arrays are
different names for the same thing - a contiguous block of
memory.
That's simply false. *In C++, arrays have a type; they're not
just a block of (raw) memory. *And that type includes the
dimension.
There are two separate issues: type and binary layout.
You are quite right that the types are distinct and unrelated
according to the standard, and thus casting is formally undefined
behaviour.
The binary layout is quite a different story. Size of an object is
always a multiple of its alignment. The size is defined this way, so
that when objects are stored in an array there is no padding between
the objects. Thus, the size of a one-dimensional array is nothing more
than the size of an element multiplied by the number of elements.
In a multi-dimensional array the elements are arrays. Due to the
requirement that there be no padding between the elements of an array,
there is no padding between elements-arrays of a multi-dimensional
array. Thus, multi-dimensional arrays can not be stored any other way,
but exactly as a single-dimensional array with the number of elements
equal to the total number of elements of the multi-dimensional array.
Essentially, the standard implicitly guarantees that the binary
layouts of arrays with the same underlying object type but with
different dimensions are the same as long as the total number of the
objects is the same.
Using language constructs one can view that block as a
one-dimensional or multi-dimensional array.
Not without invoking undefined behavior.
On one hand the standard says that the casting between arrays of
different dimensions is undefined behaviour, on the other hand it
provides the aforementioned binary layout guarantees. In my opinion,
it is an underspecification or inconsistency of the standard.
--
Max This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: M.Siler |
last post by:
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT>
<!--
var factor_val = new Array(8,7)
factor_val = 68.8
factor_val = 55
|
by: Kevin |
last post by:
Help!
Why are none of these valid?
var arrayName = new Array();
arrayName = new Array('alpha_val', 1);
arrayName = ;
I'm creating/writing the array on the server side from Perl, but I
|
by: Luke Wu |
last post by:
Hello,
I'm having some problems understanding 2 dimensional arrays. My
problem relates to the following code:
#include <stdio.h>
#define M 3
#define N 3
|
by: Mark Smith |
last post by:
I'm trying to copy data from a 1D array to a 2D array.
The obvious thing doesn't work:
int twoDee = new int;
int oneDee = new int { 1, 2 };
Array.Copy(oneDee, 2, twoDee, 2, 2);
This causes a...
|
by: Claire |
last post by:
I have a multidimensional array defined as
private double myArray = new double;
The first column of the array contains X values, the other contains Y values
I have a charting function defined as...
|
by: chris |
last post by:
Hi there,
I created a Multidimensional array of labels
Label lblMultiArray = new Label { {Label3, LblThuTotal},
{Label4,LblFriTotal} };
Now I would like to compare the values in the array,...
|
by: Kavya |
last post by:
int main (){
int a={{1,2,3},{4,5,6}};
int (*ptr)=a;
/* This should be fine and give 3 as output*/
printf("%d\n",(*ptr));
++ptr;
|
by: Karl Groves |
last post by:
I'm missing something very obvious, but it is getting late and I've
stared at it too long.
TIA for responses
I am writing a basic function (listed at the bottom of this post) that
returns...
|
by: LittleCake |
last post by:
Hi All,
I have a multidimensional array where each sub-array contains just two
entries, which indicates a relationship between those two entries. for
example the first sub-array:
=Array
(
=30...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |