Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stddef.h>
- #include <stdlib.h>
- int *split_and_add(size_t si, const int arr[si], int n, size_t *so) {
- // initializing variables
- int left;
- int right;
- int *top_arr;
- int *bottom_arr;
- int *result = malloc(si * sizeof(int));
- // copies input arr[] over to use
- for (int i = 0; i < si; i++)
- {
- result[i] = arr[i];
- }
- while (n != 0)
- {
- if (si % 2 == 0)
- {
- left = si / 2;
- right = left;
- top_arr = malloc(left * sizeof(int));
- bottom_arr = malloc(right * sizeof(int));
- for (int i = 0; i < left; i++)
- {
- top_arr[i] = result[i];
- bottom_arr[i] = result[i+si/2];
- }
- }
- else
- {
- left = si / 2 + 1;
- right = left;
- top_arr = malloc(left * sizeof(int));
- bottom_arr = malloc(right * sizeof(int));
- top_arr[0] = 0;
- for (int i = 0; i < left; i++)
- {
- top_arr[i+1] = result[i];
- bottom_arr[i] = result[i+si/2];
- }
- }
- // add up both arrays
- for (int i = 0; i < left; i++)
- {
- result[i] = top_arr[i] + bottom_arr[i];
- }
- // get new value for array size and reduce count
- si = left;
- n--;
- }
- for (int i = 0; i < si; i++)
- {
- printf("%d ", result[i]);
- }
- printf("\n");
- printf("\n");
- *so = si;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement