# 2D Array

A

#### axcytz

Hello all,

I have the following code to have a 2D array, and my aim is to have a matrix whose (1) column size is variable(i.e. 1st row has size 3, 2nd has 2, 3rdhas 5, and so on);(2) row size is dynamically changed (I want to be able to add new rows in the future.) Unfortunately, I can't achieve these goals. Thanks for your helps in advance!

#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

int main()
{

int rowSize = 3, columnSize = 5, z=0, counter = 0;
int **array;
array = new int* [rowSize];

for (int i = 0; i < rowSize; i++)
array = new int [columnSize]; // rowSize* columnSize matrix iscreated

for (int z = 0; z < counter; z++ ){ //z is the index of new row
array[rowSize+z] = new int [size[z]];} //to add new row
counter++; // count the added rows
cout << "added row's number of elements:\n";

for (int i = 0; i < addedRow[z]; i++ ){
array[z] = 1; //all elements are set to 1
}

for(int i=0; i<rowSize; i++)
{
for(int j=0; j<n; j++)
{
cout << array[j] << " ";
}
cout << endl;
}

for (int i = 0; i < rowSize; i++){
delete [] array;
}
delete [] array;

}

Ö

#### Öö Tiib

I have the following code to have a 2D array, and my aim is to have a
matrix whose (1) column size is variable(i.e. 1st row has size 3, 2nd
has 2, 3rd has 5, and so on);

In mathematics, a matrix (plural matrices) is a *rectangular* array of
numbers, symbols, or expressions, arranged in rows and columns. So what
you need is definitely not "matrix" because varying row length does not
result with rectangular array.
(2) row size is dynamically changed (I want to be able to add new rows in
the future.)

Unfortunately, I can't achieve these goals. Thanks for your helps in

You forgot to describe why you can't achieve these goals and what
prevents you. Why you try to manage the memory manually?

A

#### axcytz

In mathematics, a matrix (plural matrices) is a *rectangular* array of

numbers, symbols, or expressions, arranged in rows and columns. So what

you need is definitely not "matrix" because varying row length does not

result with rectangular array.

It feels that 'std::vector<std::vector<int>>' matches exactly with your

requirements.

You forgot to describe why you can't achieve these goals and what

prevents you. Why you try to manage the memory manually?

I can't achieve because it doesn't add a new row to my matrix, and all the column sizes are the same. I agree with that it is not a matrix but i triedto code it by that matrix method. Anyways, if I use vectors, how should I modify the code, or should I rewrite it? Thanks

Ö

#### Öö Tiib

I can't achieve because it doesn't add a new row to my matrix, and all
the column sizes are the same. I agree with that it is not a matrix
but i tried to code it by that matrix method. Anyways, if I use vectors,
how should I modify the code, or should I rewrite it? Thanks

Just write a new program. std::vector of std::vectors has all the
functionality that you said you need (and more). You can dynamically
'resize' it or its rows, you can 'push_back' elements to row end or
'insert' into middle and you can 'push_back' rows to its end or 'insert'
into middle. No need to 'new' or 'delete' there anything.

A

#### axcytz

Just write a new program. std::vector of std::vectors has all the

functionality that you said you need (and more). You can dynamically

'resize' it or its rows, you can 'push_back' elements to row end or

'insert' into middle and you can 'push_back' rows to its end or 'insert'

into middle. No need to 'new' or 'delete' there anything.

I found out something like this.

#include <iostream>
#include <vector>
int main()
{
std::vector< std::vector<int> > array =
{ {{2, 3, 5, 1, 5}},
{{0, 1, 1, 4}} } ;

std::vector<int> row = {8,2,8,1};
array.push_back(row);

}

But it doesn't work and gives an error about the array initialization. Is there a mistake in initialization?

A

#### axcytz

So what is the error? My crystal ball seems to be a little bit broken.

Most probably you are using an out-dated compiler, the above example uses

C++11 style initialization. In C++03 the initialization of such vectors

is a bit more cumbersome.

hth

Paavo

The error is "c++98 array must be initialized by constructor, not by {...}".. Is it something to be fixed?

Ö

#### Öö Tiib

The error is "c++98 array must be initialized by constructor, not by {...}".
Is it something to be fixed?

You likely need to add --std=c++0x (or something like that) to your
compiler command line to enable C++11 features. It seemingly assumes that
you want to compile using C++98 features.

A

#### axcytz

You likely need to add --std=c++0x (or something like that) to your

compiler command line to enable C++11 features. It seemingly assumes that

you want to compile using C++98 features.

It didn't work out. Is there a way to fix it within the code?

A

#### axcytz

It didn't work out. Is there a way to fix it within the code?

In C++98 style one must either use intermediate raw arrays or construct

the inner vectors by some other way, e.g. element-by-element. Both

approaches exemplified below:

#include <iostream>

#include <vector>

int main()

{

std::vector< std::vector<int> > array;

int init1[] = {2, 3, 5, 1, 5};

std::vector<int> row1( init1, init1+sizeof(init1)/sizeof(init1[0]));

array.push_back(row1);

int init2[] = {0, 1, 1, 4};

std::vector<int> row2( init2, init2+sizeof(init2)/sizeof(init2[0]));

array.push_back(row2);

std::vector<int> row;

row.push_back(8);

row.push_back(2);

row.push_back(8);

row.push_back(1);

array.push_back(row);

}

Thank you so much for the help Paavo Helde. The method of using intermediate rows is not familiar to me. I presume "row1( init1, init1+sizeof(init1)/sizeof(init1[0]));" defines the array init1 and its size by "init1+sizeof(init1)/sizeof(init1[0])". Is it correct? And a critical question is about thememory usage. Is it more efficient than using matrix with pointers method?Thanks-

B

#### Bart van Ingen Schenau

#include <iostream>
#include <vector>

int main()
{
std::vector< std::vector<int> > array;

int init1[] = {2, 3, 5, 1, 5};
std::vector<int> row1( init1,
init1+sizeof(init1)/sizeof(init1[0]));
Thank you so much for the help Paavo Helde. The method of using
intermediate rows is not familiar to me. I presume "row1( init1,
init1+sizeof(init1)/sizeof(init1[0]));" defines the array init1 and its
size by "init1+sizeof(init1)/sizeof(init1[0])". Is it correct?

No. The line
std::vector<int> row1( init1, init1+sizeof(init1)/sizeof(init1[0]);
defines the vector `row1` and initializes it with the contents of the
array `init1` (which was defined in the preceding line, as shown in the
quote above).

That line uses a constructor for std::vector that accepts two iterators
that delimit a sequence of values. The first iterator is `init1` (the
array name, which decays to the address of the first element.
The second iterator is `init1 + sizeof(init1)/sizeof(init1[0])`. The two
sizeof expressions in there respectively give the number of bytes taken
by the entire array and the number of bytes of the first array element.
Dividing those two gives you the number of array elements.
And a
critical question is about the memory usage. Is it more efficient than
using matrix with pointers method? Thanks-

With regards to the memory consumption, a vector of vectors probably uses
a few bytes more than a home-grown array of pointers.
With regards to the ways in which you can screw things up, an array of
pointers is very much harder to get right.

Bart v Ingen Schenau

A

#### axcytz

You know, it's extremely annoying and unhelpful to simply say "it gives

an error" or "it didn't work out" with no specifics of any kind.

*What* error? *Why* it didn't "work out"? What did the compiler say,

exactly? What exactly did you do? What exactly was it that you wrote

and where did you write it?

--- news://freenews.netfront.net/ - complaints: (e-mail address removed) ---

I forgot to post that it works. Thanks for the helps.