C语言 我如何制作一个包含 0 到 1000 之间的所有整数的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30928462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:59:57  来源:igfitidea点击:

How would I make an array of all the integers between 0 and 1000?

c

提问by bigpotato

Not finding this anywhere online google... And I'm not typing out the numbers one by one either because there has to be an easier way!

没有在任何地方在线谷歌找到这个......而且我也不会一个一个地输入数字,因为必须有一个更简单的方法!

int myFunction(void) { 
        //numbers = ??????;
        int sum = 0;
        for(int i = 0; i < sizeof(numbers); i++) {
                int currentNumber = numbers[i];
                if (currentNumber % 3 == 0 || currentNumber % 5 == 0) {
                        sum = sum + currentNumber;      
                }       
        }       
        printf("%d", sum);
}    

UPDATE

更新

Someone in the comments made a good point about just setting the condition of the for loop, however i'm still curious how one would make an array of integers [0,1,2,3,4..1000]

评论中有人对设置 for 循环的条件提出了很好的观点,但是我仍然很好奇如何制作整数数组 [0,1,2,3,4..1000]

回答by Matt

I'm not sure it makes any sense to actually create the array from 1-1000, if all you are going to do is numbers[i], given that numbers[i] == i. Using just a single integer i, you can save yourself from wasting the extra space. If there is motivation other than what you have posted, you can do the following:

我不确定从 1 到 1000 实际创建数组是否有意义,如果您要做的就是numbers[i],鉴于numbers[i] == i. 只使用一个 integer i,您就可以避免浪费额外的空间。如果除了您发布的内容之外还有其他动机,您可以执行以下操作:

int numbers[1001];
for(int i = 0; i <= 1000; i++){
    numbers[i] = i;
}

回答by chux - Reinstate Monica

How would I make an array of all the integers between 0 and 1000

我如何制作一个包含 0 到 1000 之间所有整数的数组

Could take the direct, and useless, approach:

可以采取直接而无用的方法:

int main(void) {
  int a[1001] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
          18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
          35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
          52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
          69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
          86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
          103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
          117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
          131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
          145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158,
          159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
          173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186,
          187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200,
          201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214,
          215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228,
          229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242,
          243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256,
          257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270,
          271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284,
          285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298,
          299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312,
          313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326,
          327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340,
          341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354,
          355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368,
          369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382,
          383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396,
          397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410,
          411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424,
          425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438,
          439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452,
          453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466,
          467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480,
          481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494,
          495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508,
          509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522,
          523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536,
          537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550,
          551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564,
          565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578,
          579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592,
          593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606,
          607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620,
          621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634,
          635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648,
          649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662,
          663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676,
          677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690,
          691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704,
          705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718,
          719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732,
          733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746,
          747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760,
          761, 762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774,
          775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788,
          789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 800, 801, 802,
          803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816,
          817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830,
          831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844,
          845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858,
          859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872,
          873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886,
          887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900,
          901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914,
          915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928,
          929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942,
          943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956,
          957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970,
          971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984,
          985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998,
          999, 1000 };
  return a[0];
}

or something more interesting ...

或者更有趣的东西......

#include <stdlib.h>
int main(void) {
  size_t max = 1000;
  int *a = malloc((max+1) * sizeof *a);
  if (a) {
    for (size_t i = 0; i <= max; i++) a[i] = i;
    free(a);
  }
  return 0;
}

回答by PADYMKO

The Cprogramming language has no a support, by default, for generate array as it has the Python, Ruby, JavaScript and may be other programming languages.

ç编程语言有没有支持,默认情况下,用于生成阵列,因为它有从Python,Ruby,JavaScript和可能是其他编程语言。

In the Python:

在 Python 中

>>> tuple(range(1, 10, 1))
(1, 2, 3, 4, 5, 6, 7, 8, 9)

In the Ruby:

在红宝石中

irb(main):004:0> Array (1..10)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In the JavaScript

在 JavaScript 中

> Array.apply(null, Array(10)).map(function(currentValue, index) { return index;});
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

So, you need write something similar you own.

所以,你需要写一些你拥有的类似的东西。

#include <stdio.h>
#include <errno.h> // http://man7.org/linux/man-pages/man3/errno.3.html


/*
    Prints an array of integer
 */
static void
printIntArray(int array[], size_t length) {
    char ending_charapter[] = ", ";
    putchar('[');
    for (size_t i = 0; i < length; ++i) {
        printf("%d", array[i]);
        if (i < length - 1) {
            printf("%s", ending_charapter);
        }
    }
    puts("]");
}


/*
    Fills an array of values in a passed range and with step.
    The step may be more or less 0.
    Return a non-zero value, if something went wrong.

    SIZE OF AN ARRAY IS A RESPONSIBILITY OF A PROGRAMMER.
 */
static int
rangeIntArray(int array[], const int start, const int end, const int step) {

    int value;
    int index = 0;

    // the step must not be zero
    if (step == 0) {
        errno = EINVAL;
        perror("Step must be not zero");
        return -1;
    }

    if (step > 0) {
        for (value = start; value < end; value += step, ++index) {
            array[index] = value;

        }
    } else if (step < 0) {
        for (value = start; value > end; value += step, ++index) {
            array[index] = value;

        }
    }
    return 0;

}


int
main (const int argc, const char *argv[])
{

    int arr1[10];
    rangeIntArray(arr1, 0, 10, 1);
    printIntArray(arr1, 10);

    int arr2[10];
    rangeIntArray(arr2, 0, -10, -1);
    printIntArray(arr2, 10);

    int arr3[4];
    rangeIntArray(arr3, 20, 10, -3);
    printIntArray(arr3, 4);

    int arr4[5];
    rangeIntArray(arr4, -20, -10, 2);
    printIntArray(arr4, 5);

    int arr5[5];
    rangeIntArray(arr5, -20, -10, -5);
    printIntArray(arr5, 5);

    int arr6[5];
    rangeIntArray(arr6, 20, 10, 5);
    printIntArray(arr6, 5);

    int arr7[5];
    int result = rangeIntArray(arr7, 20, 10, 0);
    if (result != 0) {
        puts("Raised error");
    }

    return 0;
}


Results

结果



Part the code

部分代码

int arr1[10];
rangeIntArray(arr1, 0, 10, 1);
printIntArray(arr1, 10);

Result

结果

Generated an array with values from 0 to 10 (exclusive) by step +1

按步骤 +1 生成一个值从 0 到 10(不包括)的数组

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Part the code

部分代码

int arr2[10];
rangeIntArray(arr2, 0, -10, -1);
printIntArray(arr2, 10);

Result

结果

Generated an array with values from 0 to -10 (exclusive) by step -1

通过步骤-1 生成了一个值从 0 到 -10(不包括)的数组

[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]


Part the code

部分代码

int arr3[4];
rangeIntArray(arr3, 20, 10, -3);
printIntArray(arr3, 4);

Result

结果

Generated an array with values from 20 to 10 (exclusive) by step -3

通过步骤-3 生成一个值从 20 到 10(不包括)的数组

[20, 17, 14, 11]


Part the code

部分代码

int arr4[5];
rangeIntArray(arr4, -20, -10, 2);
printIntArray(arr4, 5);

Result

结果

Generated an array with values from -20 to -10 (exclusive) by step +2

按步骤 +2 生成值从 -20 到 -10(不包括)的数组

[-20, -18, -16, -14, -12]


Part the code

部分代码

int arr5[5];
rangeIntArray(arr5, -20, -10, -5);
printIntArray(arr5, 5);

Result

结果

An upon attempt generated an array with values from -20 to -10 (exclusive) by step -5 changed no the array, so you will be see incorrect values

尝试生成一个数组,其值从 -20 到 -10(不包括)通过步骤 -5 没有更改数组,因此您将看到不正确的值

[837314392, 32724, 837312896, 32724, 0]


Part the code

部分代码

int arr6[5];
rangeIntArray(arr6, 20, 10, 5);
printIntArray(arr6, 5);

Result

结果

An upon attempt generated an array with values from 20 to 10 (exclusive) by step 5 changed no the array, so you will be see incorrect values

尝试生成一个数组,其值从 20 到 10(不包括)通过第 5 步更改没有数组,因此您将看到不正确的值

[1, 32765, 837312936, 32724, 0]


Part the code

部分代码

int arr7[5];
int result = rangeIntArray(arr7, 20, 10, 0);
if (result != 0) {
    puts("Raised error");
}

Result

结果

A value of the step must not be zero, so you will be see error

步长的值不能为零,因此您将看到错误

Raised error
Step must be not zero: Invalid argument


Notes:

