Created
December 12, 2016 06:17
-
-
Save jjlumagbas/d6c3056b679c1286b88973f46a7e2ac6 to your computer and use it in GitHub Desktop.
Arrays in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int factorial(int n) { | |
int result = 1; | |
/* for i in range(1, n + 1): */ | |
for (int i = 1; i < n + 1; i++) { | |
result = result * i; | |
} | |
return result; | |
} | |
int count_div_2(int n) { | |
int count = 0; | |
while (n != 1) { | |
count = count + 1; | |
n = n / 2; | |
} | |
return count; | |
} | |
void display_array(int nums[], int length) { | |
for (int i = 0; i < length; i++) { | |
printf("%d\n", nums[i]); | |
} | |
} | |
int main() { | |
int nums[5] = {1, 2, 3, 4, 5}; | |
/* nums = {1, 2, 3, 4, 5}; */ | |
/* nums[0] = 1; */ | |
/* nums[1] = 2; */ | |
/* nums[2] = 3; */ | |
/* nums[3] = 4; */ | |
/* nums[4] = 5; */ | |
/* printf("%d\n", nums[0]); */ | |
/* printf("%d\n", nums[1]); */ | |
/* printf("%d\n", nums[2]); */ | |
/* printf("%d\n", nums[3]); */ | |
/* printf("%d\n", nums[4]); */ | |
/* printf("%d\n", nums[5]); */ | |
// A // B // C | |
for (int i = 0; i < 5; i++) { // i = i + 1 | |
printf("%d\n", nums[i]); // D | |
} | |
int i = 0; | |
while (i < 5) { | |
printf("%d\n", nums[i]); | |
i++; | |
} | |
int longer_nums[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
display_array(nums, 5); | |
display_array(longer_nums, 10); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment