In this article, we will discuss different ways to initialize an array of given size with same value in C++.
Table Of Contents
- Problem Description
- Initialize an Array with same value using Initializer List
- Initialize an Array with same value using Designated Initializers
- Initialize an Array with same value using std::fill_n() function
- Initialize an Array with same value using For loop
- Initialize an Array with same value using Macros
- Summary
Problem Description
In this article, we will learn how to initialize all the elements in array with the same value in C++. We can think of solving the given problem in five ways. Let’s understand all the approaches in details one by one.
Initialize an Array with same value using Initializer List
The very first and the basic approach to initialize an array with the same value in C/C++ is to give an initializer list.
// size of array is specify int arr[5] = { 10, 10, 10, 10, 10}; // size is not specify int arr[] = { 10, 10, 10, 10, 10 };
If we provide an empty initializer list or only specify 0 in the initializer list, the array will be initialized to 0.
// output array is [0, 0, 0, 0, 0] int arr[5] = { }; // output array is [0, 0, 0, 0, 0] int arr[5] = { 0 };
Initialize an Array with same value using Designated Initializers
The second approach that we are going to learn is using the Designated Initializer. It can only be used with GCC compiler. The Designated Initializers is used when we want to initialize the array with the same value over the range.
The basic syntax is [first … last] = value;
Frequently Asked:
- Find minimum value and its index in an Array in C++
- Check if all elements of Array matches a Condition in C++
- Find maximum value and its index in an Array in C++
- How to convert an Array to List in C++?
// size of array is specify int arr[5] = {[0 ... 4] = 10}; // size is not specify int arr[] = {[0 ... 4] = 10};
Initialize an Array with same value using std::fill_n() function
The third approach that we are going to learn is using the std::fill_n() function. It is used to fill a container with default values. The fill_n() function fills the value up to the first n elements, from a given starting point. It takes iterator begin and the number of elements i.e. n, as the parameters. It fills the first n index with the specified value, starting from the position pointed by begin.
Syntax
fill_n(iterator begin, int n, type value);
// Using fill_n predefined function to initialize // the elements in an array with the same value. #include <iostream> #include <algorithm> using namespace std; int main() { int n = 5; int val = 10; // declaration of the array int arr[n]; // applying the fill_n() function with required parameters std::fill_n(arr, n, val); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout<< endl; return 0; }
Output
10 10 10 10 10
Initialize an Array with same value using For loop
The fourth approach that we are going to learn is using for-loop to initialize an array, with the similar values.
#include <iostream> using namespace std; int main() { int n = 5; int val = 10; // declaration of the array int arr[n]; // initialization of array for (int i = 0; i < n; i++) { arr[i] = val; } for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout<<endl; return 0; }
Output
10 10 10 10 10
Initialize an Array with same value using Macros
The fifth approach that we are going to learn is using macros, to initialize elements in an array with the same value. Large arrays won’t work with the initializer list, and designated initializers will only work with GCC compilers. We can use macros to initialize a large array with the same value. Implementation is shown below :
// Using macros to initialize the elements // in an array with the same value. #include <iostream> using namespace std; #define x1 10 #define x2 x1, x1 #define x4 x2, x2 #define x8 x4, x4 #define x16 x8, x8 int main() { int arr[] = {x16, x8, x4, x2, x1}; // n = 31 = (16 + 8 + 4 + 2 + 1) int n = sizeof(arr) / sizeof(int); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout<<endl; return 0; }
Output
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
Summary
We have seen a detailed explanation of five approaches to initialize elements in an array with the same value. The first one is Using Initializer List, second is Using Designated Initializers, third one is Using std::fill_n() function, fourth one is using the For loop, fifth one is Using the Macros. Also, we have seen the detailed explanation, working and code of all the five approaches. Happy Coding.