Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3021

Need help with C Code to print out Combinations in a Pick 3 Game

$
0
0
A very good day to you Sir !

I hope your day is going Great !

Below is the C Code to print the Combinations of numbers from an Array for a PICK 3 Game

The Code only does Singles ...

Can some kind soul please modify the code so that it also outputs Doubles ???

Thank you and regards

// Program to print all combination of size r in an array of size n
#include <stdio.h>

void printCombination(int arr[], int n, int r)
{
int data[r];
combinationUtil(arr, data, 0, n-1, 0, r);
}

void combinationUtil(int arr[], int data[], int start, int end, int index, int r)
{
if (index == r)
{
for (int j=0; j<r; j++)
printf("%d ", data[j]);
printf("\n");
return;
}

for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}

int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
}

Output:
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

Viewing all articles
Browse latest Browse all 3021