Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #define CHUNK (1 << 9)
- void checkAlloc(void *p) {
- if (p == NULL) {
- perror("");
- exit(EXIT_FAILURE);
- }
- }
- typedef struct {
- int len, mem;
- uint64_t *at;
- } DynArr;
- DynArr mkArr(int ini) {
- DynArr arr = {ini, ini, NULL};
- if (ini) {
- arr.at = calloc(sizeof(uint64_t), ini);
- }
- return arr;
- }
- void addToArr(DynArr *arr, uint64_t x) {
- if (arr->len == arr->mem) {
- arr->mem += CHUNK;
- arr->at = realloc(arr->at, sizeof(uint64_t) * arr->mem);
- checkAlloc(arr->at);
- }
- arr->at[arr->len++] = x;
- }
- void printArr(const DynArr *const arr) {
- for (int i = 0; i < arr->len; i++) {
- printf("%lu ", arr->at[i]);
- }
- puts("");
- }
- int main(void) {
- DynArr fib = mkArr(2);
- fib.at[1] = 1;
- int term;
- scanf("%d", &term);
- for (int i = 2; i < term; i++) {
- int newElem = fib.at[i - 2] + fib.at[i - 1];
- addToArr(&fib, newElem);
- }
- printArr(&fib);
- free(fib.at);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement