Home > Backend Development > C++ > C++ program to add element to beginning of array

C++ program to add element to beginning of array

WBOY
Release: 2023-09-08 15:01:02
forward
871 people have browsed it

C++ program to add element to beginning of array

By using arrays and data structures, it is possible to store homogeneous (identical) data in multiple memory locations. The key benefit of using arrays is that we can retrieve them from any position using the index parameter. This data structure becomes linear because data must be inserted and extracted step by step. We just need to put the index or position number of the element inside square brackets to retrieve it from the array. In this article, we will use an array A and another element e. We will insert e in C at the starting position of A.

Understand concepts and illustrate with examples

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
After inserting 23 at the end, the array will look like this:
[23, 10, 14, 65, 85, 96, 12, 35, 74, 69]
Copy after login

In the above example, we have an array A containing nine elements. We will insert another element 23 at the beginning of array A. The resulting array contains all elements plus the leading 23. To insert an element at the beginning we have to move all elements one position to the right, then the first slot will be empty and we put the new element in that position. Let’s take a look at the algorithm for a clearer understanding.

algorithm

  • Take an array A and an element e

  • If array A has enough space to insert element e, then

    • For i in the range from n-1 to 0, do the following:

      • A[ i 1 ] = A[ i ]

    • End loop

    • A[0]=e

    • Increase n 1

  • End if

  • Return A

The Chinese translation of

Example

is:

Example

#include <iostream>
# define Z 50

using namespace std;

void displayArr(int arr[], int n){
   for( int i = 0; i < n; i++ ){
      cout << arr[ i ] << ", ";
   }
   cout << endl;
}
void insertAtBeginning( int A[], int &n, int e ){
   if( n + 1 < Z ) {
      for( int i = n - 1; i >= 0; i-- ) {
         A[ i + 1 ] = A[ i ];
      }
      A[ 0 ] = e;
      n = n + 1;
   } 
}

int main() {
   int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   cout << "Array before insertion: ";
   displayArr( A, n );
   
   cout << "Inserting 58 at the beginning:" << endl;
   insertAtBeginning( A, n, 58 );
   
   cout << "Array after insertion: ";
   displayArr( A, n );
   
   cout << "Inserting 225 at the beginning:" << endl;
   insertAtBeginning( A, n, 225 );
   
   cout << "Array after insertion: ";
   displayArr( A, n );
}
Copy after login

Output

Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 58 at the beginning:
Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 225 at the beginning:
Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
Copy after login
Copy after login

Use a vector to insert an element in front

Vector is a dynamic data structure that is part of the C STL. We can get similar functionality to arrays in vectors. But in vector we can insert only at the end or behind. There is no direct way to insert at the beginning. However, we can move the element one position back as before and insert the new element at the beginning. Or we can create another one-element vector containing only the new elements and then concatenate them. Therefore, the resulting vector will contain all previous elements and the new element at the beginning. Let's look at the algorithm and C implementation.

algorithm

  • Take an array A and an element e

  • Create an empty vector B

  • Insert e into B

  • A := Connect B and A (first B and then A)

  • Return A

The Chinese translation of

Example

is:

Example

#include <iostream>
#include <vector>
# define Z 50

using namespace std;

void displayArr( vector<int> v ){
   for( int i = 0; i < v.size() ; i++ ){
      cout << v[ i ] << ", ";
   }
   cout << endl;
}

vector<int> insertAtBeginning( vector<int> A, int e ){
   vector<int> B( 1 );
   B[ 0 ] = e;
   B.insert( B.end(), A.begin(), A.end() );
   return B;
}

int main() {
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};  
   cout << "Array before insertion: ";
   displayArr( A );
   
   cout << "Inserting 58 at the beginning:" << endl;
   A = insertAtBeginning( A, 58 );
   
   cout << "Array after insertion: ";
   displayArr( A );
   
   cout << "Inserting 225 at the beginning:" << endl;
   A = insertAtBeginning( A, 225 );
   
   cout << "Array after insertion: ";
   displayArr( A );
}
Copy after login

Output

Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 58 at the beginning:
Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 225 at the beginning:
Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
Copy after login
Copy after login

in conclusion

In this article, we have seen how to insert elements at the beginning of an array. Here we discuss two different solutions. The first solution uses static C arrays, while the second solution uses vectors. Vectors have no way to insert elements directly at the beginning. We can insert elements directly at the end using the push_back() method. To do this, we use a trick where we create an array of size 1 and insert the new element into it. Then concatenate it with the given array. We can achieve the same effect using lists. However, C lists have push_front() method, which can insert elements directly in front. But lists are not arrays, they are linked lists.

The above is the detailed content of C++ program to add element to beginning of array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template