# Add item to a position in an array

Add item to an array, programming exercise.

It’s an essential to every array-making-coder to know how to create and manage arrays. And for every hacker to remove or add items to an array. Or other databases and such.

Create a function that takes an array (of type <string>), position(counting from zero), and a item(string), then adds that item in the position mentioned.

```Example:
input: {"a","b","ddd"}, 2, "c"
output: {"a","b","c","ddd"} ```

Scroll down for the solution of this nice coding problem for beginners.

The plan is to create a second array. Copy the elements from 0 to position. Then copy the item. Then copy the rest of the elements.

1) Its a good idea to decide what to do when the position is outside of the boundary of the array. I thing it should do nothing.
2) Then we need to declare the second array. The length of the second array should naturally be 1 more the the original.
3) We copy the elements form 0 to position.
4) Copy the item on the right position.
5) Copy the rest of the elements.
6) return the result.
7) Subscribe, Like, Share, Make a salad, Drink some beer, Watch a TV show.. and do some programming exercise.

```    /// <summary>
/// Adds a string to a position. Makes coffee. And doesn't play chess.
/// </summary>
public static string[] SetItemOnPosition (string[] arr, int position, string item)
{
//Step 1
if (position > arr.Length|| position<0) return arr;
//Step 2
string[] r = new string[arr.Length+1];
//Step 3
for (int i = 0; i < position; i++)
{
r[i] = arr[i];
}
//Step 4
r[position] = item;
//Step 5
for (int i = position+1; i <= arr.Length; i++)
{
r[i] = arr[i - 1];
}
//Step 6
return r;
}```

In you programming future, you will start to make larger programs. ( Like 100 or more lines of code ðŸ˜‰ And you will start to loose your cognitive abilities somewhat proportionally to the lines of code you are writing. Your brains short term memory have very little RAM, so we as a human beings, we have to aim to reduce the complexity of the code we are writing. There are many ways to do that. If one is a philosopher or a psychologist, one can make a name for oneself writing a book or two about that problem. But for now, you can start, by first spending a lot of time preparing a plan for your more-then-100-lines-of-code programs. The more time you spend preparing before the construction of the code, the better and EASYER time you will have coding. Because your brain will have more free cognitive resources focused on the coding only. Learn how to make it easier for your brain.