Like Tree3Likes
  • 1 Post By Adak
  • 1 Post By Shakti
  • 1 Post By nonoob

Need help to find out combinations of elements of different array

This is a discussion on Need help to find out combinations of elements of different array within the C Programming forums, part of the General Programming Boards category; Hi Everyone, I have n no. of arrays with a 0 to a n no of elements in each array. ...

  1. #1
    Registered User devilwar's Avatar
    Join Date
    Nov 2011
    Location
    Rourkela, India
    Posts
    5

    Post 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.

    Please help me write a C program for this.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    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. #4
    Registered User devilwar's Avatar
    Join Date
    Nov 2011
    Location
    Rourkela, India
    Posts
    5
    Quote Originally Posted by Shakti View Post
    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. #5
    Registered User devilwar's Avatar
    Join Date
    Nov 2011
    Location
    Rourkela, India
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    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. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    devilwar likes this.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Recursion. Im just going to leave you with that word and hopefully you will be able to figure it out
    devilwar likes this.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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.
    devilwar likes this.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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. #10
    Registered User devilwar's Avatar
    Join Date
    Nov 2011
    Location
    Rourkela, India
    Posts
    5
    Quote Originally Posted by Shakti View Post
    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....
    Last edited by devilwar; 01-17-2012 at 11:32 PM.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #13
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    So he's banned at his own request?
    Code:
    while(!asleep) {
       sheep++;
    }

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by TheBigH View Post
    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 subscribe to a feed

Similar Threads

  1. how to determine all combinations of array elements
    By DaleZ in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2010, 12:44 PM
  2. Replies: 5
    Last Post: 10-17-2010, 01:30 PM
  3. Replies: 5
    Last Post: 12-05-2007, 03:39 PM
  4. Obtaining all combinations of elements...
    By Mr_Miguel in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2006, 12:33 PM
  5. Replies: 2
    Last Post: 08-03-2003, 10:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21