appending and subtracting a number in an array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

appending and subtracting a number in an array

 
0
  #1
Dec 15th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: appending and subtracting a number in an array

 
0
  #2
Dec 15th, 2005
oh this is for C++ too
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: appending and subtracting a number in an array

 
0
  #3
Dec 15th, 2005
See Arrays
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: appending and subtracting a number in an array

 
0
  #4
Dec 16th, 2005
eh that didnt help me much
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: appending and subtracting a number in an array

 
0
  #5
Dec 16th, 2005
>How do u append in an array.
If u want to add at end then simply do
  1. 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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: appending and subtracting a number in an array

 
0
  #6
Dec 16th, 2005
Try using the std::vector template class.
  1. std::vector<int> my_int_array;
  2.  
  3. //Add elements
  4. my_int_array.push_back(10):
  5. my_int_array.push_back(2):
  6.  
  7. //Display all the numbers
  8. for(int i=0;i<my_int_array.size();i++)
  9. {
  10. cout<<my_int_array[i];
  11. }
  12.  
  13. //Deletion of position i :P
  14. 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?
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: appending and subtracting a number in an array

 
0
  #7
Dec 16th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: appending and subtracting a number in an array

 
0
  #8
Dec 16th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: appending and subtracting a number in an array

 
0
  #9
Dec 16th, 2005
yea i did and i like that cause it helps so thanks but i was just curious
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC