3 ms·
This inspired me to write one that is even shorter and doesn't heap-allocate any memory at all. What can I say, I'm a sucker for minimal C. #include <stdio.
by haberman 15y ago
This inspired me to write one that is even shorter and doesn't heap-allocate any memory at all. What can I say, I'm a sucker for minimal C.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int row = atoi(argv[1]);
long data[row];
for (int i = 0; i < row; i++) {
data[i] = 1;
for (int j = i - 1; j > 0; j--) data[j] += data[j - 1];
}
for (int i = 0; i < row; i++) printf("%ld ", data[i]);
printf("\n");
return 0;
}
- hn_reader 15y agoBest solution I've seen here so far.
- deleted 15y ago[deleted]
- demallien 15y agoWhen did C start accepting non-constants for array size declarations?
- shaggyfrog 15y agoIf row is too big you can bust your stack frame when you define your data array.
- hn_reader 15y agoTrue, though in this example integer overflow would happen first.
- kunqiana 15y agoI think your code is off by 1.