- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program to get the first given number of items from the array

Arrays are special data structures which are used to store or hold similar kind of data (homogeneous data) in consecutive memory locations. The great advantage of using arrays is that, we can access them from any location we want using the index parameter. But to insert and delete it needs sequential operations which makes this data structure a linear data structure. To retrieve an element from array, we can simply use the index or the position number for that element inside the square bracket only. In this article we will see how we can read first k given numbers from the array in C++.

## Understanding the concept with examples

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 first k elements from A, which are: 10, 14, 65, 85

For any array we have the elements inside array and another parameter is also very important, which is n. The n represents the number of valid elements present inside an array. The n may not be the size of the array. Array may hold Z number of elements at maximum, but only n of them are valid, rest are free spaces. Here k must be less than or equal to n, otherwise we cannot get k number of elements from the array. We must check it before picking up the elements. Let us see the algorithm for a clear understanding.

### Algorithm

Read an array A as input. Also take number of elements in it: n and k to read first k elements from A

create an empty array B

if k < n, then

for i in range 0 to k - 1, do

B[ i ] = A[ i ]

end for

end if

return B

### 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 pickFirstKElement( int A[], int n, int B[], int &m, int k) { if( k <= n ){ for( int i = 0; i < k; i++ ) { B[ i ] = A[ i ]; m = m + 1; } } } int main() { int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; int n = 12; int B[ Z ]; int m = 0; cout << "Given Array: "; displayArr( A, n ); pickFirstKElement( A, n, B, m, 7 ); cout << "The first 7 element from A: "; displayArr( B, m ); m = 0; pickFirstKElement( A, n, B, m, 10 ); cout << "The first 10 element from A: "; displayArr( B, m ); }

### Output

Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, The first 7 element from A: 57, 10, 14, 19, 86, 52, 32, The first 10 element from A: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65,

## Using Vectors

In the above approach, the static arrays are used to store and pick elements from the array. We can do the same using vectors. Vectors are C++ STL data structures, which are dynamic arrays. Let us see the code. The algorithm is like before.

### 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> pickFirstKElement( vector<int> A, int k) { vector<int> B; if( k <= A.size() ){ for( int i = 0; i < k; i++ ) { B.push_back( A[ i ] ); } } return B; } int main() { vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; vector<int> B; cout << "Given Array: "; displayArr( A ); B = pickFirstKElement( A, 7 ); cout << "The first 7 element from A: "; displayArr( B ); B = pickFirstKElement( A, 10 ); cout << "The first 10 element from A: "; displayArr( B ); }

### Output

Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, The first 7 element from A: 57, 10, 14, 19, 86, 52, 32, The first 10 element from A: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65,

## Using the Vector Constructor

The last method is a manual process where we are creating an empty vector then copying the elements one by one. However, we can directly copy first k elements using vector iterators inside the vector constructor. Let us see the code to understand this concept.

### 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> pickFirstKElement( vector<int> A, int k) { vector<int> B( A.begin(), A.begin() + k ); return B; } int main() { vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; vector<int> B; cout << "Given Array: "; displayArr( A ); B = pickFirstKElement( A, 7 ); cout << "The first 7 element from A: "; displayArr( B ); B = pickFirstKElement( A, 10 ); cout << "The first 10 element from A: "; displayArr( B ); }

### Output

Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, The first 7 element from A: 57, 10, 14, 19, 86, 52, 32, The first 10 element from A: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65,

Here the B vector is being created with the first k elements from A vector. The begin() method is used to get the address of first item, and using the offset with** begin()** we can end up to k elements.

## Conclusion

In this article we have seen three different methods to read or pick first n numbers from a given array. The first method is using static default array, but second and third solutions are based on vectors. The first two solutions are straightforward. We are copying k elements one by one using for loops. The last method is the simplest one where we are using vector constructor which creates a vector by copying elements from another vector using its iterator.