Question:
what code in c++ to see all possible combination 5 out of 30?
andy
2011-12-13 01:42:53 UTC
i just start learning c++ and my teacher tell us to combine all the thing that we learn and it's like taking all the combination 5 out 30 but it should be like this example
1. 1,2,3,4,5
2. 1,4,3,2,5
and so on with number on it with space with commabut and it should not be repeated but i don't know how to do it ... when i make it something problem come up .. please anyone help me... please....
Four answers:
2011-12-13 02:42:52 UTC
Please be more clear. At the moment it seems like you are speaking in Alien even to the best of programmers because we can not understand what you are trying to do (I am not saying I am a good C++ programmer).



Please take your time to use better English and make your problem clear, as that is the best way to recieve the correct help as fast as possible :)
Mr Ed
2011-12-13 19:28:20 UTC
Anne,



I agree with the other answers... your question is not very clear. But I think I know what you want. If so, here's your code in BASIC. (Sorry, I don't code in C++.) You will have to look at my code, figure it out, and do the same thing in C++. Please note that this will produce a VERY large text file. Reason: There are exactly 17,100,720 combinations of five numbers, out of the numbers 1 - 30:



OPEN "c:\combos.txt" FOR OUTPUT AS #1



FOR a = 1 TO 30

FOR b = 1 TO 30

FOR c = 1 TO 30

FOR d = 1 TO 30

FOR e = 1 TO 30

IF a <> b AND a <> c AND a <> d AND a <> e THEN

IF b <> c AND b <> d AND b <> e THEN

IF c <> d AND c <> e AND d <> e THEN

INCR total

combo$ = LTRIM$(STR$(a)) + "," + LTRIM$(STR$(b)) + ","

combo$ = combo$ + LTRIM$(STR$(c)) + "," + LTRIM$(STR$(d)) + ","

combo$ = combo$ + LTRIM$(STR$(e))

PRINT #1, total;". "; combo$

END IF

END IF

END IF

NEXT e

NEXT d

NEXT c

NEXT b

NEXT a

PRINT #1,

PRINT #1, total

CLOSE #1



Yahoo doesn't keep my indentation, so the above code will be hard to read. The above code will produce a file that begins like this:



1. 1,2,3,4,5

2. 1,2,3,4,6

3. 1,2,3,4,7

4. 1,2,3,4,8

5. 1,2,3,4,9

6. 1,2,3,4,10

7. 1,2,3,4,11

8. 1,2,3,4,12

9. 1,2,3,4,13

10. 1,2,3,4,14

11. 1,2,3,4,15

12. 1,2,3,4,16

13. 1,2,3,4,17

14. 1,2,3,4,18

15. 1,2,3,4,19

16. 1,2,3,4,20

17. 1,2,3,4,21

18. 1,2,3,4,22

19. 1,2,3,4,23

20. 1,2,3,4,24

21. 1,2,3,4,25

22. 1,2,3,4,26

23. 1,2,3,4,27

24. 1,2,3,4,28

25. 1,2,3,4,29

26. 1,2,3,4,30

27. 1,2,3,5,4

28. 1,2,3,5,6

29. 1,2,3,5,7

30. 1,2,3,5,8

31. 1,2,3,5,9

32. 1,2,3,5,10

33. 1,2,3,5,11

34. 1,2,3,5,12

35. 1,2,3,5,13

36. 1,2,3,5,14

37. 1,2,3,5,15

38. 1,2,3,5,16

39. 1,2,3,5,17

40. 1,2,3,5,18

41. 1,2,3,5,19

42. 1,2,3,5,20

43. 1,2,3,5,21

44. 1,2,3,5,22

45. 1,2,3,5,23

46. 1,2,3,5,24

47. 1,2,3,5,25

48. 1,2,3,5,26

49. 1,2,3,5,27

50. 1,2,3,5,28

51. 1,2,3,5,29

52. 1,2,3,5,30

53. 1,2,3,6,4



and ends like this:



17100693. 30,29,28,26,25

17100694. 30,29,28,26,27

17100695. 30,29,28,27,1

17100696. 30,29,28,27,2

17100697. 30,29,28,27,3

17100698. 30,29,28,27,4

17100699. 30,29,28,27,5

17100700. 30,29,28,27,6

17100701. 30,29,28,27,7

17100702. 30,29,28,27,8

17100703. 30,29,28,27,9

17100704. 30,29,28,27,10

17100705. 30,29,28,27,11

17100706. 30,29,28,27,12

17100707. 30,29,28,27,13

17100708. 30,29,28,27,14

17100709. 30,29,28,27,15

17100710. 30,29,28,27,16

17100711. 30,29,28,27,17

17100712. 30,29,28,27,18

17100713. 30,29,28,27,19

17100714. 30,29,28,27,20

17100715. 30,29,28,27,21

17100716. 30,29,28,27,22

17100717. 30,29,28,27,23

17100718. 30,29,28,27,24

17100719. 30,29,28,27,25

17100720. 30,29,28,27,26
green meklar
2011-12-13 21:53:19 UTC
I think I understand what you're talking about here. It isn't a terribly difficult problem. Note however that the two examples you gave have the same numbers in different orders, the version where ordering matters is known as a 'permutation', not a 'combination'.



You could go with either a recursive or iterative solution. Intuitively, this problem lends itself to recursion. However, a more general solution for longer strings might just run into excessively many recursive calls, so you might want to go with an iterative solution.



The pseudocode for a generalized recursive solution would look something like this:



- Function permutations(array A,string S,integer N):

- - If N is at most 0, do this:

- - - Print S.

- - - Return.

- - Set X to 0.

- - While X is less than the length of A, do this:

- - - If A[X] is not flagged, do this:

- - - - Flag A[X].

- - - - Set PS to S+A[X].

- - - - Set PN to N-1.

- - - - Call permutations(A,PS,PN).

- - - - Unflag A[X].

- - - Set X to X+1.



Where the + operator on strings represents concatenation; A is the array of values to select from (in your case it will have length 30) with all of them being unflagged when the original call to permutations() is made; S is the current output string which is empty when the original call to permutations() is made; and N is the additional depth to recurse to (in your case, the original call to permutations() will pass 5 as this value).



I'll leave the translation from pseudocode to C++ up to you.
2011-12-13 10:28:25 UTC
It looks like Yahoo is chopping off some of my statements,oh well



// execute.cpp : Defines the entry point for the console application.

//



#include "stdafx.h"



int printNumber(int *theNumber, int index, int noNumber,int minValue,int maxValue);

#define NO_NUMBERS 5

#define MIN_VALUE 1

#define MAX_VALUE 30



int _tmain(int argc, _TCHAR* argv[])

{

int theNumber[NO_NUMBERS];

int result = printNumber(theNumber,0,NO_NUMBERS,MIN_VALUE,MAX_VALUE);



if(getchar());

return result;

}



int printNumber(int *theNumber, int index, int noNumber,int minValue,int maxValue)

{

int value;

int count;



if((noNumber<1)||(maxValue<=minValue)||(index<0))

return -1;



for(value = minValue;value<=maxValue;value++)

{

theNumber[index] = value;



for( count= index-1;count>=0;count--)

{

if(theNumber[count]==theNumber[index])

break;

}

if(count==-1)

{

if((index+1)
printNumber(theNumber,index+1,noNumber,minValue,maxValue);

else

{

for(count = 0;count
{

printf("%d ",theNumber[count]);

}

printf("\n");

}

}

}

return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...