| | |
Adding things to Array-Based Lists
![]() |
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Solved Threads: 0
I have a question about adding say integers to an array-based list of numbers.
I know how to add something at the end it would be something like:
values[count] = newValue;
count ++;
What about if someone wanted to add something to the beginning or somewhere between? My book doesn't say how to do such things because with an unsorted list order doesn't matter so it only thinks you would just throw things on the end. But what if you didn't want to?
I know how to add something at the end it would be something like:
values[count] = newValue;
count ++;
What about if someone wanted to add something to the beginning or somewhere between? My book doesn't say how to do such things because with an unsorted list order doesn't matter so it only thinks you would just throw things on the end. But what if you didn't want to?
>What about if someone wanted to add something to the beginning or somewhere between?
It's more or less the same deal. You move everything from the index you want to add to, to the end of the array forward by one element. Then you add the new value to the index you want and increase the count:
It's not desirable because it's potentially very expensive to shift array elements like that.
It's more or less the same deal. You move everything from the index you want to add to, to the end of the array forward by one element. Then you add the new value to the index you want and increase the count:
C++ Syntax (Toggle Plain Text)
for ( int i = count; i > index; i-- ) values[i] = values[i - 1]; values[index] = newValue; ++count;
Last edited by Narue; Oct 16th, 2007 at 6:29 pm.
I'm here to prove you wrong.
If you want to keep all existing values in the array then you need use a loop to shift everything down from the spot you want to add the new value.
[edit]^^^ what Narue said [/edit]
[edit]^^^ what Narue said [/edit]
Last edited by Ancient Dragon; Oct 16th, 2007 at 6:31 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Problem on array based list (C)
- Adding Content to image based web pages? (Site Layout and Usability)
- Graphics In Pixel: Part II B (C)
- reading a file into code (Java)
- Error listing files... (C++)
Other Threads in the C++ Forum
- Previous Thread: hangman program stuck.. nid help.
- Next Thread: Help with Loop (C++/MFC)
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






