Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.
#define SIZE 100
int* transform_array(int *array, int arraySize)
{
int i; // index of new malloc array
int k; // index of input array
int num = 0; // value of input array element
int *temp = (int*)malloc(100*sizeof(int));
for(i=0, k=0; i < SIZE; i++)
{
if( i%2 == 0 && k < arraySize )
{
num = *(array + k);
*(temp + i) = num;
k++;
}
else {
*(temp + i) = 0;
}
}
return temp;
}
int main()
{
int v[6] = {1, 1, 2, 1, 1, 2};
int i = 0;
int * aux;
aux = transform_array(v, 6);
for(i=0; i < SIZE; i++)
{
printf("%d\n",*(aux + i));
}
free(aux);
}
#define SIZE 100
int* transform_array(int *array, int arraySize)
{
int k; // index of input array
int *temp = (int*)calloc( SIZE, sizeof(int) );
int *bigArray = temp;
for(k=0; k < arraySize; k++)
{
*temp = *array++;
temp += 2;
}
return bigArray;
}
int main()
{
int v[7] = {1, 1, 2, 1, 1, 2};
int i = 0;
int * aux;
aux = transform_array(v, sizeof(v)/sizeof(v[0]) );
for(i=0; i < SIZE; i++)
{
printf("%d\n",*(aux + i));
}
free(aux);
}
If so, something like this should do the trick :
Open in new window