2D Array

Discussion in 'C++' started by axcytz@gmail.com, Jun 28, 2013.

  1. Guest

    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


    int* addedRow;
    addedRow = new int [rowSize]; //to add new rows



    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";
    cin >> addedRow[z];

    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;

    }
    , Jun 28, 2013
    #1
    1. Advertising

  2. Öö Tiib Guest

    On Friday, 28 June 2013 20:59:46 UTC+3, wrote:
    > 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.)


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

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


    You forgot to describe why you can't achieve these goals and what
    prevents you. Why you try to manage the memory manually?
    Öö Tiib, Jun 28, 2013
    #2
    1. Advertising

  3. Guest

    On Friday, June 28, 2013 1:19:57 PM UTC-5, Öö Tiib wrote:
    > On Friday, 28 June 2013 20:59:46 UTC+3, wrote:
    >
    > > 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.)

    >
    >
    >
    > It feels that 'std::vector<std::vector<int>>' matches exactly with your
    >
    > requirements.
    >
    >
    >
    > > Unfortunately, I can't achieve these goals. Thanks for your helps in

    >
    > > advance!

    >
    >
    >
    > 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
    , Jun 28, 2013
    #3
  4. Öö Tiib Guest

    On Friday, 28 June 2013 23:10:00 UTC+3, wrote:
    > On Friday, June 28, 2013 1:19:57 PM UTC-5, Öö Tiib wrote:
    > > On Friday, 28 June 2013 20:59:46 UTC+3, wrote:
    > > > 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.)

    > >
    > > It feels that 'std::vector<std::vector<int>>' matches exactly with your
    > > requirements.
    > >
    > > > Unfortunately, I can't achieve these goals. Thanks for your helps in
    > > > advance!

    > >
    > > 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 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.
    Öö Tiib, Jun 28, 2013
    #4
  5. Guest

    On Friday, June 28, 2013 4:01:27 PM UTC-5, Öö Tiib wrote:
    > On Friday, 28 June 2013 23:10:00 UTC+3, wrote:
    >
    > > On Friday, June 28, 2013 1:19:57 PM UTC-5, Öö Tiib wrote:

    >
    > > > On Friday, 28 June 2013 20:59:46 UTC+3, wrote:

    >
    > > > > I have the following code to have a 2D array, and my aim is to havea

    >
    > > > > 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.)

    >
    > > >

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

    >
    > > > requirements.

    >
    > > >

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

    >
    > > > > advance!

    >
    > > >

    >
    > > > 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 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.


    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?
    , Jun 28, 2013
    #5
  6. Guest

    On Friday, June 28, 2013 5:05:15 PM UTC-5, Paavo Helde wrote:
    > wrote in
    >
    > news::
    >
    >
    >
    > > On Friday, June 28, 2013 4:01:27 PM UTC-5, Öö Tiib wrote:

    >
    > >> On Friday, 28 June 2013 23:10:00 UTC+3, wrote:

    >
    > >>

    >
    > >> > On Friday, June 28, 2013 1:19:57 PM UTC-5, Öö Tiib wrote:

    >
    > >>

    >
    > >> > > On Friday, 28 June 2013 20:59:46 UTC+3, wrote:

    >
    > >>

    >
    > >> > > > 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, 2

    >
    > > nd

    >
    > >>

    >
    > >> > > > has 2, 3rd has 5, and so on);

    >
    > >>

    >
    > >> > >

    >
    > >>

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

    >
    > >> > > array o

    >
    > > f

    >
    > >>

    >
    > >> > > numbers, symbols, or expressions, arranged in rows and columns.

    >
    > >> > > So wh

    >
    > > at

    >
    > >>

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

    >
    > >> > > does n

    >
    > > ot

    >
    > >>

    >
    > >> > > result with rectangular array.

    >
    > >>

    >
    > >> > >

    >
    > >>

    >
    > >> > > > (2) row size is dynamically changed (I want to be able to add

    >
    > >> > > > new r

    >
    > > ows in

    >
    > >>

    >
    > >> > > > the future.)

    >
    > >>

    >
    > >> > >

    >
    > >>

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

    >
    > >> > > with yo

    >
    > > ur

    >
    > >>

    >
    > >> > > requirements.

    >
    > >>

    >
    > >> > >

    >
    > >>

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

    >
    > >> > > > helps i

    >
    > > n

    >
    > >>

    >
    > >> > > > advance!

    >
    > >>

    >
    > >> > >

    >
    > >>

    >
    > >> > > 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 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.

    >
    > >

    >
    > > 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?

    >
    >
    >
    > 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?
    , Jun 28, 2013
    #6
  7. Öö Tiib Guest

    On Saturday, 29 June 2013 01:07:38 UTC+3, wrote:
    >
    > 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.
    Öö Tiib, Jun 28, 2013
    #7
  8. Guest

    On Friday, June 28, 2013 5:37:46 PM UTC-5, Öö Tiib wrote:
    > On Saturday, 29 June 2013 01:07:38 UTC+3, wrote:
    >
    > >

    >
    > > 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.


    It didn't work out. Is there a way to fix it within the code?
    , Jun 29, 2013
    #8
  9. Guest

    On Saturday, June 29, 2013 1:12:00 AM UTC-5, Paavo Helde wrote:
    > wrote in
    >
    > news::
    >
    >
    >
    > > On Friday, June 28, 2013 5:37:46 PM UTC-5, Öö Tiib wrote:

    >
    > >> On Saturday, 29 June 2013 01:07:38 UTC+3, wrote:

    >
    > >>

    >
    > >> >

    >
    > >>

    >
    > >> > 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.

    >
    > >

    >
    > > 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-
    , Jun 29, 2013
    #9
  10. On Sat, 29 Jun 2013 09:03:28 -0700, axcytz wrote:

    > On Saturday, June 29, 2013 1:12:00 AM UTC-5, Paavo Helde wrote:
    >> wrote in
    >>
    >> #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]));

    <snip>
    > 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
    Bart van Ingen Schenau, Jun 29, 2013
    #10
  11. Guest

    On Monday, July 1, 2013 1:38:57 AM UTC-5, Juha Nieminen wrote:
    > wrote:
    >
    > >> 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.

    >
    >
    >
    > 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?
    >
    >
    >
    > You need to be more specific. We cannot read your mind.
    >
    >
    >
    > --- news://freenews.netfront.net/ - complaints: ---


    I forgot to post that it works. Thanks for the helps.
    , Jul 1, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel
    Replies:
    1
    Views:
    1,033
    Kevin Collins
    Aug 14, 2003
  2. S300
    Replies:
    4
    Views:
    21,963
    Roedy Green
    Aug 19, 2003
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    470
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    196
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    460
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page