I have 2 arrays:
int element[3] = {0, 1, 2};
int quantity[3] = {2, 3, 4};
Now I want a result array that will have two zeros, three ones and four twos.
int result[2+3+4] = {0, 0, 1, 1, 1, 2, 2, 2, 2};
How do I do this using loop?
Asked
Active
Viewed 70 times
-1

Vlad from Moscow
- 301,070
- 26
- 186
- 335

Min Bani Adam
- 23
- 4
-
A good start would be to calculate the size of the new `result` array. Then perhaps a loop over the `element` array. And inside that loop, a loop using the corresponding value from `quantity`. – Some programmer dude Oct 26 '22 at 09:00
-
"... and a partridge in a pear tree..." `:-)` ... Seriously, `realloc()` could be used to achieve this with a single loop growing the array (and a trivial inner loop poking repetitions of a value into its elements.) Hint: `realloc( NULL, n );` acts just like `malloc( n )`... Start with 0 and work your way up to the completed array. – Fe2O3 Oct 26 '22 at 09:13
-
@Jabberwocky You have the elements and their quantity. Now produce the resulting array. – Min Bani Adam Oct 26 '22 at 09:20
2 Answers
1
You need to count the number of elements in the result array and to declare either a variable length array with the calculated value or to allocate dynamically such an array.
For example
int quantity[3] = {2, 3, 4};
size_t n = 0;
for ( size_t i = 0; i < 3; i++ )
{
n += quantity[i];
}
int result[n];
// or
// int *result = malloc( n * sizeof( int ) );
And then in nested loops you need fill the resulted array.
For example
for ( size_t i = 0, j = 0; i < 3; i++ )
{
for ( size_t k = 0; k < quantity[i]; k++ )
{
result[j++] = element[i];
}
}

Vlad from Moscow
- 301,070
- 26
- 186
- 335
-
"And then in nested loops you need fill the resulted array.". But how? – Min Bani Adam Oct 26 '22 at 09:14
-
-
'result[j++] = element[i];' should be 'result[j] = element[i];' and then j++; – Min Bani Adam Oct 26 '22 at 09:31
-
@MinBaniAdam If you want to write two statements instead of one statement you can do that.:) – Vlad from Moscow Oct 26 '22 at 09:32
1
Firstly we need to calculate the size of the result array. Then start populating your result array each element at a time. While we populate the result array, we need to increment the index.
int elementSize = sizeof(element)/sizeof(element[0]);
int resultSize = 0;
//pre calculating the size of result array
for(int i=0;i<elementSize;i++ ) {
resultSize += quantity[i];
}
int result[resultSize], currIndex = 0;
//picking each element
for(int i = 0;i< elementSize; i++ ) {
int currElement = element[i];
int currQuantity = quantity[i];
//filling the current element required no of times in the result array
while(currQuantity--) {
result[currIndex] = currElement;
currIndex++;
}
}
//just a for loop to check the elements inside result array
for(int i=0;i<resultSize;i++)
printf("%d\n",result[i]);

deathByChocolate
- 23
- 5
-
-
just a minor change if using C. For checking the elements inside result array, use printf instead of cout. ```printf("%d\n",result[i]);``` Edited my answer, thanks for pointing out. – deathByChocolate Oct 26 '22 at 09:32