
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
Initialization of a normal array with one default value in C++
An array is a fixed-size collection of elements of the same data type that stores data in contiguous memory locations. Initializing an array with one default value in C++ is an easy task and can be easily implemented using various approaches that we are going to understand with code examples.
Initializing Array with Zeroes in C++
To initialize all elements of an array with 0, we will simply initialize the array with 0 or just use empty curly braces.
Example
Here is an example that uses the above mentioned methods to initialize the array with 0:
#include <iostream> using namespace std; int main() { int a[10] = {0}; int d[10] = {}; cout << "Elements of array a: "; for (int i = 0; i < 10; i++) { cout << a[i] << " "; } cout << "\n"; cout << "Elements of array d: "; for (int i = 0; i < 10; i++) { cout << d[i] << " "; } }
The output of the above code is as follows:
Elements of array a: 0 0 0 0 0 0 0 0 0 0 Elements of array d: 0 0 0 0 0 0 0 0 0 0
Here is a list of approaches to initialize an array with one default value:
Using for Loop
The following approach uses the for loop to initialize all elements with a specific value. We have used two arrays: arr and arr2.
- The first array arr uses arr[10] = {5} to initialize all elements with 5, but here only the first element is set to 5, and the rest are 0.
- The second array arr2, uses a for loop to iterate over all the elements starting from 0th index to end of the array (n-1) and setting each element to specific value.
Example
The following example initializes all array elements of arr2 to 5 using for loop:
#include <iostream> using namespace std; int main() { int arr[10] = {5}; cout << "Elements of array arr: "; for (int i = 0; i < 10; i++) { cout << arr[i] << " "; } cout << "\n"; int arr2[10]; cout << "Elements of array arr2: "; for (int i = 0; i < 10; i++) { arr2[i] = 5; // Initializing array elements to 5 cout << arr2[i] << " "; // Displaying array elements } cout << "\n"; return 0; }
The output of the above code is as follows:
Elements of array arr: 5 0 0 0 0 0 0 0 0 0 Elements of array arr2: 5 5 5 5 5 5 5 5 5 5
Using array::fill() Function
The array::fill() function assigns a specific value to all elements of an array.
Example
The following example uses the array::fill() function to set all elements of the array to 5:
#include <iostream> #include <array> using namespace std; int main() { array<int, 10> arr; arr.fill(5); cout << "Elements of Array arr: "; for (int i = 0; i < 10; ++i) { cout << arr[i] << " "; } }
The output of the above code is as follows:
Elements of Array arr: 5 5 5 5 5 5 5 5 5 5
Using vector
To initialize an array with one default value, we have used vector. It will create an object v and it uses constructor v(10, 5) that creates a vector array of 10 elements and initialize each element to 5.
Example
Here is an example using vector to initialize all the array elements with 5:
#include <iostream> #include <vector> using namespace std; int main() { // Array of 10 elements, each element is 5 vector<int> v(10, 5); cout << "Elements of Array v: "; for (int i = 0; i < 10; ++i) { cout << v[i] << " "; } }
The output of the above code is as follows:
Elements of Array v: 5 5 5 5 5 5 5 5 5 5