An array is a homogeneous data structure used to hold similar types of data in contiguous memory locations that can be accessed using base addresses and indexes. There are many different applications where we use arrays to hold data that is suitable for the appropriate purpose. Inserting elements into an array is one of the tedious processes. We can insert them by looping to get input from the user, or insert them from a file, or there are some other ways of inserting them. There are also a few different ways of initializing an array to a specific value (inserting that value at all positions in the array). In this article, we will see how to create an array of size n and insert element k at all positions in it using C.
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 way is to use a loop to fill element k into the array. Let's look at the first algorithm where we insert k into each position of the array A using a for loop.
Take out the array A of size n and insert element k
For index i from 0 to n-1, perform the following operations
A[i]:=k
End loop
Return A
#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 ); }
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,
Unlike static arrays, vectors can be used to create dynamic arrays. To initialize a vector with an element, we can use the second parameter inside the vector constructor. The vector constructor accepts the first element as its size and the second element as the initialization value. Let's take a look at the code for a clearer understanding.
#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 ); }
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,
In this article, we saw two ways to initialize an array with specific values. The first method uses a static array and we can assign it using a loop. In the next method, dynamic arrays or vectors are used. For vectors, you can use the constructor for assignment, but there is a prerequisite. Arrays must have some predefined locations to store data. Therefore, the size of the vector must be provided as the first argument, and then the second element is the key element to be placed at each position of the vector.
The above is the detailed content of C++ program to fill specific elements of an array. For more information, please follow other related articles on the PHP Chinese website!