So the problem is that I've made this MergeSort that works for smaller arrays but not for bigger arrays
in bigger arrays it inserts an extra zero in the beginning
as you can see below.
------------------------------------------------
SORTING TESTS: START
Problem: 323, 389, 881, 821, 663, 964, 958, 68, 988, 624, 44, 288, 226, 317, 347, 840, 241, 940, 8, 461, 125, 686, 917, 400, 378, 158, 171, 823, 804, 331, 930, 717, 189, 850, 790, 481, 983, 152, 575, 408, 888, 570, 872, 671, 734, 671, 850, 428, 36, 306, 700, 916, 868, 781, 463, 385, 820, 606, 727, 584, 563, 815, 717, 138, 759, 975, 177, 293, 762, 686, 498, 8, 992, 519, 212, 686, 608, 663, 656, 545, 618, 968, 710, 150, 534, 283, 506, 769, 794, 413, 495, 771, 210, 365, 125, 233, 272, 363, 692, 95,
Solution: 8, 8, 36, 44, 68, 95, 125, 125, 138, 150, 152, 158, 171, 177, 189, 210, 212, 226, 233, 241, 272, 283, 288, 293, 306, 317, 323, 331, 347, 363, 365, 378, 385, 389, 400, 408, 413, 428, 461, 463, 481, 495, 498, 506, 519, 534, 545, 563, 570, 575, 584, 606, 608, 618, 624, 656, 663, 663, 671, 671, 686, 686, 686, 692, 700, 710, 717, 717, 727, 734, 759, 762, 769, 771, 781, 790, 794, 804, 815, 820, 821, 823, 840, 850, 850, 868, 872, 881, 888, 916, 917, 930, 940, 958, 964, 968, 975, 983, 988, 992,
Sorted(Testing): 0, 8, 8, 36, 44, 68, 95, 125, 125, 138, 150, 152, 158, 171, 177, 189, 210, 212, 226, 233, 241, 272, 283, 288, 293, 306, 317, 323, 331, 347, 363, 365, 378, 385, 389, 400, 408, 413, 428, 461, 463, 481, 495, 498, 506, 519, 534, 545, 563, 570, 575, 584, 606, 608, 618, 624, 656, 663, 663, 671, 671, 686, 686, 686, 692, 700, 710, 717, 717, 727, 734, 759, 762, 769, 771, 781, 790, 794, 804, 815, 820, 821, 823, 840, 850, 850, 868, 872, 881, 888, 916, 917, 930, 940, 958, 964, 968, 975, 983, 988, 992,
Sorted(InsertionSort): 8, 8, 36, 44, 68, 95, 125, 125, 138, 150, 152, 158, 171, 177, 189, 210, 212, 226, 233, 241, 272, 283, 288, 293, 306, 317, 323, 331, 347, 363, 365, 378, 385, 389, 400, 408, 413, 428, 461, 463, 481, 495, 498, 506, 519, 534, 545, 563, 570, 575, 584, 606, 608, 618, 624, 656, 663, 663, 671, 671, 686, 686, 686, 692, 700, 710, 717, 717, 727, 734, 759, 762, 769, 771, 781, 790, 794, 804, 815, 820, 821, 823, 840, 850, 850, 868, 872, 881, 888, 916, 917, 930, 940, 958, 964, 968, 975, 983, 988, 992,
assertion "isArrEqual(sortThis, &solutionArr[1], solutionArr[0])" failed: file "
/cygdrive/c/src/universal-repos/datastructures-and-algorithms/main.c", line 119,
function: sortingTest
a
Process finished with exit code 0
Below you can see the testing and implementation of the mergeSort
I believe the fault lies in the merging function but if I knew what I was doing then I would've managed to fix this bug of mine.
int *mergeSortShell(const int *const sortThis, int size) {
if(sortThis == NULL || size < 1){
return NULL;
}
int * copyArr = arrCopy(sortThis, 0, size);
mergeSort(copyArr, 0, size);
return copyArr;
}
void mergeSort(int * sort, int left, int right) {
//the array and left should be zero and right bound right you be the size of the array
if (sort == NULL) {
return;
}
if (right > left) {
int middle = (right - left);
if (middle % 2 != 0){
middle--;
}
middle = middle / 2 + left;
mergeSort(sort, left, middle);
mergeSort(sort, middle + 1, right);
merge(sort, left, middle, right);
}
}
void merge(int *mergeThis, int left, int middle, int right) {
if (mergeThis == NULL) {
return;
}
int leftSize = (middle - left) + 1;
int rightSize = right - middle;
int i, j, k; //iterators to be used throughout the function
int leftArray[leftSize + 1], rightArray[rightSize + 1];//tempArrays
leftArray[leftSize] = INT_MAX;
rightArray[rightSize] = INT_MAX;
for (i = 0; i < leftSize; i++) {
leftArray[i] = mergeThis[left + i];
}
for (i = 0; i < rightSize; i++) {
rightArray[i] = mergeThis[middle + 1 + i];
}
i = 0;//index of leftArray
j = 0;//index of rightArray
k = left;//index of sortCpy
while (i < leftSize && j < rightSize) {
if (leftArray[i] <= rightArray[j]) {
mergeThis[k] = leftArray[i];
i++;
} else {
mergeThis[k] = rightArray[j];
j++;
}
k++;
}
//if any of the the right or left has remaining elements
//this will run
while (i < leftSize) {
mergeThis[k] = leftArray[i];
k++;
i++;
}
while (j < rightSize) {
mergeThis[k] = rightArray[j];
k++;
j++;
}
}
int *arrCopy(const int *const arrayPtr, int leftBound, int size) {//leftbound should be left at zero if you want it
// to copy the whole array
int *sortCpy = NULL;
sortCpy = (int *) malloc(sizeof(int) * (size - leftBound));
if (arrayPtr == NULL) {
return NULL;
}
for (int i = 0, j = leftBound; i < (size - leftBound) && j < size; i++, j++) {
sortCpy[i] = arrayPtr[j];
}
//memcpy(sortCpy, arrayPtr, size * sizeof(int));
if (isArrEqual(sortCpy, arrayPtr, size) == false) {
return NULL;
}
return sortCpy;
}
bool sortingTest() {
printf("------------------------------------------------\n");
printf("SORTING TESTS: START\n");
const int l1[] = {3, 1, 5, 10, 8, 7};
const int l1solution[] = {1, 3, 5, 7, 8, 10};
const int l2[] = {5, 2, 9, 6, 1, 2};
const int l2solution[] = {1, 2, 2, 5, 6, 9};
const int pNum[] = {1, 9, 9, 5, 0, 1, 2, 6, 0, 0, 9, 6};
const int pNumSolution[] = {0, 0, 0, 1, 1, 2, 5, 6, 6, 9, 9, 9};
assert(isArrEqual(l1, l1, 6) == true);
assert(isArrEqual(l1, l1solution, 6) == false);
assert(isArrEqual(l2, l2solution, 6) == false);
assert(isArrEqual(NULL, NULL, 5) == false);
assert(arrPrint(NULL, INT_MIN) == false);
// assert(isArrEqual(pNum, pNumSolved, 12));
assert(load_file(NULL) == NULL);
char sortingProblem[] = {"../sorting_problems/test-10-1-problem"};
char sortingSolution[] = {"../sorting_problems/test-10-1-solution"};
const int * const problemArr = load_file(sortingProblem);
const int * const solutionArr = load_file(sortingSolution);
assert(problemArr != NULL);
assert(solutionArr != NULL);
int *copyTest = NULL;
copyTest = arrCopy(&solutionArr[1], 0, solutionArr[0]);
assert(isArrEqual(copyTest, &solutionArr[1], solutionArr[0]));
free(copyTest);
copyTest = arrCopy(&problemArr[1], 0, problemArr[0]);
assert(isArrEqual(copyTest, &problemArr[1], problemArr[0]));
free(copyTest);
int *sortThis = NULL;
int *reference = NULL;
//----------------MERGE SORT START----------------------
assert(mergeSortShell(NULL, INT_MIN) == NULL);
assert(mergeSortShell(l1, INT_MIN) == NULL);
sortThis = mergeSortShell(l1, (sizeof(l1) / sizeof(int)));
assert(sortThis != NULL);
assert(isArrEqual(sortThis, l1solution, sizeof(l1) / sizeof(int)));
free(sortThis);
sortThis = mergeSortShell(l2, (sizeof(l2) / sizeof(int)));
assert(sortThis != NULL);
assert(isArrEqual(sortThis, l2solution, sizeof(l2) / sizeof(int)));
free(sortThis);
sortThis = mergeSortShell(pNum, (sizeof(pNum) / sizeof(int)));
assert(sortThis != NULL);
assert(isArrEqual(sortThis, pNumSolution, sizeof(pNum) / sizeof(int)));
free(sortThis);
sortThis = mergeSortShell(&problemArr[1], problemArr[0]);
assert(sortThis != NULL);
//region Status
printf("Problem: \t\t\t\t");
arrPrint(&problemArr[1], problemArr[0]);
printf("Solution:\t\t\t\t");
arrPrint(&solutionArr[1], solutionArr[0]);
printf("Sorted(Testing):\t\t");
arrPrint(sortThis, solutionArr[0]+1);
//endregion
assert(isArrEqual(sortThis, &solutionArr[1], solutionArr[0]));
free(sortThis);
/**/
// ----------------MERGE SORT END------------------------
//----------------QUICK SORT START----------------------
//Might not implement
//----------------QUICK SORT END------------------------
free((int*)problemArr);
free((int*)solutionArr);
printf("SORTING TESTS: END\n");
printf("------------------------------------------------\n");
return 0;
}
int main() {
printf("------------------------START OF TESTS------------------------\n");
altLinkedListTest();
arrayTest();
sortingTest();
printf("------------------------END OF TESTS--------------------------");
return 0;
}
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]
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:
int numbers[1001];
for(int i = 0; i <= 1000; i++){
numbers[i] = i;
}
How would I make an array of all the integers between 0 and 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);
}
}
The C programming language has no a support, by default, for generate array as it has the Python, Ruby, JavaScript and may be other programming languages.
In the 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
> 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
[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
[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
[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
[-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
[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
[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:
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).
Tested only with the 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
From your code, you don't need an extra array that has integers from 0 to 1000. Use i instead:
for(int i = 0; i < 1000; i++) {
int currentNumber = i;
sizeof will not work for that case. take a look at this
just loop and add i to the array in each iteration:
int myFunction(void) {
int numbers[1001];
for(int i = 0; i <= 1000; i++) {
numbers[i] = i;
}
}
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]);
}
}
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;
From your updated question
How one would make an array of integers [1,2,3,4,..,1000].
In the for loop
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=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.
int numbers[1001] means that we declare an array variable sized of 1001 integer value, start from index 0 to index 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'.
numbers[0] = i = 0
i++
numbers[1] = i = 1
i++
numbers[2] = i = 2
i++
.
.
.
numbers[1000] = i = 1000
Hope this helps.
This is the easiest way to do this
var myArray = [...Array(101).keys()]
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 :
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.
---Using 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]);
}
}
To initialize an array in C, you use: type arrayName [ size ].
A complete example would be:
#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