1,105,399 Community Members

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

Member Avatar
Candace Parker
Newbie Poster
3 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: