c++ initialize array 1 to n

To initialize an array in C++ with values from 1 to n, you can follow these steps:

  1. Declare an integer array variable with the desired size, n. For example, if n is 10, you can declare an array as follows: int array[n];

  2. Use a loop to assign values to each element of the array. In this case, since you want to initialize the array with values from 1 to n, you can use a for loop that iterates from 0 to n-1. Inside the loop, assign the value of the loop variable plus 1 to each element of the array. Here's an example: for (int i = 0; i < n; i++) { array[i] = i + 1; }

That's it! After executing these steps, the array will be initialized with values from 1 to n, where the first element is 1, the second element is 2, and so on, up to the nth element.