
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fill an Array with a Specific Element in C++
Arrays are homogeneous data structures to hold a similar type of data in consecutive memory locations which can be addressed using base addresses and indices. There are plenty of different applications where we use arrays to hold data for suitable uses. Inserting elements into an array is one of the cumbersome processes. We can either insert them by taking inputs from users in a loop or insert them from a file or there are such other processes to insert them. To initialize an array with a specific value (insert that value at all positions of that array) has also a few different approaches. In this article, we will see how to create an array of size n, and insert element k at all locations inside it using C++.
Understanding the concept with examples
Given array length n = 10, insert k = 5 at every location inside the array. The array A will be like this: A = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
The simplest method is using loops to fill the array with the element k. Let us see the first algorithm where we are using for loops to insert k at each place in the array A.
Algorithm
Take the array A of size n, and element k to insert
-
for index i ranging from 0 to n - 1, do
A[ i ] := k
end for
return A
Example
#include <iostream> # define Z 30 using namespace std; void displayArr(int arr[] ) { for( int i = 0; i < Z; i++ ){ cout << arr[ i ] << ", "; } } void initializeArray( int A[], int k ){ for( int i = 0; i < Z; i++ ){ A[ i ] = k; } } int main() { int arr[ Z ]; cout << "Initialize array with value 15" << endl; initializeArray( arr, 15); cout << "Array elements: " << endl; displayArr( arr ); }
Output
Initialize array with value 15 Array elements: 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
Using Vector Constructor
Unlike static arrays, vectors can be used to create dynamic arrays. To initialize a vector with some element, we can use the second argument inside the vector constructor. The vector constructor takes the first element which is its size, and the second element is the initialization value. Let us see the code for a clear understanding.
Example
#include <iostream> #include <vector> # define Z 30 using namespace std; void displayArr( vector<int> v ){ for( auto e : v ){ cout << e << ", "; } } int main() { cout << "initialize vector with 20:" << endl; vector<int> arr( Z, 20 ); cout << "Array elements: " << endl; displayArr( arr ); }
Output
initialize vector with 20: Array elements: 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
Conclusion
In this article, we have seen two methods to initialize an array with a specific value. The first method uses static arrays where we can assign using loops. In the next method, dynamic arrays or vectors are begin used. For vectors, elements can be assigned using constructors but it has a pre-condition. The array must have a few pre-defined places to hold data. So the first parameter which is the size of the vector must be provided, then the second element is the key element which will be placed at each location in the vector.