Ordering method Bubblesort with dynamic allocation + pointer

1

Hello everyone. I'm trying to solve an exercise by bubblesort ordering method and dynamic allocation (no pointers and no dynamic allocation the exercise gets easier, however, the teacher asked to do with AD). The exercise is simple, it is to say how many times the problem will turn, to pass the amount of numbers to be rearranged, and to play on the screen. Here is the code:

#include<stdio.h>
#include<stdlib.h>
main(){
    int qt,n,i,j,aux;
    int *p;
    scanf("%d",&qt);

    while(qt!=0){
        scanf("%d",&n);
        p = (int *) calloc(n, sizeof(int));

          for(i=0; i<n; i++){
                scanf("%d",(p+i));

          }

         /* for(i=0; i<n; i++){
                printf("%d\n",*p); /* Neste for, estou testando se o ponteiro está pegando os números, e está */
                ++p;
          } */

          for(i=0; i<n-1; i++){
            for(j=n-1; j>i; j--){
                if(*p < *(p-1)){
                    aux = *p;
                    *p = *(p-1);
                    *(p-1) = aux;

                }
            }
          }
          for(i=0; i<n; i++){
            printf("%d ",*p);
            ++p;
          }
          printf("\n");

          free(p);




        qt--;
    }

}

Anyway, the error happens in the sort method, when in the end, I start the reordered values, only appear garbage. I know I'm wrong in the sort method, I've tried to put a ++ p or --p, but it did not work. I think that * (p-1) is wrong. Could someone tell me the error? Thankful.

    
asked by anonymous 15.09.2016 / 21:37

1 answer

0

I found it easier to redo the program than to explain the errors individually ...

Points to note:

1) If you have int* p then *(p + i) is the same as p[i] .

2) Comments / * * / do not nest, that is, /* ..... */ ... */ usually goes    give compile error (if you're lucky)

3) Be careful when typing, do not change i, 1, l (and other similar cases)

4) use more spaces, it is easier to see the code ... for example, this is bad: while(qt!=0) , use while (qt != 0)

#include<stdio.h>
#include<stdlib.h>

int main()
{
   int *p;
   int qt, n, i, j, aux;

   printf("\nQuantas rodadas ? ");
   scanf("%d",&qt);

   while (qt != 0)
   {
      printf("\nQuantos numeros ? ");
      scanf("%d", &n);
      p = (int*)calloc(n, sizeof(int));

      // printf("Digites %d numeros e tecle ENTER: ", n);
      for (i = 0; i < n; i++)
      {
         printf("%d/%d: ", i+1, n);
         scanf("%d", (p+i));
      }

      for (i = 0; i < n-1; i++)
      {
         for (j = i+1; j < n; j++)
         {
            if (p[i] > p[j])
            {
               aux = p[i];
               p[i] = p[j];
               p[j] = aux;
             } // if
         } // for j
       } // for i

       for (i = 0; i < n; i++)
       {
          printf("%d ", p[i]);
       }

       printf("\n");

       free(p);

       qt--;

   } // while

}

That's all for now folks:)

    
16.09.2016 / 03:45