笔记:

  1. This code must be work for the C++ also, since the C++ is the C with classes (https://en.wikipedia.org/wiki/C%2B%2B).
  2. Tested only with the GCC
  1. 此代码也必须适用于 C++,因为 C++ 是带有类的 C(https://en.wikipedia.org/wiki/C%2B%2B)。
  2. 仅使用 GCC 测试


Testing environment

测试环境

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 8.6 (jessie)
Release:    8.6
Codename:   jessie
$ uname -a
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ python3 --version
Python 3.5.2
$ ruby --version
ruby 2.1.5p273 (2014-11-13) [x86_64-linux-gnu]
$ nodejs --version
v6.9.2

回答by Yu Hao

From your code, you don't need an extra array that has integers from 0to 1000. Use iinstead:

从您的代码中,您不需要一个包含整数的额外数组 from 0to 1000。使用i来代替:

for(int i = 0; i < 1000; i++) {
        int currentNumber = i;

回答by Cacho Santa

sizeofwill not work for that case. take a look at this

sizeof不适用于这种情况。看看这个

just loop and add ito the array in each iteration:

只需循环并i在每次迭代中添加到数组中:

int myFunction(void) { 
        int numbers[1001];
        for(int i = 0; i <= 1000; i++) {
                numbers[i] = i;
        }       
}  

回答by Conrad Siahaan

From your updated question How one would make an array of integers [1,2,3,4,..,1000].

来自您更新的问题 How one will make an array of integers [1,2,3,4,..,1000]。

In the for loop

在 for 循环中

for(int i=0; i<=1000; i++) {}

The program will iterate the value in variable 'i' start from '0' (i = 0) until 'i' is less than or equal with 1000 (i <= 1000) On each iteration, you can see from the third param in the for loop (i++) The program will add '1' to the value of 'i'. It is the same with (i = i + 1) So, by thought it will run like this

程序将迭代变量 'i' 中的值,从 '0' (i = 0) 开始,直到 'i' 小于或等于 1000 (i <= 1000) 在每次迭代中,您可以从第三个参数中看到for 循环 (i++) 程序会在 'i' 的值上加 '1'。它与 (i = i + 1) 相同,因此,它会像这样运行

i=0, i=1, i=2, i=3, i=4, . . ., i=1000(end-of-loop)

I'm taking the code below from one people who answered this question

我从一个回答这个问题的人那里得到了下面的代码

int numbers[1001];
for(int i = 0; i <= 1000; i++)
  numbers[i] = i;

After explaining how for loop works, now you can see the declaration of the array of integer as a variable where we store the data.

在解释了 for 循环的工作原理之后,现在您可以看到整数数组的声明作为我们存储数据的变量。

int numbers[1001] means that we declare an array variable sized of 1001 integer value, start from index 0 to index 1000

int numbers[1001] 表示我们声明了一个大小为 1001 整数值的数组变量,从索引 0 开始到索引 1000

By illustration, it will be like:

举例来说,它会是这样的:

numbers[0] - numbers[1] - numbers[2] - numbers[3] - numbers[4] - . . . - numbers[1000]

The number inside the "[]" tag is the index number of array variable and inside the for loop, you can see each index from array variable(numbers) is assigned by the value of variable 'i'.

“[]”标签内的数字是数组变量的索引号,在for循环中,你可以看到数组变量(数字)中的每个索引都由变量“i”的值赋值。

numbers[0] = i = 0
i++
numbers[1] = i = 1
i++
numbers[2] = i = 2
i++
.
.
.
numbers[1000] = i = 1000

Hope this helps.

希望这可以帮助。

回答by Bosco Noronha

Is this what you wanted to do?

这是你想做的吗?

#include <stdlib.h>
#include <stdio.h>

int main() {
    int array[1000];
    for(int i = 0; i < 1000; i++) {
         array[i] = i+1;
         printf("%d", array[i]);
    }
}

回答by MikeTrischetta

To make the array, a loop is the most intuitive:

要制作数组,循环是最直观的:

int numbers[1001];
for(int i=0; i<1001;i++) {
    numbers[i] = i;
}

However it's usually a waste of space to use an array to store numbers when you can get them computationally. For example, you want to sum numbers in some range, right? You can use this method which uses considerably less space:

但是,当您可以通过计算获取数字时,使用数组来存储数字通常会浪费空间。例如,您想对某个范围内的数字求和,对吗?您可以使用此方法,它占用的空间要少得多:

int x = 1000;
int sum = (x * (x + 1))/2;

回答by ashutosh pandey

you can either use tuples if u are in python or you can make a function that returns an array up to that number. For the second one you can use a recursion function :

如果你在 python 中,你可以使用元组,或者你可以创建一个函数来返回一个数组,直到该数字。对于第二个,您可以使用递归函数:

ar=[]
def diff(n):
    if n==0:
        ar.append(0)
        return 
    else:
        ar.append(n)
        return diff(n-1) 

where n is the number upto which you want the array, in your case 1000 .But using tuples in python will be much easy.

其中 n 是您想要数组的数字,在您的情况下是 1000 。但是在 python 中使用元组会容易得多。

---Using c:

---使用 c:

#include <stdio.h>
#define n 1000

    int ar[n];
    int main(){
        for(int i=0;i<=n;i++){
            ar[i]=i;
        }
        for(int i=0;i<=n;i++){
        printf("%d  ",ar[i]);
        }
    }

回答by Kookerus

To initialize an array in C, you use: type arrayName [ size ]. A complete example would be:

要在 C 中初始化数组,请使用:type arrayName [ size ]。一个完整的例子是:

#include <stdio.h>

int main()
{
    //initialize array
    int myArray [1000];

    int i;
    //for loop to add integers to array
    for(i = 0; i <= 1000; i++)
    {
        myArray[i] = i;
        printf("%d\n", myArray[i]);
    }

    return 0;
}

A good tutorial on this is http://www.tutorialspoint.com/cprogramming/c_arrays.htm

一个很好的教程是http://www.tutorialspoint.com/cprogramming/c_arrays.htm