forked from detro/coding-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascal_triangle_generator.c
More file actions
54 lines (43 loc) · 1.48 KB
/
pascal_triangle_generator.c
File metadata and controls
54 lines (43 loc) · 1.48 KB
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
#include <stdio.h>
#include <stdlib.h>
void print_row(unsigned long long* row, unsigned int size) {
static unsigned int j;
for ( j = 0; j < size; ++j ) { printf("%llu ", row[j]); }
printf("\n");
}
unsigned long long* pascal_triangle_line(unsigned int pascal_row){
static unsigned int i, j;
unsigned long long* result = NULL;
result = (unsigned long long*)calloc(pascal_row, sizeof(unsigned long long));
if ( NULL != result ) {
result[pascal_row-1] = 1; // First element on the right is always '1'
for ( i = 2; i <= pascal_row; ++i ) {
#ifdef DEBUG
print_row(result, pascal_row);
#endif
for ( j = pascal_row-i; j < pascal_row-1; ++j ) {
result[j] += result[j+1]; // Calculate the 'j'-th element
#ifdef VERBOSE
printf("(row: %d, element: %d) => %llu\n", i, j, result[j]);
#endif
}
}
}
return (result);
}
int main(int argc, char** argv)
{
unsigned int input;
unsigned long long* result = NULL;
// Check the Input
if ( argc == 2 ) input = atoi(argv[1]); else return (EXIT_FAILURE);
// Calculate the required line
result = pascal_triangle_line(input);
// Print result
if ( NULL != result ) {
printf("RESULT ROW:\n");
print_row(result, input);
return (EXIT_SUCCESS);
}
return (EXIT_FAILURE);
}