Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi so I wrote this code to make anagrams, which means if i enter top it should g

ID: 3857210 • Letter: H

Question

Hi so I wrote this code to make anagrams, which means if i enter top it should give me top tpo opt otp pot pto

But I don't know how to fix my Loop function so that I can use my values from my other function. help me please

void permutation(string word,int index,int size)

{

int i;

if (index == size)

cout << word << endl;

else

{

loop(i, word.length());

}

}

  

void flip(char* a, char* b)

{

char ohlordhelpme;

ohlordhelpme = *a;

*a = *b;

*b = ohlordhelpme;

}

void Loop(int i, int max){

if (i>=max)

return;

flip(&word[index],&word[i]);

permutation(word, index + 1, size);

flip(&word[index],&word[i]);

Loop(i+1, word.length());

}

Explanation / Answer

First you don't need the permutation method. It won't work in c++ because you are doing circuar calling of methods inside Loop method.

#include <iostream>

#include <fstream>

#include <istream>

#include <cstring>

using namespace std;

void flip(char* a, char* b) // This method swaps two chars

{

char ohlordhelpme;

ohlordhelpme = *a;

*a = *b;

*b = ohlordhelpme;

}

// Pass char* pointer instead of string here. It will make easy to access string

void Loop(char* word, int index, int max){

int j;

if (index == max)

cout << word << " ";// Print anagram

else

{

for (int j = index; j <= max; j++)

{

flip((word+index),(word+j));// First swap the chars before doing permutaion

Loop(word, index + 1, max);// Call itself with different anagram

flip((word+index),(word+j));// Do swap on backtraking

}

}

  

}

int main()

{

char str[] = "top";// As it was in you problem

Loop(str, 0, 2); // Call your Loop here

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote