Initialize int array with range 1 to n in C++
This post will discuss how to initialize an integer array with the range 1 to n in C++.
1. Using std::iota
The std::iota function can be used to assign successive values to every element in the specified range. For example, the following solution fills an integer array with consecutive values starting from 1.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <numeric> #include <iterator> int main() { const int n = 10; int arr[n]; int start = 1; std::iota(std::begin(arr), std::end(arr), start); for (int &i: arr) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
Before C++ 11, do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <numeric> int main() { const int n = 10; int arr[n]; int start = 1; std::iota(arr, arr + n, start); for (int &i: arr) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
If you’re using std::array, the usage remains similar:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <array> #include <numeric> int main() { const int n = 10; std::array<int, n> arr; int start = 1; std::iota(arr.begin(), arr.end(), start); for (int &i: arr) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
2. Using std::generate_n
Alternatively, you can use the std::generate_n function to generate consecutive values with successive calls to a generator function and assign them to an array. A typical implementation of this approach would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <algorithm> struct increment { int value; increment(): value(0) {} int operator() () { return ++value; } }; int main() { const int n = 10; int arr[n]; std::generate_n(arr, n, increment()); for (int &i: arr) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
The usage remains similar for std::array with class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <array> #include <algorithm> class increment { int value; public: increment(): value(0) {} int operator() () { return ++value; } }; int main() { const int n = 10; std::array<int, n> arr; std::generate_n(arr.begin(), n, increment()); for (int &i: arr) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
3. Using for loop
Finally, you can use a regular for loop to fill the array with consecutive numbers, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <numeric> int main() { const int n = 10; int arr[n]; int start = 1; for (int i = 0; i < n; i++) { arr[i] = start++; } for (int &i: arr) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10
That’s all about initializing an integer array with the range 1 to n in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)