CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2016
    Posts
    2

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

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

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

    Do you have an idea how to do it using a paper sheet and a pencil?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

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

    modify the code so that it also outputs Doubles
    First, as Victor states in post #2, you need to determine how you would do this without a computer. Once you know this then the method used can be coded.

    See
    http://www.codeguru.com/cpp/cpp/algo...tions-in-C.htm
    http://www.codeproject.com/Articles/...binations-in-C
    http://stackoverflow.com/questions/9...binations-in-c

    and others
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured