In this article, we will learn how to initialize an array with range 1 to n in C++.
Table Of Contents
Suppose if n is 10, then array contents whould be,
1 2 3 4 5 6 7 8 9 10
We can think of solving the given problem in three ways. Let’s understand all the approaches in details one by one.
Initialize array with range using std::generate_n()
The very first and basic approach to initialize an array with range 1 to n in C++ is using std::generate_n() function. It is used to generate consecutive values with successive calls to a generator function and assign them to an array. This approach would be implemented in the following way:
Frequently Asked:
// using generate_n predefined function // to initialize array with range 1 to n. #include <iostream> #include <algorithm> using namespace std; // generator function that increment the value // by 1 after each successive call int generator() { static int val = 0; return ++val; } int main() { // size of array int n = 10; // declaration of array int arr[n]; // applying the generate_n function to initialize // the array with range 1 to n std::generate_n(arr, n, generator); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
Output
1 2 3 4 5 6 7 8 9 10
Initialize array with range using std::iota
The second approach to initialize an array with range 1 to n in C++ is using std::iota() function. In this approach, every element in the specified range can be assigned consecutive values using the std::iota() function. For example, the following approach, fills an integer array with consecutive values starting at 1.
// using std::iota predefined function to // initialize array with range 1 to n. #include <iostream> #include <algorithm> #include <numeric> using namespace std; int main() { // size of array const int n = 10; // declaration of array int arr[n]; // taking start as 1 int start = 1; // applying the iota function to initialize // array with range 1 to n. std::iota(std::begin(arr), std::end(arr), start); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
Output
1 2 3 4 5 6 7 8 9 10
Initialize array with range using For loop
The third approach that we are going to learn is using for-loop to initialize an array, with the range from 1 to n. We can use the for-loop to fill the array with consecutive numbers, as implemented below:
Best Resources to Learn C++:
// using for-loop to initialize array with range 1 to n. #include <iostream> using namespace std; int main() { // size of array int n = 10; // declaration of array int arr[n]; // taking start as 1 int start = 1; // Applying for loop to initialize array with range 1 to n. for (int i = 0; i < n; i++) { arr[i] = start; // increment start by 1 start++; } for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
Output
1 2 3 4 5 6 7 8 9 10
Summary
We have seen a detailed explanation of three approaches to initialize an array with range 1 to n. The first one is using std::generate_n() function, second is using std::iota() function, third one is using for-loop. Also, we have seen the detailed explanation, working and code of all the three approaches. Happy Coding.