We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,731 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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;
}
2
Contributors
1
Reply
8 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
Candace Parker
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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).

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0605 seconds using 2.73MB