- **C++**
(*http://www.velocityreviews.com/forums/f39-c.html*)

- - **2D Array**
(*http://www.velocityreviews.com/forums/t962261-2d-array.html*)

2D ArrayHello 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[i] = 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][i] = 1; //all elements are set to 1 } for(int i=0; i<rowSize; i++) { for(int j=0; j<n; j++) { cout << array[i][j] << " "; } cout << endl; } for (int i = 0; i < rowSize; i++){ delete [] array[i]; } delete [] array; } |

Re: 2D ArrayOn Friday, 28 June 2013 20:59:46 UTC+3, axc...@gmail.com 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? |

Re: 2D ArrayOn Friday, June 28, 2013 1:19:57 PM UTC-5, Öö Tiib wrote:
> On Friday, 28 June 2013 20:59:46 UTC+3, axc...@gmail.com 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 |

Re: 2D ArrayOn Friday, 28 June 2013 23:10:00 UTC+3, axc...@gmail.com wrote:
> On Friday, June 28, 2013 1:19:57 PM UTC-5, Öö Tiib wrote: > > On Friday, 28 June 2013 20:59:46 UTC+3, axc...@gmail.com 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. |

Re: 2D ArrayOn Friday, June 28, 2013 4:01:27 PM UTC-5, Öö Tiib wrote:
> On Friday, 28 June 2013 23:10:00 UTC+3, axc...@gmail.com wrote: > > > On Friday, June 28, 2013 1:19:57 PM UTC-5, Öö Tiib wrote: > > > > On Friday, 28 June 2013 20:59:46 UTC+3, axc...@gmail.com 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? |

Re: 2D ArrayOn Friday, June 28, 2013 5:05:15 PM UTC-5, Paavo Helde wrote:
> axcytz@gmail.com wrote in > > news:f46e4d35-3007-43ef-bf84-49254fc5da8c@googlegroups.com: > > > > > On Friday, June 28, 2013 4:01:27 PM UTC-5, Öö Tiib wrote: > > >> On Friday, 28 June 2013 23:10:00 UTC+3, axc...@gmail.com wrote: > > >> > > >> > On Friday, June 28, 2013 1:19:57 PM UTC-5, Öö Tiib wrote: > > >> > > >> > > On Friday, 28 June 2013 20:59:46 UTC+3, axc...@gmail.com 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? |

Re: 2D ArrayOn Saturday, 29 June 2013 01:07:38 UTC+3, axc...@gmail.com 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. |

Re: 2D ArrayOn Friday, June 28, 2013 5:37:46 PM UTC-5, Öö Tiib wrote:
> On Saturday, 29 June 2013 01:07:38 UTC+3, axc...@gmail.com 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? |

Re: 2D ArrayOn Saturday, June 29, 2013 1:12:00 AM UTC-5, Paavo Helde wrote:
> axcytz@gmail.com wrote in > > news:ae2340d7-4be6-4e9f-a89f-0c5bfa0b2ce2@googlegroups.com: > > > > > On Friday, June 28, 2013 5:37:46 PM UTC-5, Öö Tiib wrote: > > >> On Saturday, 29 June 2013 01:07:38 UTC+3, axc...@gmail.com 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- |

Re: 2D ArrayOn Sat, 29 Jun 2013 09:03:28 -0700, axcytz wrote:
> On Saturday, June 29, 2013 1:12:00 AM UTC-5, Paavo Helde wrote: >> axcytz@gmail.com 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 |

All times are GMT. The time now is 04:42 AM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.