Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Initialize Normal Array with One Default Value in C++



An array stores a collection of data or a collection of variables of the same type of fixed size. 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.

In this article, our task is to initialize an array with one default value in C++. First, we will discuss how to initialize an array with zeroes, and then different approaches to initialize an array with one default value.

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. It initializes all the array elements with 0.

Example

In this example, we have used both the methods mentioned above, i.e., either initialize all elements with 0 (int a[10] = {0}) or use empty curly braces (int d[10] = {}):

#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, with step-wise explanation and complete example codes in C++:

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 set the value of each element to 5.

Example

Here is an example implementing the above steps to initialize all the elements of an array with 5 using a for loop in C++:

#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

In this approach to initialize an array with one default value, we have used the array::fill() function. The array::fill() function assigns a specific value to all elements of an array. Here we have assigned 5 to all elements of the array.

Example

The following example uses the array::fill() function to set one specific, value i.e., 5, to all elements of the array:

#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 a vector to initialize a vector array of 10 elements, each set to 5 using vector<int> v(10, 5).

Example

In this example, we have used a vector to set all the elements to 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
Updated on: 2025-05-20T13:30:37+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements