1.11M Members

Lists the permutations of ABCDEF where C and E are beside each other in any oder

 
0
 

Good day, people! I have here a code in C that needs improvement. It should list all the permutations of ABCDEF where C and E should be beside each other in any order (CE and EC). I know that I must treat C and E as one so I used Z temporarily. But when I run the program, it says that there is an overflow in implicit constant conversion. The only way I know to replace Z with CE and EC is using putc and getc. It's not working so I think there is another code for that. Please guys help me. Thank you in advance! Any kind of help is appreciated.

# include <stdio.h>

FILE * file;

void swap (char *x, char *y) {
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void permute(char *a, int i, int n) {
   int j;
   if (i == n) {
     printf("%s\n", a);
     fprintf(file, "%s\n", a);
   }
   else {
        for (j = i; j <= n; j++) {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j));
       }
   }
}

int main() {
   char a[] = "ABZDF";
   char ch;
   file = fopen("permutations.txt", "w");
   permute(a, 0, 4);
   while (!feof(file)) {
        ch= getc (file);
        switch(ch) {
            case 'Z': ch='CE';
        }
        putc (ch, file);
    }
   fclose(file);
   getchar();
   return 0;
}
 
0
 

'CE' is not a character therefor you can't use putc() Fix your switch to output a 'C' then 'E' in case-Z and output the character if not case-Z (default).

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: