Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 13th, 2006, 5:27 AM   #1
veiga2
Newbie
 
veiga2's Avatar
 
Join Date: May 2006
Location: Philippines
Posts: 21
Rep Power: 0 veiga2 is an unknown quantity at this point
Newbie prob:how to make combinations without repeatition

About the code in making the numbers from 1-42, in different combinations without repeatition, by 6 numbers, is there any function about that?, hmmmm... i cant find the answer on this... i know im going to use the for loop but i cant find the solution... pls help me hnx

example: collection of numbers from 1-7
answer:
123456
123457
123467
123567
124567
134567
234567

Thanks for helping a newbie prob
veiga2 is offline   Reply With Quote
Old Aug 13th, 2006, 8:35 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Use nested for loops, where the start or end conditions of inner loops are determined by the value controlling outer loops.
grumpy is offline   Reply With Quote
Old Aug 15th, 2006, 6:31 PM   #3
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
uh, what the hell are you trying to do? "making numbers"? w/o repitition there is the fundamental theorem of arithmetic, which might help you.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Aug 16th, 2006, 1:41 AM   #4
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Next time, post in the proper forum: "Software design and algorithms".
std::vector<std::vector<value> >
get_combinations(std::vector<value> values, unsigned m)
{
	std::vector<std::vector<value> > ret;
	unsigned n = values.size();
	std::vector<value>::const_iterator* iters = 
		new std::vector<value>::const_iterator[m];
	for(unsigned i=0;i<m;i++)
		iters[i] = values.begin() + i;
	while(true)
	{
		unsigned i;
		std::vector<value> tmp;
		for(unsigned j=0;j<m;j++)
			tmp.push_back(*(iters[j]));
		ret.push_back(tmp);
		
		for(i=m;i>0;i--)
		{
			if(++(iters[i-1]) != values.end() - (m - i))
				break;
		}
		if(!i)
			break;
		for(unsigned j = i;j<m;j++)
		{
			iters[j] = iters[j-1]+1;
		}
	}
	return ret;
}
It was hard work forcing my brain to solve this in a non-recursive way.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old Aug 16th, 2006, 5:02 AM   #5
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
By the way, that will use ten metric fucktons of memory if you run it with a lot of values. If you needed to find all the combinations of a large set of values, but you didn't need to hold them all in memory at once, it'd be far better to implement as find_first_combination() and find_next_combination() functions.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Make the newbie feel welcome! TeamElement3 Community Introductions 22 Jul 17th, 2005 11:09 PM
Newbie: How to make simple Timer programe smith000monday Assembly 0 May 17th, 2005 2:54 AM
Newbie here... MuSiG Visual Basic 9 Apr 21st, 2005 7:50 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC