Click here to Skip to main content
15,885,066 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 the pseudocode of insertion into an Array List. Recall that we assume that all elements in the array must be contiguous. Also, note that the pseudocode uses 0-based indexing.

insert(element, index): // inserts element into array and returns True on success (or False on failure)
    // perform the required safety checks before insertion
    if index < 0 or index > n:   // invalid indices
        return False

    if n == array.length:        // if array is full
        newArray = empty array of length 2*array.length
        for i from 0 to n-1:     // copy all elements of old array to new array
            newArray[i] = array[i]
        array = newArray         // replace old array with new array

    // perform the insertion algorithm
    if index == n:               // insertion at end of array
        array[index] = element   // perform insertion

    else:                        // general insertion
        for i from n-1 to index: // make space for the new element (if insertion not at the end)
            array[i+1] = array[i]
        array[index] = element   // perform insertion

    n = n+1                      // increment number of elements
    return True


What I have tried:

I am trying to do this by going step by step as per algorithm but getting errors after errors.
Posted
Updated 15-Sep-18 6:59am
Comments
phil.o 15-Sep-18 9:19am    
First thing would be to use the "Improve question" button, and qualify the What I have tried: section with the code you have written so far. A description of the errors you are getting would be a good idea, too.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
If you are getting "errors after errors" with code you have written to do this, then show us exactly what you tried, tell us what the errors are, copy and paste any messages, and explain what you have tried in order to fix them.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
When you want to code in C++ you need to learn the syntax. Primary a lot of braces are missing and the usage of ":" is completly wrong.

You need to learn from some C++ tutorial to use this language. Install the Visual Studio and dig into some sample code. ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900