Click here to Skip to main content
12,246,754 members (57,147 online)
Click here to Skip to main content

Stats

224K views
5K downloads
64 bookmarked
Posted

Combinations in C++

, 12 Apr 2016 CPOL
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 (Senior)
Singapore Singapore
IT Certifications

  • IT Infrastructure Library Foundational (ITIL v3)
  • Scrum Alliance Certified Scrum Master (CSM)
  • EC-Council Certified Secure Programmer (ECSP) .NET
  • EC-Council Certified Ethical Hacker (CEH)
  • EC-Council Certified Security Analyst (ECSA)
  • Certified Secure Software Lifecycle Professional (CSSLP)

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 12 Apr 2016
Article Copyright 2016 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid