-1

This program is sorting the string inside the arrays.

The function Sort is stoping after the 3rd time it runs with no compile errors

int  main(){
   char * arrP1[] = { "father", "mother", NULL };
   char * arrP2[] = { "sister", "brother", "grandfather", NULL };
   char * arrP3[] = { "grandmother", NULL };
   char * arrP4[] = { "uncle", "aunt", NULL };
   char ** arrPP[] = { arrP1, arrP2, arrP3, arrP4 , NULL }; 

   printAllStrings(arrPP);

   sort(arrPP);
   printAllStrings(arrPP);


   return 0;
}

void sort(char ** arrPP[]) {
int i, j, n, pi, pj;
int t;
char * temp;

for (n = 0; n < 8; n++) {
    pi = 0;
    pj = 0;
    printf("round %d\n", n);

    for (i = 0; i < (sizeof(arrPP)); i++) {
        for (j = 0; arrPP[i][j] != NULL; j++) {
            t = 0;
            if (i == 0 && j == 0)
                continue;

            while (1) { // checking wich word is bigger and swiching between them if needed

                if (arrPP[pi][pj][t] == arrPP[i][j][t])
                    continue;

                if (arrPP[pi][pj][t] > arrPP[i][j][t]) {


                    temp = arrPP[pi][pj];
                    arrPP[pi][pj] = arrPP[i][j];
                    arrPP[i][j] = temp;
                    break;
                }
                else {
                    break;
                }
                t++;
            }
            pi = i;
            pj = j;

        }
    }

}
}

outpot:

(father, mother)

(sister, brother, grandfather)

(grandmother)

(uncle, aunt)

round 0

round 1

round 2

expected outpot:

(father, mother)

(sister, brother, grandfather)

(grandmother)

(uncle, aunt)

round 0

round 1

round 2

round 3

round 4

round 5

round 6

round 7

(aunt, brother)

(father, grandfather, grandmother)

(mother)

(siter, uncle)

  • 2
    What does the debugger tell you when you step through the code? – Ken White Jan 10 '17 at 03:19
  • I dont really know how to use the debugger in visual studio.. but when i run the program with the debugger it show the output on the console and show the precentage of the cpu until it get to 100% ..sounds like an infinite loop? – Chen Kahalany Jan 10 '17 at 03:40
  • @ChenKahalany Use the debugger is fairly easy. Go to a line and press F9 to set a break point. Then press F5 to launch the debugger, press F10 to step throw the code. Watch the results in the watch window. – nikau6 Jan 10 '17 at 03:48
  • 1
    `sizeof(arrPP)` gives the size of a pointer, not the size of your arrays. – nikau6 Jan 10 '17 at 03:51
  • @nikau6 so how can i get the size of the array arrPP? – Chen Kahalany Jan 10 '17 at 03:58
  • @ChenKahalany go through it until you hit the null pointer on the end – M.M Jan 10 '17 at 04:07

1 Answers1

1

I run your code in a debugger, I didn't really try to see if it works, I only tried to find why you get a infinite loop. The problem is there :

 while (1) 
  { 
      if (arrPP[pi][pj][t] == arrPP[i][j][t])
          continue;
          ...

if the result of the comparison is positive you enter a infinite loop. It happens when comparing grandfather with grandmother.

nikau6
  • 922
  • 9
  • 16
  • Ok, but your code works only cause they're four elements in your 3D array, and sizeof(arrPP) gives the size of a pointer which is.. four for you. That's pure luck. You need to stock the size of your 3D array somewhere in your code. – nikau6 Jan 10 '17 at 04:22