TLDR : If you are searching for a solution specifically for the pset, then I would suggest Kareem's first answer.
This answer is exactly with reference to the question mentioned in your title..
There are such features in python, but I haven't searched for whether they are available in c99. However, I managed to write something useful in this context.
Here is a function that works on strings, and extracts characters from it, with index in the range [start, end]
(both inclusive), at each discrete steps
. It returns the length(to be considered) for the edited array.
int parts(char* array, int start, int end, int steps)
{
int i, j;
char temp [(end-start)/steps + 1];
for (i = start , j = 0 ; i <= end ; i += steps , ++j)
temp[j] = array[i];
for (i = 0 ; i < (end - start)/steps + 1 ; ++i)
array[i] = temp[i];
//for ( ; i <= end ; ++i)
// array[i] = ' ';
return (end - start)/steps + 1;
}
Consider char array[] = "abcdefghijklmnopqrstuvwxyz";
. When calling this function and printing characters of array from 0 to returned value of the function,
parts(array, 0, 25, 1);
parts(array, 0, 25, 2);
parts(array, 10, 25, 3);
we get output as these respectively.
abcdefghijklmnopqrstuvwxyz
acegikmoqsuwy
knqtwz
A similar implementation for integer arrays looks like this.
int parts(int* array, int start, int end, int steps)
{
int i, j;
int temp [end];
for (i = start , j = 0 ; i <= end ; i += steps , ++j)
temp[j] = array[i];
for (i = 0 ; i < j ; ++i)
array[i] = temp[i];
//for ( ; j < end ; ++j)
// array[j] = 0;
return (end - start)/steps + 1;
}
Consider int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
, then function calls and their respective outputs are :
parts(arr, 0, 14, 1);
parts(arr, 0, 14, 2);
parts(arr, 5, 14, 3)
Outputs something like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 3 5 7 9 11 13 15
6 9 12 15
Remember, don't mess with values of start
, end
, and steps
. Also don't experiment with any values such that start
> end
or steps
< 0
, the functions are not implemented to support such expectations. Although, feel free to modify the function in any way you want to add these extra features.
Good Luck.
P.S. : Also, the functions partially modify the array passed to them, so it would be good only if the values of that array are accessed for index from 0 to the value returned by those functions. If you want to keep the original array unchanged, then try creating its duplicate and pass it to these functions.