# Thread: Need help to find out combinations of elements of different array

1. ## Need help to find out combinations of elements of different array

Hi Everyone,

I have n no. of arrays with a0 to an no of elements in each array. I want a combination of each elements of 1st array, 2nd array, 3rd array...nth array. The 1st place will always be taken by elements of 1st array, 2nd place will always be taken by elements of 2nd array and similarly nth place will be taken by elements of nth array.

2. Nope.... we don't give out source code like that.

YOU make the effort... get the project started. Take it along as far as you can by yourself.
If you get stuck (really stuck, not lazy stuck) you can post your code and ask specific questions.

You don't learn anything by having others do your work for you.

3. To give a hint of how to perform the actual combination do it on paper first. Have 2 arrays, each with 2 values. Now on paper perform the combinations (there are only 4 of them so it should be easy). Then do it for arrays with 3 values instead. After that do it with arrays with 2 values again but use 3 arrays instead.

After that is done it should not be too difficult to write code that does what you just did on paper.

4. Originally Posted by Shakti
To give a hint of how to perform the actual combination do it on paper first. Have 2 arrays, each with 2 values. Now on paper perform the combinations (there are only 4 of them so it should be easy). Then do it for arrays with 3 values instead. After that do it with arrays with 2 values again but use 3 arrays instead.

After that is done it should not be too difficult to write code that does what you just did on paper.
Thanx for you help Shakti. Actually I am able to write the code and execute it properly when no. of arrays are fixed. Because no. of array equals no of for / while loops. Problem is that no of array are dynamically assigned. Can you help me with this. I know the answer to this problem is very simple but little tricky and I must be missing something.

5. Originally Posted by CommonTater
Nope.... we don't give out source code like that.

YOU make the effort... get the project started. Take it along as far as you can by yourself.
If you get stuck (really stuck, not lazy stuck) you can post your code and ask specific questions.

You don't learn anything by having others do your work for you.
If you do not know the answer its better to keep shut. Do not criticize (at least not some lazy criticism) to get more post counts. There are many people in the forum who really helps. And to your kind information I wasn't looking for source code. Just how to approach the problem would have been sufficient. No offence but I bet you cant even do that coz you don't even have a slightest idea of what my problem is.

6. First, welcome to the forum, Devilwar.

Your stated problem is an interesting one which I can't recall seeing before.

Second, you're WAY off base - Tater is one of the most prolific helpers on the forum - obviously he's trying to help you here, letting you know that we almost never give out code, upon request - rather, we leave you to start the program, and then if and when you get stuck, post up the program and state the problem with it. That saves us a huge amount of time, because quite often the student figures out the problem with just a small amount of assistance. Also, students who want code just for the asking, go elsewhere. We do get asked for code, a fair amount, as you might imagine. Some even request full comments and sent to their email address, if you can imagine.

Follow Shakti's good advice, and as you work it through on paper, or even with coins on a tabletop, notice the patterns of logic that you use to do this. It may take you a few times through it, but humans are decent problem solvers once they put their minds to it, and once you start seeing the patterns of logic that you use, you just need to note them down (in small steps), to help build up the pseudo code for your program.

7. Recursion. Im just going to leave you with that word and hopefully you will be able to figure it out

8. A 2-dimensional array? If I understand the problem correctly, it's a square array too. You can simply malloc the amount of memory needed for that since you want it to be dynamically assigned.
The tricky part is to devise a set of nested for loops that is dynamic. Or equivalent logic thereof.
Recursion perhaps, or manage your own "stack" of counters which is the same idea.

9. You may want to design your algorithm recursively, but it should be easy to eliminate it since it will be tail recursion.

What happened to tater???

10. Originally Posted by Shakti
Recursion. Im just going to leave you with that word and hopefully you will be able to figure it out
Hey Shakti,

Thank you dude. I know what you are suggesting. Yea since the no of loops are not fixed, i can control the no of loops by recursion. I missed this altogether. I really think this might work for me.

Thank you very much nonoobfor you suggestion too....

11. Actually there's nothing particularly recursive about this problem at all. In fact, a recursive implementation is terribly inefficient in this case. I just wrote a function in about 14 lines with two nested loops that solves it with any number of arrays of any sizes. No need to manage your own stack or anything like that.

12. Tater told me he was going to be concentrating on some tools for the Pelles C IDE/compiler, and working more with his electronics. He's on the Pelles C forum if you want to PM him.

13. So he's banned at his own request?

14. Originally Posted by TheBigH
So he's banned at his own request?
He's had a few prickly points with the mods, from time to time, but I didn't see one recently. Mods may have deleted it, of course.

All he told me was he was going to concentrate on the programming tools/libraries for Pelles C and electronics, and would be off this forum. I did read in the Pelles forum that he was going to give the new language Go, a look.

Popular pages Recent additions