35 lines
896 B
Text
35 lines
896 B
Text
string string_of_int_array(int[] arr, int len) {
|
|
var res = "[";
|
|
for (var i = 0; i < len; i = i + 1;) {
|
|
res = string_cat(res, " ");
|
|
res = string_cat(res, string_of_int(arr[i]));
|
|
}
|
|
res = string_cat(res, " ]\n");
|
|
return res;
|
|
}
|
|
|
|
string pascal_triangle(int num_rows) {
|
|
var current_row = new int[] {1};
|
|
|
|
var result = "";
|
|
|
|
for (var current_len = 1; current_len <= num_rows; current_len = current_len + 1;) {
|
|
result = string_cat(result, string_of_int_array(current_row, current_len));
|
|
|
|
var new_row = new int[current_len + 1];
|
|
|
|
new_row[0] = current_row[0];
|
|
for (var j = 1; j < current_len; j = j + 1;) {
|
|
new_row[j] = current_row[j - 1] + current_row[j];
|
|
}
|
|
new_row[current_len] = current_row[current_len - 1];
|
|
current_row = new_row;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
int program(int argc, string[] argv) {
|
|
print_string(pascal_triangle(10));
|
|
return 0;
|
|
}
|