How do you take the (mathematical) inverse of an array


Hello,

I need to take an array, and get the inverse of it (basically, just how you would take an inverse of a function in math). I'm kind of stumped. I need to do it where, if a[i] = x, b[x] = i. I would just copy from array a to array b in a function.
Hmm, can you use hash tables? Or is it restricted only to arrays?
Unfortunately, not...I have to follow a function prototype that has a const array, regular array (where it would be copied to) and a numElements int.

Just thinking out loud, since the index would have to become the output, and then the output would become the index, I figure that I could run a loop, and then do something like:

for (int i = 0; i < elements; i++){

b[x] = i;

}

From there, I just have to get the order right (i.e. index), which would be the output of a[i].
Last edited on
Can you write out the prototype so I can give you some sense of direction?
You have the basic idea down.

The only problem you aren't considering is what is the value of x.

It can be easily done just by thinking about the syntax in C++.
void inverse( unsigned a[], const unsigned b[], unsigned elements );
Last edited on
What's up with the bool?

Are you sure you are to return the inverse of the array or are you simply checking to see if they are inverses of each other?
Oh, actually, that's a mistake...It's a void.
Haha. Sounds good.

So, here's some pointers:

b will contain some values that actually will be the indices of a.

Say, the first element of b, as in b[0], contains 4.

So that means the fifth element of a, as in a[4], will contain 0.

As in, b[i] = x; a[x] = i;

In conclusion, the syntax would look like this:
a[b[i]] = i

I really, really hope that a is big enough to hold its values. Can you guess why?
Last edited on
Wow, thank you. That's definitely it. I had the output part down, but for whatever reason was stumped on how to get the output of b to be the index of a. That makes a lot of sense.

Yeah, I think I know what you mean...You're talking about if "b" has a very large value of one of it's numbers, like 1 million, "a" would need at least a million indexes.

Thanks again, man.
Yep. No problem. Glad I could help.
Topic archived. No new replies allowed.