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


To store several elements of the same type in locations that are sequentially accessible or in a manner that allows for sequential access. The array is among the best options. For storing data, almost any computer language, array, or related data structure is available. Because fundamental operations like insert, remove, traverse, and update take linear time to complete, arrays are linear data structures. It is also simple to access array items. This article will demonstrate how to choose the last element in a C++ array.

Understanding the concept with examples

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

For instance, the final member can be reached using the index position, much like the given array in the previous example. Array indices in C++ (and certain other programming languages like java and python) begin at index 0. Therefore, to read the last index, we simply select the element from the index (n − 1), where n is the array's element count.

Algorithm

  • Take an array A as input

  • n := number of elements in A

  • last_element := is taken using A[ n – 1 ]

  • return last_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 pickLastElement( int A[], int n) {
   int last;
   last = A[ n - 1 ];
   return last;
}

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 last = pickLastElement( A, n ); 
   cout << "The last element of A: " << last << endl;
   
   int B[ Z ] = { 98, 12, 10, 23, 45, 74 };
   int m = 6;
   
   cout << "Another array: ";
   displayArr( B, m );
   
   last = pickLastElement( B, m ); 
   cout << "The last element of B: " << last << endl;
}

Output

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

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 last 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 pickLastElement( int A[], int n) {
   int last;
   last = *(A + n - 1);
   return last;
}

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 last = pickLastElement( A, n ); 
   cout << "The last element of A: " << last << endl;
   
   int B[ Z ] = { 98, 12, 10, 23, 45, 74 };
   int m = 6;
   
   cout << "Another array: ";
   displayArr( B, m );
   
   last = pickLastElement( B, m ); 
   cout << "The last element of B: " << last << endl;
}

Output

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

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 last element, we just access the last index which is vector.size() − 1. 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 pickLastElement( vector<int> A) {
   int last;
   last = A[ A.size() - 1 ];
   return last;
}

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

Output

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

Using the vector back() function

In the previous method, we are taking elements using index 0 but there is another possible way out. We can use the back() method which returns the last 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 pickLastElement( vector<int> A) {
   int last;
   last = A.back();
   return last;
}

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

Output

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

Conclusion

For the method to read the last 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 last 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, by adding the offset we get the last element. In the next two methods, we have used vectors. Here also the approaches are the same as static arrays. The final method uses back() for vector iterators which return the last element in a vector.

Updated on: 13-Dec-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements