Home>

Originally as below
char data[5]="ijklm";
char disp[5]="ijklm";
If i give the character to sort with, the following program

#include<stdio.h>
char data[5]="ijklm";
char disp[5]="ijklm";
int flg[5]={0};
void sub(int num)
{
    static int cnt = 0;
    if (num == 5) {
        printf("%s ",disp);
        cnt++;
        if (cnt %8 == 0) {
            printf("\n");
            cnt = 0;
        }
        return;
    }
    for (int i = 0;i<5;i++) {
        if (!flg[i]) {
            flg[i] = 1;
            disp[num] = data[i];
            sub(num + 1);
            flg[i] = 0;
        }
    }
}
int main()
{
sub(0);
return 0;
}


When you run

ikljm iklmj ikmjl ikmlj iljkm iljmk ilkjm ilkmj
ilmjk ilmkj imjkl imjlk imkjl imklj imljk imlkj
jiklm jikml jilkm jilmk jimkl jimlk jkilm jkiml
jklim jklmi jkmil jkmli jlikm jlimk jlkim jlkmi
jlmik jlmki jmikl jmilk jmkil jmkli jmlik jmlki
kijlm kijml kiljm kilmj kimjl kimlj kjilm kjiml
kjlim kjlmi kjmil kjmli klijm klimj kljim kljmi
klmij klmji kmijl kmilj kmjil kmjli kmlij kmlji
lijkm lijmk likjm likmj limjk limkj ljikm ljimk
ljkim ljkmi ljmik ljmki lkijm lkimj lkjim lkjmi
lkmij lkmji lmijk lmikj lmjik lmjki lmkij lmkji
mijkl mijlk mikjl miklj miljk milkj mjikl mjilk
mjkil mjkli mjlik mjlki mkijl mkilj mkjil mkjli
mklij mklji mlijk mlikj mljik mljki mlkij mlkji
And the output is 15 rows and 8 columns.

If i improve the above program as follows and input 5 characters,

#include<stdio.h>
void sub(int num)
{
char* data[5];
char* disp[5];
int flg[5]={0};
    static int cnt = 0;
    if (num == 5) {
        printf("%s ",disp);
        cnt++;
        if (cnt %8 == 0) {
            printf("\n");
            cnt = 0;
        }
        return;
    }
    for (int i = 0;i<5;i++) {
        if (!flg[i]) {
            flg[i] = 1;
            disp[num] = data[i];
            sub(num + 1);
            flg[i] = 0;
        }
    }
}
int main(){
char* data[5];
char* disp[5];
scanf("%s %s",&data,&disp);
sub(0);
return 0;
}


When ijklm is input, the blank is output, and the output is not the same as the program that originally provided ijklm.
If i know how to fix it, thank you

c++
  • Answer # 1

    Is it like this...

    #include<stdio.h>
    void sub(int num, int* cnt, int* flg, char* data, char* disp)
    {
      if (num == 5) {
        disp[5] ='\0';
        printf("%s ", disp);
        *cnt += 1;
        if (*cnt %8 == 0) {
          printf("\n");
          *cnt = 0;
        }
        return;
      }
      for (int i = 0;i<5;i++) {
        if (!flg[i]) {
          flg[i] = 1;
          disp[num] = data[i];
          sub(num + 1, cnt, flg, data, disp);
          flg[i] = 0;
        }
      }
    }
    int main()
    {
      char data[6];
      scanf("%s", data);
      char disp[6];
      int flg[5] = {0};
      int cnt = 0;
      sub(0,&cnt, flg, data, disp);
      return 0;
    }
    input
    ijklm
    output
    ijklm ijkml ijlkm ijlmk ijmkl ijmlk ikjlm ikjml
    ikljm iklmj ikmjl ikmlj iljkm iljmk ilkjm ilkmj
    ilmjk ilmkj imjkl imjlk imkjl imklj imljk imlkj
    jiklm jikml jilkm jilmk jimkl jimlk jkilm jkiml
    jklim jklmi jkmil jkmli jlikm jlimk jlkim jlkmi
    jlmik jlmki jmikl jmilk jmkil jmkli jmlik jmlki
    kijlm kijml kiljm kilmj kimjl kimlj kjilm kjiml
    kjlim kjlmi kjmil kjmli klijm klimj kljim kljmi
    klmij klmji kmijl kmilj kmjil kmjli kmlij kmlji
    lijkm lijmk likjm likmj limjk limkj ljikm ljimk
    ljkim ljkmi ljmik ljmki lkijm lkimj lkjim lkjmi
    lkmij lkmji lmijk lmikj lmjik lmjki lmkij lmkji
    mijkl mijlk mikjl miklj miljk milkj mjikl mjilk
    mjkil mjkli mjlik mjlki mkijl mkilj mkjil mkjli
    mklij mklji mlijk mlikj mljik mljki mlkij mlkji

  • Answer # 2

    char data[5]="ijklm";
    char disp[5]="ijklm";
    int flg[5]={0};

    However,
    A 5 character string requires 6 bytes
    '\0' is required at the end of the character string.

    So, let's make all array sizes 6

    char* data[5];
    char* disp[5];

    Why is it an array of pointers