HOW TO INSERT A VALUE IN AN ARRAY

I need to insert a value in an already filled array, say index array[9]. I've tried using two for loops but i don't seem to get it right. I don't know how to shift the filled indexes one space up to allow me to insert a value where i want to insert it.
o_O

Do you mean assign a value?
1
2
int myArray[10];
myArray[5] = 100; // The 6th element is now equal to 100! 

Or do you mean INSERT a value between two existing elements?
1
2
std::vector<int> myArray[9];
myArray.insert(myArray.begin()+5, 100); // Inserts '100' between 5th and 6th elements and increases array size by 1. 

To use the last example, you'll have to #include <vector>. It's a dynamically-sized array!
If you mean you want to add a new value in the middle, and then the array has increased in size by one, you have to create a whole new array and copy the contents across yourself. If you simply move the last value along one, and something else is already using that memory, you'll write over that something else.

This being C++, there are containers that provide this sort of thing without you having to manually copy elements yourself. For example, the vector container class.

If you insist on using an array, this code demonstrates doing such a thing. Note that tthe new aray was dynamically allocated, requiring the user to delete it herself. It's by no means the most elegant way, but it shows the steps clearly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

int* insertValue (int* originalArray, int positionToInsertAt, int ValueToInsert, int sizeOfOriginalArray)
{
  // Create the new array - user must be told to delete it at some point
  int* newArray = new int[sizeOfOriginalArray + 1];

  
  for (int i=0; i<=sizeOfOriginalArray; ++i)
  {
    if (i < positionToInsertAt)  // All the elements before the one that must be inserted
    {
       newArray[i] = originalArray[i];
    }
  
    if (i == positionToInsertAt)  // The right place to insert the new element
    {
      newArray[i] = ValueToInsert;
    }
 
    if (i > positionToInsertAt)  // Now all the remaining elements
    {
      newArray[i] = originalArray[i-1];
    }
  }
return newArray;
}


int main()
{
  int x[5]={0, 1, 2, 3, 4};
  for (int y=0;y<5;y++)
    std::cout << x[y] << " ";
  std::cout << std::endl;
   int* z = insertValue(x,3,7,5);
   for (int y=0;y<6;y++)
    std::cout << z[y] << " ";
  delete[] z;
  return 0;

}



The key to this is understanding what an array is; it is no more than a number of objects of the same type all next to each other in memory. You cannot just push some more memory in the middle; all you can do is change the values in the memory, and if you need more memory you have to organise it yourself.
Last edited on
Again, in template form, so it can be applied to any kind of array.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>

template <class T>
T* insertValue (T* originalArray, int positionToInsertAt, T ValueToInsert, int sizeOfOriginalArray)
{
  // Create the new array - user must be told to delete it at some point
  T* newArray = new T[sizeOfOriginalArray + 1];
  for (T i=0; i<=sizeOfOriginalArray; ++i)
  {
    if (i < positionToInsertAt)
    {
       newArray[i] = originalArray[i];
    }
  
    if (i == positionToInsertAt)
    {
      newArray[i] = ValueToInsert;
    }
 
    if (i > positionToInsertAt)
    {
      newArray[i] = originalArray[i-1];
    }
  }
return newArray;
}


int main()
{
  int x[5]={0, 1, 2, 3, 4};
  for (int y=0;y<5;y++)
    std::cout << x[y] << " ";
  std::cout << std::endl;
   int* z = insertValue(x,0,7,5);
   for (int y=0;y<6;y++)
    std::cout << z[y] << " ";
  std::cout << std::endl;
  delete[] z;


  char a[4] = { 'a', 'b', 'c', 'd'};
 for (int y=0;y<5;y++)
    std::cout << a[y] << " ";
  std::cout << std::endl;
   char* b = insertValue(a,2,'z',5);
   for (int y=0;y<6;y++)
    std::cout << b[y] << " ";
  delete[] b;

  return 0;

}

Last edited on
thank you Gaminic. its the second method that i want, however my lecturer asked that we use a for loop or maybe nested for loops instead of pre-defined function like insert. basically the array size must be initialized to 100 maximum. the programm must then input exam marks into this array. after that the marks should be arranged from small to biggest. lastly a random value between 1 and 999 must be inserted in element 5 of an array by adjusting the array size by one space up to avoid over-writting the value already contained in element 5. NB the number of marks can been anything but not greater than 100 or less than -1. I hope this clarifies my problem. thank you.
Then Moschops' solution is perfect for your needs!
Topic archived. No new replies allowed.