Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How do I write a c program to display all possible combinations from two arrays depending on filters?

I have 8 arrays in total, I have to write a program such that I find all possible combinations such that, There are total 14 elements in the final display, each chosen in following manner,
Each of the eight arrays can contain any number of elements (atleast to fulfill basic minimum conditions), but the total number of elements in each of arrays will be 28.

These elements will have numerical value one among (8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5) and one character string as its variable name

choose "2" elements from array 1 and array 2 ; final arrays should contain exactly two from this group
choose "3-6" elements from array 3 and array 4; final arrays should contain at-least 3 from this group
choose "2-4" elements from array 5 and array 6; final arrays should contain at-least 2 from this group
choose "3-6" elements from array 7 and array 8; final arrays should contain at-least 3 from this group

And total of all elements of final arrays should be less than or equal to 150.

And less than total 9 elements from arrays A1, A3, A5, A7
And less than total 9 elements from arrays A2, A4, A6, A8

I cannot get started with the algorithm itself. Please help.

following is the sample dataset
A1 = [Tom (8.5)]
A2 = [Ron (11.5), Ronny (12.5)]
A3 = [Abbie (12), Abby (11.5), alexa (11), alex (11), bert(11)]
A4 = [Lex (10), Luthor(10.5), thor(9.5), loki(10.5), jack(11), jackie(11.5)]
A5 = [Ali (11.5) Lee (10.5), nessy (11), Nes (10.5)]
A6 = [Alfie (11), Ala (11), Gail(11.5)]
A7 = [Andy (12), Nanny (10.5), nancy(8.5), bernie(9)]
A8 = [Casie (12.5), Ryan (10), Ben(9.5)]

And ouput will be arrays of 14 names from above according to the conditions
For eg:
output array1 : [tom, ron, abbie, abby, alexa, loki, jackie, Ali, nes, casie, ryan, ben, nancy, bernie]

The array1 has total less than 150, it has 2 from array 1 and 2, atleast 3 from array 3 and 4, atleast 2 from array 5 and 6, atleast 3 from array 7 and 8, and less than 9 elements from elements of A1, A3, A5, A7 and less than 9 elements from elements of A2, A4, A6, A8 Thus satisfying each and every condition;

Similarly Output should display all such arrays

What I have tried:

I tried with the total number of arrays that will be possible first, except the sum less than equal to feature
I tried to display final array but was unsuccessful in implementing the total less than or equal to filter and I was not able to use name variables and their numerical value at once
Posted
Updated 14-May-19 7:37am
v2
Comments
Patrice T 14-May-19 6:08am    
Give working sample data for the 8 arrays and output start.
Sunita-chandra 14-May-19 6:40am    
A1 = [Tom (8.5)]
A2 = [Ron (11.5), Ronny (12.5)]
A3 = [Abbie (12), Abby (11.5), alexa (11), alex (11), bert(11)]
A4 = [Lex (10), Luthor(10.5), thor(9.5), loki(10.5), jack(11), jackie(11.5)]
A5 = [Ali (11.5) Lee (10.5), nessy (11), Nes (10.5)]
A6 = [Alfie (11), Ala (11), Gail(11.5)]
A7 = [Andy (12), Nanny (10.5), nancy(8.5), bernie(9)]
A8 = [Casie (12.5), Ryan (10), Ben(9.5)]

And ouput will be arrays of 14 names from above according to the conditions
Patrice T 14-May-19 6:41am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Patrice T 14-May-19 6:42am    
please show a couple of outputs.
And do you have 2 or 3 possibles picks of 2 in arrays 1 and 2 ?
Rick York 14-May-19 11:03am    
We are here to help you with your code. We are not here to write it for you.

Work it out on paper: write your arrays as separate lines, and start following the rules manually. Each time you do something, write it down as an instruction.
When you have the solution, you can stop.
How select different data for your arrays, and use the instructions you generated to give you a result. Is it right? Excellent! Repeat a few times to be sure and you are ready to code.
If it isn't, then revise the instructions to cope with the problem. Try again with the first data set to make sure it isn't "broken" and then with the second again. Repeat until both data sets work. Now repeat with several more datasets.

When your manual instructions give the right result for all datasets, you can start converting that into code.
 
Share this answer
 
Comments
Sunita-chandra 14-May-19 6:45am    
Yes I tried that but since it has vast number of solutions possible, it is not possible to do it for whole dataset. I got the basics of forming loops and conditions to get desired outputs, but I cannot get it into one single program; I can display possible combinations, check if they lie in specfic range, and their sum is less than 150 individually, but cannot transform it into single program
OriginalGriff 14-May-19 7:03am    
How would you do it by hand, without a computer?
Patrice T 14-May-19 9:24am    
Show your code so far and explain where you are stuck.
Quote:
How do I write a c program to display all possible combinations from two arrays depending on filters?

Your job is to analyze the requirement to make some logic of it and do a hierarchical ordering, organize things, and build an algorithm of all this.

My quick analyze tells me that I would start by setting the number of names to pick in each array with respect of the conditions of requirement. Then, when I know how many names to pick in a pair of arrays, start to build the list of names for those arrays.

After that, one need to define missing details. for example: how many picks are legal in those?
2, 3? 4? or 6?
C++
A1 = [Tom (8.5)]
A2 = [Ron (11.5), Ronny (12.5)]

P1= [Tom, Ron]
P2= [Tom, Ronny]
P3= [Ron, Ronny]
P4= [Ron, Tom]
P5= [Ronny, Tom]
P6= [Ronny, Ron]

Depending on your answer, the code is not the same.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900