Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ bubble sort 2d array error.

Options
  • 31-01-2011 6:54pm
    #1
    Registered Users Posts: 649 ✭✭✭


    SOLVED SOLVED SOLVED SOLVED SOLVED SOLVED

    'm having a problem with the following code where 19 gets put in when it's not there and you loose one of the numbers in the array because of it.

    The problem seems to happen when the bubble sort does its stuff...

    I can't for the life of me figure out what is causing the 19...and when there actually is a 19 in the array TWO appear xD

    Any help would be appreciated.

    #include<iostream.h>
    #include<conio.h>
    #include<iomanip.h>
    #include<stdlib.h>
    //Name:
    //Author:
    //Date:
    void generate(void);
    void extract(void);
    void bubble_sort(void);
    void pushback(void);
    void display2d(void);
    
    int arr2d[4][4], arr1d[16];
    main()
    {
     generate();                //call generate() to populate the 2d array
     display2d();                //display the unsorted 2d array
     extract();             //pull nums out of 2d into 1d array
     bubble_sort();         //use bubble sort to sort in 1d array
     pushback();            //push nums back into 2d from sorted 1d array
     display2d();           //show sorted 2d array on screen
    }
    //------------------------------------------------------------------------
    void generate()
    {
     int x,y;
     randomize();
    
     for(x=0;x<4;x++)
     {
      for(y=0;y<4;y++)
       {
        arr2d[x][y]= random(50)+1; //pick random number & store in 2d array
       }
      }
    }        //end of generate function
    //------------------------------------------------------------------------
    void extract()
     {
      int x,y,cnt=0;
    
     for(x=0;x<4;x++)
     {
      for(y=0;y<4;y++)
       {
        arr1d[cnt] = arr2d[x][y];   //take from 2d array & put into 1d array
        cout<<arr1d[cnt]<<" ";      //show value on screen
        cnt++;                      //increase index into 1d array by 1
       }
     }
     cout<<endl;
    }            //end of extract function
    //------------------------------------------------------------------------
    void bubble_sort()
    {
          int i,x, j, flag = 1;    // set flag to 1 to start first pass
          int temp;                // holding variable
    
        while(flag == 1)
         {
              flag = 0;
              for (j=0; j < 16; j++)
               {
                   if (arr1d[j+1] < arr1d[j])  // ascending order simply changes to <
                  {
                        temp = arr1d[j];       // swap elements
                        arr1d[j] = arr1d[j+1];
                        arr1d[j+1] = temp;
                        flag = 1;              // indicates that a swap occurred.
                   }
               }
         }
    }
    //------------------------------------------------------------------------
    void pushback()
    {
     int x,y,cnt=0;
    
     for(x=0;x<4;x++)
     {
      for(y=0;y<4;y++)
       {
        arr2d[x][y] = arr1d[cnt];  //take number from 1d array & put into 2d
        cnt++;                     //increase index into 1d array
       }
     }
    }
    //------------------------------------------------------------------------
    void display2d()
    {
     int x,y;
      cout<<endl;
    
     for(x=0;x<4;x++)
     {
      for(y=0;y<4;y++)
       cout<<setw(4)<<arr2d[x][y]; //output element off array in field width 4
    
       cout<<endl;
     }
     cout<<endl;
    
    getch();
    }
    //------------------------------------------------------------------------
    


Comments

  • Closed Accounts Posts: 427 ✭✭scotty_irish


    set your loop from 0 to 15, not 16!as then you access an non existant 17th element.

    easy mistake to make!

    good luck!


  • Registered Users Posts: 649 ✭✭✭Steviemoyne


    set your loop from 0 to 15, not 16!as then you access an non existant 17th element.

    easy mistake to make!

    good luck!


    You sir. Are a genius.

    Thanks very much!


Advertisement