I need a program as below:
we have a data array like: {1000, 3000, 5000, 3000, 8888}
and an index array like: {2, 0, 2, 3, 3, 0}
we want the function to set the elements of result array to count from index array and read from data array and create the result with the numbers of index elements.(in this case result is : {5000, 1000, 5000, 3000, 3000, 1000}
Actually I need it with all parameters. The complete description is:
void f( unsigned result[], const unsigned data[], unsigned dataElements,
const unsigned index[], unsigned indexElements )
f’s job is to set the elements of result to the correct values. No I/O. data has dataElements elements, index has indexElements elements, and result has indexElements elements, because it is parallel to the index array. Each element of index must be in the range [0, dataElements), or f will complain and die (by calling your die function). The i’th element of result is copied from whichever element of data has a subscript that is equal to the i’th element of indexElements.
For instance, if data were {1000, 3000, 5000, 3000, 8888} and index were {2, 0, 2, 3, 3, 0}, then result would be set to {5000, 1000, 5000, 3000, 3000, 1000}.
It would be polite to give the complete description up front. Not afterwards.
It should be easy to adapt Techno01's version.
He writes to std::cout, you want to write to result.
The purpose of dataElements is clear too: for range check of index[i].