diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-09 16:07:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-09 16:07:51 +0100 |
| commit | 7f56aab79ae8a7f93785170c3545c37f3ad4bcd6 (patch) | |
| tree | 01203ce24eb9198fce07c2f17fafebbc35a6e540 | |
| parent | 99848f534e866b77924f601f7c2ad77bc68d804d (diff) | |
| parent | 12724a5efda7d9d03ae7aee71bffd1ad09fc0aba (diff) | |
| download | perlweeklychallenge-club-7f56aab79ae8a7f93785170c3545c37f3ad4bcd6.tar.gz perlweeklychallenge-club-7f56aab79ae8a7f93785170c3545c37f3ad4bcd6.tar.bz2 perlweeklychallenge-club-7f56aab79ae8a7f93785170c3545c37f3ad4bcd6.zip | |
Merge pull request #9906 from zapwai/master
C update
| -rw-r--r-- | challenge-264/zapwai/c/ch-2.c | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/challenge-264/zapwai/c/ch-2.c b/challenge-264/zapwai/c/ch-2.c index 86096faa72..f4ae6d9082 100644 --- a/challenge-264/zapwai/c/ch-2.c +++ b/challenge-264/zapwai/c/ch-2.c @@ -16,31 +16,37 @@ void proc(int source[], int indices[], int slen) { int current_length = 0; for (int i = 0; i < slen; i++) { - /* target[current_length] = ; */ - + int *post = malloc((slen - indices[i] - 1) * sizeof(int)); int *pre = malloc(current_length * sizeof(int)); for (int j = 0; j < current_length; j++) pre[j] = target[j]; + for (int j = 0; j < slen - indices[i] - 1; j++) + post[j] = target[indices[i] + j]; - int *post = malloc((slen - indices[i]) * sizeof(int)); - for (int j = 0; j < slen - indices[i]; j++) - post[j] = target[j]; - - /* Work in progress... */ + int skip = 0; /* 0, or 1 if replacement occurs */ + int current_value = NIL; /* gets replaced */ + if (current_length > indices[i]) { + current_value = target[indices[i]]; + skip = 1; + } + target[indices[i]] = source[i]; + if (current_value != NIL) + target[indices[i] + 1] = current_value; + + if (current_length > indices[i]) { + for (int j = 0; j < slen - indices[i] - 1; j++) + target[indices[i] + j + skip] = post[j]; + } + free(pre); free(post); current_length++; - - /* chunk = splice @target, indices[i]; */ - /* target[indices[i]] = source[i]; */ - /* push @target, @chunk; */ - } + } printf("Output: { "); for (int i = 0; i < slen; i++) printf("%d ", target[i]); printf("}\n"); - } int main() { @@ -55,4 +61,4 @@ int main() { int source3[] = {1}; int indices3[] = {0}; proc(source3, indices3, sizeof(source3) / sizeof(int)); -} +}
\ No newline at end of file |
