Home >Backend Development >C++ >C++ program to get the last given number of items in an array
Array is a data structure specially designed to retain the same type of data in a series of memory areas. The main benefit of using arrays is that we can access them from any position using index parameters. However, inserting and deleting data requires sequential operations, which will make this data structure a linear data structure. We can simply use the index or position number in square brackets to extract elements from the array. This article will demonstrate how to read the nearest k numbers from an array in C.
Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] We have another number k = 4 The number of elements in A is 9 The output will be the last k elements from A, which are: 12, 35, 74, 69
We have the elements inside the array for every array, and the quantity n is also crucial. The number n indicates how many valid elements are there in an array. The size of the array might not match the n. Although an array can have a maximum of Z elements, only n of them must be valid; the remaining slots are empty. In this case, k must be less than or equal to n in order to retrieve the kth element from the array. Before taking up the components, we must inspect it. For a better understanding, let's have a look at the algorithm.
Read an array A as input. Accepts the number of elements simultaneously: n and k, to read the first k elements in A
Create an empty array B
If k
for i in range 0 to k - 1, do
B[ i ] = A[ n - k i ]
end for
end if
Return B
#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 pickLastKElement( int A[], int n, int B[], int &m, int k) { if( k <= n ){ for( int i = 0; i < k; i++ ) { B[ i ] = A[ n - k + i ]; m = m + 1; } } } int main() { int A[ Z ] = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14}; int n = 12; int B[ Z ]; int m = 0; cout << "Given Array: "; displayArr( A, n ); pickLastKElement( A, n, B, m, 7 ); cout << "The last 7 element from A: "; displayArr( B, m ); m = 0; pickLastKElement( A, n, B, m, 10 ); cout << "The last 10 element from A: "; displayArr( B, m ); }
Given Array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14, The last 7 element from A: 52, 86, 14, 76, 65, 32, 14, The last 10 element from A: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,
In the above method, static array is used to store and retrieve array elements. The same functionality can also be achieved using vectors. Vectors are part of the C STL and are dynamic arrays. Let's take a look at the code. The algorithm remains unchanged.
#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> pickLastKElement( vector<int> A, int k) { vector<int> B; if( k <= A.size() ){ for( int i = 0; i < k; i++ ) { B.push_back( A[ A.size() - k + i ] ); } } return B; } int main() { vector<int> A = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14}; vector<int> B; cout << "Given Array: "; displayArr( A ); B = pickLastKElement( A, 7 ); cout << "The last 7 element from A: "; displayArr( B ); B = pickLastKElement( A, 10 ); cout << "The last 10 element from A: "; displayArr( B ); }
Given Array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14, The last 7 element from A: 52, 86, 14, 76, 65, 32, 14, The last 10 element from A: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,
The last method is to manually create an empty vector and copy the elements one by one. However, we can directly use the vector iterator to copy the last k elements in the vector constructor. Let's take a look at the code to understand this concept.
#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> pickLastKElement( vector<int> A, int k) { vector<int> B( A.begin() + (A.size() - k), A.end() ); return B; } int main() { vector<int> A = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14}; vector<int> B; cout << "Given Array: "; displayArr( A ); B = pickLastKElement( A, 7 ); cout << "The last 7 element from A: "; displayArr( B ); B = pickLastKElement( A, 10 ); cout << "The last 10 element from A: "; displayArr( B ); }
Given Array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14, The last 7 element from A: 52, 86, 14, 76, 65, 32, 14, The last 10 element from A: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,
Here, the B vector is created using the last k elements of the A vector. Use the begin() method to get the address of the first item, and use the offset begin() (A.size() − k) as the end point so you can point to The last k elements.
This article describes three different ways to read or select the last n numbers from a given array. The second and third solutions are based on vectors rather than the static default array used by the first approach. The answers to the first two questions are simple. We use a for loop to copy the last k elements one by one. The last technique is the simplest and uses a vector constructor to generate a vector by copying the components using an iterator of another vector.
The above is the detailed content of C++ program to get the last given number of items in an array. For more information, please follow other related articles on the PHP Chinese website!