| | |
appending and subtracting a number in an array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>How do u append in an array.
If u want to add at end then simply do
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
If u want to add at end then simply do
C++ Syntax (Toggle Plain Text)
arrayname[positionnumber-1]=value
>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
Try using the std::vector template class.
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?
C++ Syntax (Toggle Plain Text)
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?
![]() |
Similar Threads
- Trying To Find The Square Root Of Each Number Of A Array (C++)
- Insert a number in an array (C++)
- help with inserting a number into an array (C++)
Other Threads in the C++ Forum
- Previous Thread: works for static need help to make it dynamic
- Next Thread: linked list small problem with Insert Function
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





