DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   appending and subtracting a number in an array (http://www.daniweb.com/forums/thread36829.html)

tyczj Dec 15th, 2005 10:39 am
appending and subtracting a number in an array
 
how do u append and subtract a number in an array. i have no clue. any links to a site about this wouls greatly appreciated

tyczj Dec 15th, 2005 11:25 am
Re: appending and subtracting a number in an array
 
oh this is for C++ too

SpS Dec 15th, 2005 11:25 am
Re: appending and subtracting a number in an array
 
See Arrays

tyczj Dec 16th, 2005 12:47 am
Re: appending and subtracting a number in an array
 
eh that didnt help me much

SpS Dec 16th, 2005 1:05 am
Re: appending and subtracting a number in an array
 
>How do u append in an array.
If u want to add at end then simply do
arrayname[positionnumber-1]=value
If you want to add at the beginning or somewhere in middle then you need to shift the elements to make space for the new element.
>How do u subtract a number in an array.
I guess by this u mean removing an element...for this you need to shift elements again...which is very expensive.
If you have an array of 10 elements and you want to remove 5th element then u need to perform shifting of elements from 6th element so that, now element at 6th position becomes the element at 5th position, element at 5th position becomes the element at 4th position and so on...so that total elements are 1 less than before.
>eh that didnt help me much
That was basic array tutorial meant to make u aware of what arrays are...proper undrstanding of it will make u solve such problems...anyways here's another tutorial link
http://www.cplusplus.com/doc/tutorial/arrays.html

FireNet Dec 16th, 2005 7:04 am
Re: appending and subtracting a number in an array
 
Try using the std::vector template class.
std::vector<int> my_int_array;
 
//Add elements
my_int_array.push_back(10):
my_int_array.push_back(2):
 
//Display all the numbers
for(int i=0;i<my_int_array.size();i++)
{
  cout<<my_int_array[i];
}
 
//Deletion of position i :P
my_int_array.erase(&my_int_array[i-1],&my_int_array[i]);

If you can only use pure arrays, then you will have to learn about dynamic memory allocation to modify the size of the array at run-time.

Or were you just asking if you want to put a new value into an array of a fixed size?

tyczj Dec 16th, 2005 12:23 pm
Re: appending and subtracting a number in an array
 
Quote:

Originally Posted by FireNet

Or were you just asking if you want to put a new value into an array of a fixed size?

yea a fixed size array

could i just use pointers to add it to the array?

SpS Dec 16th, 2005 12:36 pm
Re: appending and subtracting a number in an array
 
Quote:

Originally Posted by tyczj
could i just use pointers to add it to the array?

You don't need it...plz read the link which i provided

tyczj Dec 16th, 2005 1:19 pm
Re: appending and subtracting a number in an array
 
yea i did and i like that cause it helps so thanks but i was just curious


All times are GMT -4. The time now is 1:05 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC