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

Feb 17, 2014 at 7:35pm

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.
Feb 17, 2014 at 7:51pm
Hmm, can you use hash tables? Or is it restricted only to arrays?
Feb 17, 2014 at 8:11pm
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 Feb 17, 2014 at 8:17pm
Feb 17, 2014 at 8:14pm
Can you write out the prototype so I can give you some sense of direction?
Feb 17, 2014 at 8:18pm
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++.
Feb 17, 2014 at 8:23pm
void inverse( unsigned a[], const unsigned b[], unsigned elements );
Last edited on Feb 17, 2014 at 8:26pm
Feb 17, 2014 at 8:24pm
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?
Feb 17, 2014 at 8:26pm
Oh, actually, that's a mistake...It's a void.
Feb 17, 2014 at 8:30pm
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 Feb 17, 2014 at 8:30pm
Feb 17, 2014 at 8:53pm
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.
Feb 17, 2014 at 8:54pm
Yep. No problem. Glad I could help.
Topic archived. No new replies allowed.