C++ Program to get the first item from the array


To hold multiple elements of the same type in consecutive locations or in such a way that can be accessed sequentially. The array is one of the greatest choices. Almost any programming languages, arrays, or similar data structures are available for data holding. Arrays are linear data structure because basic operations like insert, delete, traverse, and update takes linear time to execute. Accessing array elements are also an easy task. In this article, we will see how to pick the first item from an array in C++.

Understanding the concept with examples

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
The first element is 10

For example, like the given array in the above example, the first element can be accessed using its index position. In C++ (and some other programming languages like java, python) array indices start from index 0. So to read the first index we just pick the element from the index 0.

Algorithm

  • Take an array A as input

  • first_element := is taken using A[ 0 ]

  • return first_element

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;
}

int pickFirstElement( int A[], int n) {
   int first;
   first = A[ 0 ];
   return first;
}

int main() {
   int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   cout << "Given Array: ";
   displayArr( A, n );
   
   int first = pickFirstElement( A, n ); 
   cout << "The first element of A: " << first << endl;
   
   int B[ Z ] = { 98, 12, 10, 23, 45, 74 };
   int m = 6;
   
   cout << "Another array: ";
   displayArr( B, m );
   
   first = pickFirstElement( B, m ); 
   cout << "The first element of B: " << first << endl;
}

Output

Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
The first element of A: 57
Another array: 98, 12, 10, 23, 45, 74, 
The first element of B: 98

Using pointers and base address

Arrays are the base (first) position address along with the offset (indices). So another method without the square brackets to access indices is by using pointers. To get the first element, the value of the base address of the array can be used. Let us see the implementation for a clearer view.

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;
}

int pickFirstElement( int A[], int n) {
   int first;
   first = *A;
   return first;
}

int main() {
   int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   cout << "Given Array: ";
   displayArr( A, n );
   
   int first = pickFirstElement( A, n ); 
   cout << "The first element of A: " << first << endl;
   
   int B[ Z ] = { 98, 12, 10, 23, 45, 74 };
   int m = 6;
   
   cout << "Another array: ";
   displayArr( B, m );
   
   first = pickFirstElement( B, m ); 
   cout << "The first element of B: " << first << endl;
}

Output

Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
The first element of A: 57
Another array: 98, 12, 10, 23, 45, 74, 
The first element of B: 98

Here the value of A (written with pointer *A) indicates the value of the address pointed by A. This is the base address for the array.

Using Vectors

Vectors are dynamic arrays, otherwise, the entire thing is similar to the arrays. Here also to read the first element, we just access the first index which is 0. The code is like below −

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;
} 

int pickFirstElement( vector<int> A) {
   int first;
   first = A[ 0 ];
   return first;
}

int main() {
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; 
   
   cout << "Given Array: ";
   displayArr( A );
   
   int first = pickFirstElement( A ); 
   cout << "The first element of A: " << first << endl;
   
   vector<int> B = { 98, 12, 10, 23, 45, 74 }; 
   
   cout << "Another array: ";
   displayArr( B );
   
   first = pickFirstElement( B ); 
   cout << "The first element of B: " << first << endl;
}

Output

Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
The first element of A: 57
Another array: 98, 12, 10, 23, 45, 74, 
The first element of B: 98

Using the vector iterators begin() function

In the previous method, we are taking elements using index 0 but there is another possible way out. We can use the begin() method which returns the address of the first element. Let us see the code for a clearer view.

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;
} 

int pickFirstElement( vector<int> A) {
   int first;
   first = *A.begin();
   return first;
}

int main() {
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; 
   
   cout << "Given Array: ";
   displayArr( A );
   
   int first = pickFirstElement( A ); 
   cout << "The first element of A: " << first << endl;
   
   vector<int> B = { 98, 12, 10, 23, 45, 74 }; 
   
   cout << "Another array: ";
   displayArr( B );
   
   first = pickFirstElement( B ); 
   cout << "The first element of B: " << first << endl;
}

Output

Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
The first element of A: 57
Another array: 98, 12, 10, 23, 45, 74, 
The first element of B: 98

Conclusion

For the method to read the first element from the array, we have seen four different methods. The first two are based on the static array implementation in C++. To read the first element we just take the element from index 0. The same thing can be done using the pointer of the base address of the array. The base address points to the first block and the value present at that index will be the first element. In the next two methods, we have used vectors. Here also the approaches are the same as static arrays. The final method uses begin() for vector iterators which return the address of the first element in vector elements.

Updated on: 13-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements