#include <stdio.h>
#include <stdlib.h>
/* Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division.
*/
int **split_array(const int *s, int length) {
length--;
int **result
= malloc(2 * sizeof(int)); int *odd, *even;
if (length % 2 == 0) {
result
[0] = (int*) malloc((length
/ 2) * sizeof(int)); result
[1] = (int*) malloc((length
/ 2) * sizeof(int)); } else {
int even_count = (length + 1) / 2;
result
[0] = malloc(even_count
* sizeof(int)); result
[1] = malloc((length
- even_count
) * sizeof(int)); }
even = &result[0][0], odd = &result[1][0];
for (int a = 0; a < length; a++) {
if (a % 2 == 0) {
*even = s[a];
even++;
} else {
*odd = s[a];
odd++;
}
}
return result;
}
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
int *build_array(char **strs, int size) {
size--;
int *arr
= malloc(size
* sizeof(int)); int *element = &arr[0];
while (element < &arr[size]) {
strs++;
*element = **strs - '0';
// printf("%d\n", *element);
element++;
}
return arr;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv, argc);
int **result = split_array(full_array, argc);
for (int i = 0; i < argc - 1; i++) {
}
for (int i = 0; i < argc / 2; i++) {
}
for (int i = 0; i < (argc - 1) / 2; i++) {
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8qIFJldHVybiBhIHBvaW50ZXIgdG8gYW4gYXJyYXkgb2YgdHdvIGR5bmFtaWNhbGx5IGFsbG9jYXRlZCBhcnJheXMgb2YgaW50cy4KICAgVGhlIGZpcnN0IGFycmF5IGNvbnRhaW5zIHRoZSBlbGVtZW50cyBvZiB0aGUgaW5wdXQgYXJyYXkgcyB0aGF0IGFyZQogICBhdCBldm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