Answer to Question #99408 in C++ for Caleb

Question #99408
Output all combinations of character variables a, b, and c. If a = 'x', b = 'y', and c = 'z', then the output is:
xyz xzy yxz yzx zxy zyx
Your code will be tested in three different programs, with a, b, c assigned with 'x', 'y', 'z', then with '#', '$', '%', then with '1', '2', '3'.
1
Expert's answer
2019-12-16T05:40:54-0500
#include <iostream>
#include <string>

using namespace std;

// Array with characters
char M[3];

// Function that creates the sequence
void nextChar(char *, int);

int main(){
    // Variables
    char a, b, c;
    cout << "Enter a, b, c with a space\n";
    cin >> a >> b >> c;

    // Fill the array
    M[0] = a;
    M[1] = b;
    M[2] = c;

    // Array storing sequence
    char * pt = new char[4];
    for(int i = 0; i <= 3; i++)
        pt[i] = 0;

    // Create a sequence
    nextChar(pt, 0);

    // Delete array
    delete[] pt;

    return 0;
}

// Function that creates the sequence
void nextChar(char * pt, int k){
    // Loop
    for(int i = 0; i < 3; i++)
        if(M[i]!=0){
            pt[k] = M[i];
            M[i] = 0;
            if(k + 1 < 3)
                nextChar(pt, k + 1);
            else{
                cout << pt << endl;
            }
            M[i] = pt[k];
        }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS