Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Bubblesort help

Bubblesort help

Scheduled Pinned Locked Moved C / C++ / MFC
help
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kbury
    wrote on last edited by
    #1

    Can someone please help me with my bubblesort. It does not seem to sort and also prints 3 times.

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

    #define SIZE 6
    #define MAX 53

    void printArray(int N[], int T);
    void fillArray(int N[], int T);

    int main()
    {
    srand(time(NULL));

    int PICKED[] = {0,0,0,0,0,0};

    fillArray(PICKED, SIZE);
    printArray(PICKED, SIZE);

    return 0;
    }

    void fillArray(int N[], int T)
    {
    for(int P=0; P<T; P++)
    {
    N[P] = (int) (rand()% 52+1);

    for(int y=0; y<P; y++)
    {
    if(N[y] == N[P])
    {
    N[P] = (int) (rand()%52 +1);
    P=-1;
    }
    }
    }
    }

    void printArray(int N[], int T)

    {
    	for(int P=0; P<SIZE-1; P++)
    
    	{
    
    		if(N\[P\]>N\[P+1\])
    
    		{
    
    			int T = N\[P+1\];
    
    			N\[P+1\] = N\[P\];
    
    			N\[P\] = T;
    

    printf("\nLOTTO PICKS\n");
    for(int P=0; P<SIZE; P++)

    {
    printf("%d ", N[P]);

    printf("\n");

    }}}}

    _ D 2 Replies Last reply
    0
    • K kbury

      Can someone please help me with my bubblesort. It does not seem to sort and also prints 3 times.

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

      #define SIZE 6
      #define MAX 53

      void printArray(int N[], int T);
      void fillArray(int N[], int T);

      int main()
      {
      srand(time(NULL));

      int PICKED[] = {0,0,0,0,0,0};

      fillArray(PICKED, SIZE);
      printArray(PICKED, SIZE);

      return 0;
      }

      void fillArray(int N[], int T)
      {
      for(int P=0; P<T; P++)
      {
      N[P] = (int) (rand()% 52+1);

      for(int y=0; y<P; y++)
      {
      if(N[y] == N[P])
      {
      N[P] = (int) (rand()%52 +1);
      P=-1;
      }
      }
      }
      }

      void printArray(int N[], int T)

      {
      	for(int P=0; P<SIZE-1; P++)
      
      	{
      
      		if(N\[P\]>N\[P+1\])
      
      		{
      
      			int T = N\[P+1\];
      
      			N\[P+1\] = N\[P\];
      
      			N\[P\] = T;
      

      printf("\nLOTTO PICKS\n");
      for(int P=0; P<SIZE; P++)

      {
      printf("%d ", N[P]);

      printf("\n");

      }}}}

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      I suspect that P=-1 is not what you want.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      1 Reply Last reply
      0
      • K kbury

        Can someone please help me with my bubblesort. It does not seem to sort and also prints 3 times.

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

        #define SIZE 6
        #define MAX 53

        void printArray(int N[], int T);
        void fillArray(int N[], int T);

        int main()
        {
        srand(time(NULL));

        int PICKED[] = {0,0,0,0,0,0};

        fillArray(PICKED, SIZE);
        printArray(PICKED, SIZE);

        return 0;
        }

        void fillArray(int N[], int T)
        {
        for(int P=0; P<T; P++)
        {
        N[P] = (int) (rand()% 52+1);

        for(int y=0; y<P; y++)
        {
        if(N[y] == N[P])
        {
        N[P] = (int) (rand()%52 +1);
        P=-1;
        }
        }
        }
        }

        void printArray(int N[], int T)

        {
        	for(int P=0; P<SIZE-1; P++)
        
        	{
        
        		if(N\[P\]>N\[P+1\])
        
        		{
        
        			int T = N\[P+1\];
        
        			N\[P+1\] = N\[P\];
        
        			N\[P\] = T;
        

        printf("\nLOTTO PICKS\n");
        for(int P=0; P<SIZE; P++)

        {
        printf("%d ", N[P]);

        printf("\n");

        }}}}

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        First, format your code so that it is easier to read. Use the <pre> tags for this. Second, break your code up into three functions instead of two: fill, sort, and print. As far as printing, check the two for() loops in printArray(). In the end, you should end up with something resembling:

        void fillArray( int N[], int T )
        {
        for (int P = 0; P < T; P++)
        N[P] = (int) (rand() % 52 + 1);
        }

        void sortArray( int N[], int T )
        {
        }

        void printArray( int N[], int T )
        {
        printf("LOTTO PICKS\n");

        for (int P = 0; P < T; P++)
            printf("%d\\n", N\[P\]);
        

        }

        void main( void )
        {
        srand(time(NULL));

        int PICKED\[\] = {0,0,0,0,0,0};
        
        fillArray(PICKED, SIZE);
        sortArray(PICKED, SIZE);
        printArray(PICKED, SIZE);
        

        }

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        K 1 Reply Last reply
        0
        • D David Crow

          First, format your code so that it is easier to read. Use the <pre> tags for this. Second, break your code up into three functions instead of two: fill, sort, and print. As far as printing, check the two for() loops in printArray(). In the end, you should end up with something resembling:

          void fillArray( int N[], int T )
          {
          for (int P = 0; P < T; P++)
          N[P] = (int) (rand() % 52 + 1);
          }

          void sortArray( int N[], int T )
          {
          }

          void printArray( int N[], int T )
          {
          printf("LOTTO PICKS\n");

          for (int P = 0; P < T; P++)
              printf("%d\\n", N\[P\]);
          

          }

          void main( void )
          {
          srand(time(NULL));

          int PICKED\[\] = {0,0,0,0,0,0};
          
          fillArray(PICKED, SIZE);
          sortArray(PICKED, SIZE);
          printArray(PICKED, SIZE);
          

          }

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          K Offline
          K Offline
          kbury
          wrote on last edited by
          #4

          I have tried this, prints out but will not generate random numbers

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

          #define SIZE 6
          #define MAX 53

          void printArray(int N[], int T);
          void fillArray(int N[], int T);

          int main(void)
          {
          srand(time(NULL));

          int PICKED[] = {0,0,0,0,0,0};

          fillArray(PICKED, SIZE);
          printArray(PICKED, SIZE);

          return 0;
          }

          void fillArray(int N[], int T)
          {
          for(int P=0; P<SIZE; P++)
          {
          N[P] = rand() % 52+1;

          for(int PASS=0; PASS<P; PASS++)
          {
          if(N[PASS] == N[P])
          {
          N[P] = (int) (rand()%52 +1);
          P=-1;
          }
          }
          }
          }

          void printArray(int N[], int T)
          {
          int PASS, P;

          for(P=0; P<SIZE-1; P++)
          {
          
              for(PASS = P + 1; PASS < SIZE; PASS++)
              {
              if(N\[P\]>N\[PASS\])
                  {
             	        T = N\[PASS\];
          	N\[P\] = N\[PASS\];
          	N\[P\] = T;
                  }
              }
          }
          
          printf("\\nLOTTO PICKS\\n\\n");
          for(int P=0; P<SIZE; P++)
             printf("%d ", N\[P\]);
          
          printf("\\n");
          

          }

          D CPalliniC 2 Replies Last reply
          0
          • K kbury

            I have tried this, prints out but will not generate random numbers

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

            #define SIZE 6
            #define MAX 53

            void printArray(int N[], int T);
            void fillArray(int N[], int T);

            int main(void)
            {
            srand(time(NULL));

            int PICKED[] = {0,0,0,0,0,0};

            fillArray(PICKED, SIZE);
            printArray(PICKED, SIZE);

            return 0;
            }

            void fillArray(int N[], int T)
            {
            for(int P=0; P<SIZE; P++)
            {
            N[P] = rand() % 52+1;

            for(int PASS=0; PASS<P; PASS++)
            {
            if(N[PASS] == N[P])
            {
            N[P] = (int) (rand()%52 +1);
            P=-1;
            }
            }
            }
            }

            void printArray(int N[], int T)
            {
            int PASS, P;

            for(P=0; P<SIZE-1; P++)
            {
            
                for(PASS = P + 1; PASS < SIZE; PASS++)
                {
                if(N\[P\]>N\[PASS\])
                    {
               	        T = N\[PASS\];
            	N\[P\] = N\[PASS\];
            	N\[P\] = T;
                    }
                }
            }
            
            printf("\\nLOTTO PICKS\\n\\n");
            for(int P=0; P<SIZE; P++)
               printf("%d ", N\[P\]);
            
            printf("\\n");
            

            }

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            kbury wrote:

            I have tried this...

            Did you compare with the code snippet I provided?

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            K 1 Reply Last reply
            0
            • D David Crow

              kbury wrote:

              I have tried this...

              Did you compare with the code snippet I provided?

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              K Offline
              K Offline
              kbury
              wrote on last edited by
              #6

              I tried and changed to the code snippet that you had, this is what I get. 34 24 3 32 33 50 I need it to print for example LOTTO PICKS 3 8 12 21 22 39 and cannot seem to fix it to do that

              D 1 Reply Last reply
              0
              • K kbury

                I have tried this, prints out but will not generate random numbers

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

                #define SIZE 6
                #define MAX 53

                void printArray(int N[], int T);
                void fillArray(int N[], int T);

                int main(void)
                {
                srand(time(NULL));

                int PICKED[] = {0,0,0,0,0,0};

                fillArray(PICKED, SIZE);
                printArray(PICKED, SIZE);

                return 0;
                }

                void fillArray(int N[], int T)
                {
                for(int P=0; P<SIZE; P++)
                {
                N[P] = rand() % 52+1;

                for(int PASS=0; PASS<P; PASS++)
                {
                if(N[PASS] == N[P])
                {
                N[P] = (int) (rand()%52 +1);
                P=-1;
                }
                }
                }
                }

                void printArray(int N[], int T)
                {
                int PASS, P;

                for(P=0; P<SIZE-1; P++)
                {
                
                    for(PASS = P + 1; PASS < SIZE; PASS++)
                    {
                    if(N\[P\]>N\[PASS\])
                        {
                   	        T = N\[PASS\];
                	N\[P\] = N\[PASS\];
                	N\[P\] = T;
                        }
                    }
                }
                
                printf("\\nLOTTO PICKS\\n\\n");
                for(int P=0; P<SIZE; P++)
                   printf("%d ", N\[P\]);
                
                printf("\\n");
                

                }

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                In function fillArray:

                kbury wrote:

                P=-1;

                is wrong, change to

                PASS = -1;

                In function printArray

                kbury wrote:

                N[P] = N[PASS];

                is wrong, change to:

                N[PASS] = N[P];

                Some remarks (feel free to ignore...)

                • There are better ways for implementing the fillArray function (i.e. avoiding the 'duplicates' extraction).
                • Messing up with the loop index is a very inelegant practice.
                • printArray really should be called sortArray.

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                K 1 Reply Last reply
                0
                • CPalliniC CPallini

                  In function fillArray:

                  kbury wrote:

                  P=-1;

                  is wrong, change to

                  PASS = -1;

                  In function printArray

                  kbury wrote:

                  N[P] = N[PASS];

                  is wrong, change to:

                  N[PASS] = N[P];

                  Some remarks (feel free to ignore...)

                  • There are better ways for implementing the fillArray function (i.e. avoiding the 'duplicates' extraction).
                  • Messing up with the loop index is a very inelegant practice.
                  • printArray really should be called sortArray.

                  :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  K Offline
                  K Offline
                  kbury
                  wrote on last edited by
                  #8

                  Thank you very much for your help. I have been working on this for about one week now. I really appreciate it. It works perfect now.

                  CPalliniC 1 Reply Last reply
                  0
                  • K kbury

                    I tried and changed to the code snippet that you had, this is what I get. 34 24 3 32 33 50 I need it to print for example LOTTO PICKS 3 8 12 21 22 39 and cannot seem to fix it to do that

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Did you implement sortArray() (or just leave it empty)?

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    1 Reply Last reply
                    0
                    • K kbury

                      Thank you very much for your help. I have been working on this for about one week now. I really appreciate it. It works perfect now.

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #10

                      You're welcome. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups