Thread: How to modify this code for chars instead of ints

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    How to modify this code for chars instead of ints

    This code will print out all the permutations for a given number set, it also allows you to set the length of the output. I want chars not ints can I just change the ints to chars where relevant?

    Code:
    #include<bits/stdc++.h>  
    
    using namespace std;  
    
    void combinationUtil(int arr[], int data[],  
    
                        int start, int end,  
                        int index, int r);  
    
    // The main function that prints  
    // all combinations of size r  
    // in arr[] of size n. This function 
    // mainly uses combinationUtil()  
    
    void printCombination(int arr[], int n, int r)  
    {  
        // A temporary array to store 
        // all combination one by one  
    
        int data[r];  
        // Print all combination using 
    
        // temprary array 'data[]'  
    
        combinationUtil(arr, data, 0, n-1, 0, r);  
    }  
    
    /* arr[] ---> Input Array  
    data[] ---> Temporary array to  
    store current combination  
    start & end ---> Staring and 
    Ending indexes in arr[]  
    index ---> Current index in data[]  
    r ---> Size of a combination to be printed */
    
    void combinationUtil(int arr[], int data[],  
    
                        int start, int end,  
    
                        int index, int r)  
    {  
    
        // Current combination is ready 
        // to be printed, print it  
    
        if (index == r)  
    
        {  
            for (int j = 0; j < r; j++)  
    
                cout << data[j] << " ";  
    
            cout << endl;  
    
            return;  
    
        }  
        // replace index with all possible  
    
        // elements. The condition "end-i+1 >= r-index" 
    
        // makes sure that including one element  
        // at index will make a combination with  
        // remaining elements at remaining positions  
    
        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);  
        }  
    }  
    
    // Driver code  
    
    int main()  
    {  
    
        int arr[] = {1, 2, 3, 4, 5};  
        int r = 3;  
        int n = sizeof(arr)/sizeof(arr[0]);  
        printCombination(arr, n, r);  
    }
    If I want to use chars instead of ints do I simply change the ints to chars such as,
    Code:
    int arr[] = {1, 2, 3, 4, 5};
    to
    Code:
    char arr[] = {'A', 'B', 'C', 'D'};

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,411
    I want chars not ints can I just change the ints to chars where relevant?
    Did you try it? Did it work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Isolating ints and chars
    By Shoone in forum C Programming
    Replies: 7
    Last Post: 04-06-2015, 05:55 PM
  2. Defining chars as ints help
    By jackalope in forum C Programming
    Replies: 16
    Last Post: 10-14-2010, 02:33 PM
  3. Converting Chars to Ints
    By sycorax in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2005, 10:40 PM
  4. Chars - ints
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 08:22 PM
  5. converting chars to ints
    By nebie in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2001, 11:33 AM

Tags for this Thread