Parallel Arrays

Sep 9, 2013 at 5:35am
closed account (SwqGNwbp)
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}

Any body can do this favor to me please!
Sep 9, 2013 at 5:43am
closed account (28poGNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# include <iostream>
using std::endl;
using std::cout;

int main()
{
    int arrayData[] = {1000, 3000, 5000, 3000, 8888};
    int arrayIndex[] = {2, 0, 2, 3, 3, 0};

    for(int i=0;i<6;i++)
        cout << arrayData[arrayIndex[i]] << ", ";
    cout << endl;

    return 0;
}
Sep 9, 2013 at 6:04am
closed account (SwqGNwbp)
Thanks, great! how we can write it as a function with this prototype:

1
2
void f( unsigned result[], const unsigned data[], unsigned dataElements,
        unsigned index[], unsigned indexElements )
Sep 9, 2013 at 6:18am
closed account (28poGNh0)
Are you sure you need these tow parameters unsigned result[] and unsigned dataElements

coz only 3 are needed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# include <iostream>
using std::endl;
using std::cout;

void f(const unsigned data[], unsigned index[], unsigned indexElements )
{
    for(unsigned i=0;i<indexElements;i++)
        cout << data[index[i]] << ", ";
    cout << endl;
}

int main()
{
    unsigned arrayData[] = {1000, 3000, 5000, 3000, 8888};
    unsigned arrayIndex[] = {2, 0, 2, 3, 3, 0};

    f(arrayData,arrayIndex,6);

    return 0;
}
Sep 10, 2013 at 5:58am
closed account (SwqGNwbp)
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}.
Sep 10, 2013 at 7:41am
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].
Sep 10, 2013 at 10:07am
If you want to figure this out on your own and learn it, don't read this below. Otherwise here you are.
1
2
3
4
5
6
7
8
9
10
11
void f( unsigned result[], const unsigned data[], unsigned dataElements, 
        unsigned index[], unsigned indexElements )
{
    for (int i = 0; i < indexElements; ++i)
    {
        if (index[i] >= 0 && index[i] < dataElements)
            result[i] = data[ index[i] ];
        else
            die();
    }
}
Last edited on Sep 10, 2013 at 10:09am
Sep 10, 2013 at 10:23am
For bonus points, when does this evaluate true:
( x < 0 ) // where x is unsigned integer
Sep 11, 2013 at 8:04pm
closed account (SwqGNwbp)
what should be written in the main program to call this function?
Sep 11, 2013 at 8:07pm
Topic archived. No new replies allowed.