Home > Article > Backend Development > How to represent integers from 100 to 200 in an array in C++
Use an array to represent the integer range from 100 to 200: declare an array containing 101 integer elements, indexed from 0 to 100. Use a loop to initialize the array index to 100. The elements in the array will represent integers from 100 to 200. Use indexing to access and modify array elements.
How to represent integers from 100 to 200 in C
In C, you can use arrays to represent ranges an integer within. The following is an array declaration representing integers from 100 to 200:
<code class="cpp">int numbers[101]; // 0 索引数组,范围从 100 到 200</code>
This array contains 101 integer elements, indexed from 0 to 100. To represent integers from 100 to 200, we can initialize the array index to 100.
<code class="cpp">for (int i = 0; i < 101; i++) { numbers[i] = 100 + i; }</code>
Now, the array numbers
represents the integers from 100 to 200:
Index | Value |
---|---|
0 | 100 |
101 | |
102 | |
... | |
200 |
<code class="cpp">int number = numbers[50]; // 获取第 51 个整数(即 150)</code>To modify the elements in the array, you can use Assignment Operator:
<code class="cpp">numbers[50] = 123; // 将第 51 个整数修改为 123</code>By using arrays, we can easily represent and handle ranges of integers.
The above is the detailed content of How to represent integers from 100 to 200 in an array in C++. For more information, please follow other related articles on the PHP Chinese website!