Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Combinations in C++

, 13 Sep 2009
An article on finding combinations.
//char_comb_ex.cpp
//This program finds combinations using the recursive method

/*****Instructions*******
To see how it works for other combinations
Try setting N_NUM to other numbers (must not be greater than 10)
Try setting R_NUM to other numbers (must not be greater than N_NUM)
*************************/

#include "stdafx.h"
#include<iostream>

using namespace std;

//function prototype
void char_combination(char n[],int n_column,
            char r[], int r_size, int r_column, int loop);

int main()
{
	const int N_NUM=6; 
	const int R_NUM=4;
	
	char n[]="1234567890";

	char r[R_NUM+1];//+1 for the NULL character
	r[R_NUM]='\0';

	char_combination(n,0,r,R_NUM,0,N_NUM-R_NUM);
	
	cout<<"Complete!"<<endl;
	return 0;
}

void char_combination(char n[],int n_column,
            char r[], int r_size, int r_column, int loop)
{
	int localloop=loop;
	int local_n_column=n_column;
	
	///////Display the string code/////////
	if(r_column>(r_size-1))
	{
		cout<<r<<endl;
		return;
	}
	/////End of displaying string code//////
	
	for(int i=0;i<=loop;++i)
	{
		r[r_column]=n[n_column+i];
		++local_n_column;
		char_combination(n,local_n_column,r,r_size,r_column+1,localloop);
		--localloop;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Shao Voon Wong
Software Developer
Singapore Singapore
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 13 Sep 2009
Article Copyright 2003 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid