Sign in to follow this  
MotorBully

How to add a line to the two dimensional array?

Recommended Posts

The question is the same as topic.Here is the first code[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[i] = new int[n];
}
for(i =0;i<m;i++)
{
for(j= 0;j<n;j++)
{
data[i][j] = i*j;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"data["<<i<<"]["<<j<<"]="<<data[i][j]<<"\n";
}
}[/code]
then ,i want to add a line to the "array"(a array) and give the number to this line which is [code] m = m+1;
for(i=0;j<n;j++)
{
data[m][j]=(m+2)*j;
}[/code]
Is that adding "m += 1" to the code right?

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.

[code]
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.
}
[/code]

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.
[/quote]

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='alvaro' timestamp='1333472619' post='4927926']
[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.
[/quote]

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this