• Advertisement
Sign in to follow this  

How to add a line to the two dimensional array?

This topic is 2150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The question is the same as topic.Here is the first code#include <iostream.h>
void main()
{
int m = 3 ,n =2,i,j;
int **data = new int*[m];
for(i=0;i<m;i++)
{
data = new int[n];
}
for(i =0;i<m;i++)
{
for(j= 0;j<n;j++)
{
data[j] = i*j;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"data["<<i<<"]["<<j<<"]="<<data[j]<<"\n";
}
}

then ,i want to add a line to the "array"(a array) and give the number to this line which is m = m+1;
for(i=0;j<n;j++)
{
data[m][j]=(m+2)*j;
}

Is that adding "m += 1" to the code right?

Share this post


Link to post
Share on other sites
Advertisement
I try to compile that and run the program, but the program always fail to work.,why could be that?

Share this post


Link to post
Share on other sites
(i=0;j<n;j++)



is that i supposed to be j?

and i think it should be m isntead of m+1 and the second m inside the loop should be just m too

(edited)

Share this post


Link to post
Share on other sites
Thanks, "i"is sure a small accident,but it is not the main problem,i still not find a correct method to add a line to this dynamic array.The old method is worse.

Share this post


Link to post
Share on other sites
The number of items in an array in C++ is increased by creating a new, larger array; copying the items from the old array to the new one; deleting the old array and then setting your array variable to the new array.


int* array = new int[ ArraySize ] ;
// DoStuff
// I like to put anything that creates temporary variables into a temporary context, so they don't "spill over" into the rest of the program.
{
int* newArray = new int[ NewArraySize ] ;
for ( int t = 0 ; t < ArraySize && t < LargerArraySize ; ++t )
newArray[ t ] = array[ t ] ;
delete array[] ;
array = newArray ; // Works because delete doesn't remove the variable name, only the data pointed to.
}

Share this post


Link to post
Share on other sites

The number of items in an array in C++ is increased by creating a new, larger array; copying the items from the old array to the new one; deleting the old array and then setting your array variable to the new array.


Or you can use std::vector and let the library do the work for you.

By the way, if you are learning C++ from a book that tells you to use `<iostream.h>' and `void main()', use it to start fires or something, but don't read it. Buy a more recent one.

Share this post


Link to post
Share on other sites

[quote name='Narf the Mouse' timestamp='1333472317' post='4927923']
The number of items in an array in C++ is increased by creating a new, larger array; copying the items from the old array to the new one; deleting the old array and then setting your array variable to the new array.


Or you can use std::vector and let the library do the work for you.
[/quote]
True. Suppose I should have asked if he really needed an array.

Share this post


Link to post
Share on other sites
"vector"!That's a good method ,i have already forgot it.I should spend some times learning it again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement