Thread: help with bubble sort 2D array

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    help with bubble sort 2D array

    Code:
    /*
       Dang Vo
       Lab 2
       [email protected]
    */
    
    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAX 50
    #define LEN 20
    #define NUM_JUDGE 5
    #define INCLUDE_JUDGE 3
    
    int getData(FILE* spData, char name[][LEN], float difficulty[], float score[][NUM_JUDGE]);
    void BubbleSort(float score[][NUM_JUDGE], int numDiver);
    void Caltotal(float total[],float difficulty[], float score[][NUM_JUDGE]);
    void printData(float total[], char name[][LEN], float score[][NUM_JUDGE], int size);
    void printHeader();
    
    int main(void)
    {
        // Global Declarations
        FILE*spData;
        char name[MAX][LEN];
        float difficulty[MAX];
        float score[MAX][NUM_JUDGE];
        float total[MAX];
        int numDiver = 0;
    	int size =4;
        //statement
        if((spData=fopen("DIV1.txt","r"))== NULL)
         {
              printf("\n Error opennning file!\n");
              exit(100);
         }
        else
        {
            printf("opened file!\n");
        }
        // function calls
        numDiver = getData(spData, name, difficulty, score);
        Caltotal(total,difficulty, score);
        printHeader();
        printData(total, name, score, numDiver);
    
        fclose(spData);
        int i;
        scanf("%d", &i);
    
    
    return 0;
    }
    
    
    /*===============================getdata=====================================
    Pre: SpData is oppened,
    Post:
    */
    
    int getData(FILE* spData, char name[][LEN], float difficulty[], float score[][NUM_JUDGE])
    
    {
         int i=0;
         int j=0;
    
    
         while (i < MAX && (fscanf(spData,"%c", &name[i][0]) != EOF))
         {
    
                for(j = 1; j < LEN; j++)
                {
                    fscanf(spData,"%c", &name[i][j]);
                }
    
                fscanf(spData,"%f", &difficulty[i]);
                  for(j = 0; j < NUM_JUDGE; j++)
                  {
                      fscanf(spData,"%f\n", &score[i][j]);
    
                  }
    
    
                i++;
            }
    
        return i;
        BubbleSort(score, i);
    }
    
    
    /*===============================BubbleSort==================================
    Pre:
    Post:
    */
    void BubbleSort(float score[][NUM_JUDGE], int size)
    {
        int current;
        float temp;
        int walk;
    
            for(current = 0; current < size; current++)
    			
            {
                for(walk = size; walk >= current; walk--)
                    if(score[current][walk] < score[current][walk-1])
                    {
                        temp                   = score[current][walk];
                        score[current][walk]   = score[current][walk-1];
                        score[current][walk-1] = temp;
                    }
            }
    		
    
    
        return;
    }
    
    
    /*===============================Caltotal======================================
    Pre:
    Post:
    */
    void Caltotal(float total[],float difficulty[], float score[][NUM_JUDGE])
    {
        // Local Declarations
        int i;
        int j;
        float temp = 0;
        for(i = 0; i < MAX; i++)
        for(j =0; j < INCLUDE_JUDGE; j++)
           {
               temp += score[i][j+1];
           }
    
          total[i] = temp * difficulty[i] / 3;
    return;
    }
    
    
    /*===============================printHeader======================================
    Pre:
    Post:
    */
    void printHeader()
    {
    // Local Declaration
        FILE*fpOut;
    
    // Local Statments
        if(!(fpOut = fopen("output.txt", "w")))
            printf("Error writing output.txt"); //checks to see if output.txt opens
    
        fprintf(fpOut, "Dang Khoa Vo [email protected]          Lab#2\n\n");
    
        fprintf(fpOut, "NAME                  Scores         Total Score\n");
        fprintf(fpOut, "====================  ===  ===  ===  ===========\n");
    
        fclose(fpOut);
    
        return;
    }
    
    /*===============================PrintData=====================================
    Pre:
    Post:
    */
    
    void printData(float total[], char name[][LEN], float score[][NUM_JUDGE], int numDiver)
    {
    // Local Declaration
        FILE*fpOut;
        int i,n;
    
    // Local Statments
        if(!(fpOut = fopen("output.txt", "a")))
            printf("Error writing output.txt"); //checks to see if output.txt opens
    
        for (i=0; i< numDiver;i++)
            {
             for(n = 0; n < LEN; n++)
                    fprintf(fpOut, "%c", name[i][n]);
             for(n = 0; n < 3; n++)
                    fprintf(fpOut, "  %3.1f", score[i][n]);
    
             fprintf(fpOut, "     %5.2f", total[i]);
             fprintf(fpOut, "\n");
            }
    
        fprintf(fpOut, "\n");
        fclose(fpOut);
    
        return;
    }
    so i wanted to sort the last 5 number of each row of the input data from the attach file
    but my sort function does not work can anyone tell me how to fix this problem could you give me an example of sorting 2d array using bubble sort. thank you
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Function getData() returns before calling BubbleSort(), so BubbleSort() is never called.

    But if BubbleSort() were called, it would access element score[.][size], which is beyond that end of data.
    If there are N elements in an array, you index them 0 to N-1.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    like this ?
    Code:
    /*
       Dang Vo
       Lab 2
       [email protected]
    */
    
    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAX 50
    #define LEN 20
    #define NUM_JUDGE 5
    #define INCLUDE_JUDGE 3
    
    int getData(FILE* spData, char name[][LEN], float difficulty[], float score[][NUM_JUDGE]);
    void BubbleSort(float score[][NUM_JUDGE], int numDiver);
    void Caltotal(float total[],float difficulty[], float score[][NUM_JUDGE]);
    void printData(float total[], char name[][LEN], float score[][NUM_JUDGE], int numDiver);
    void printHeader();
    
    int main(void)
    {
        // Global Declarations
        FILE*spData;
        char name[MAX][LEN];
        float difficulty[MAX];
        float score[MAX][NUM_JUDGE];
        float total[MAX];
        int numDiver = 0;
    	int size =4;
        //statement
        if((spData=fopen("DIV1.txt","r"))== NULL)
         {
              printf("\n Error opennning file!\n");
              exit(100);
         }
        else
        {
            printf("opened file!\n");
        }
        // function calls
        numDiver = getData(spData, name, difficulty, score);
        Caltotal(total,difficulty, score);
        printHeader();
        printData(total, name, score, numDiver);
    
        fclose(spData);
        int i;
        scanf("%d", &i);
    
    
    return 0;
    }
    
    
    /*===============================getdata=====================================
    Pre: SpData is oppened,
    Post:
    */
    
    int getData(FILE* spData, char name[][LEN], float difficulty[], float score[][NUM_JUDGE])
    
    {
         int i=0;
         int j=0;
    
    
         while (i < MAX && (fscanf(spData,"%c", &name[i][0]) != EOF))
         {
    
                for(j = 1; j < LEN; j++)
                {
                    fscanf(spData,"%c", &name[i][j]);
                }
    
                fscanf(spData,"%f", &difficulty[i]);
                  for(j = 0; j < NUM_JUDGE; j++)
                  {
                      fscanf(spData,"%f\n", &score[i][j]);
    
                  }
    
    
                i++;
            }
    	BubbleSort(score, i);
        return i;
        
    }
    
    
    /*===============================BubbleSort==================================
    Pre:
    Post:
    */
    void BubbleSort(float score[][NUM_JUDGE], int numDiver)
    {
        int i;
        float temp;
        int walk;
    
            for(i = 0; i< numDiver;i++)
    
            {
                for(walk = NUM_JUDGE-1; walk >= 0; walk--)
                    if(score[i][walk] < score[i][walk-1])
                    {
                        temp             = score[i][walk];
                        score[i][walk]   = score[i][walk-1];
                        score[i][walk-1] = temp;
                    }
            }
    		
    
    
        return;
    }
    
    
    /*===============================Caltotal======================================
    Pre:
    Post:
    */
    void Caltotal(float total[],float difficulty[], float score[][NUM_JUDGE])
    {
        // Local Declarations
        int i;
        int j;
        float temp = 0;
        for(i = 0; i < MAX; i++)
        for(j =0; j < INCLUDE_JUDGE; j++)
           {
               temp += score[i][j+1];
           }
    
          total[i] = temp * difficulty[i] / 3;
    return;
    }
    
    
    /*===============================printHeader======================================
    Pre:
    Post:
    */
    void printHeader()
    {
    // Local Declaration
        FILE*fpOut;
    
    // Local Statments
        if(!(fpOut = fopen("output.txt", "w")))
            printf("Error writing output.txt"); //checks to see if output.txt opens
    
        fprintf(fpOut, "Dang Khoa Vo [email protected]          Lab#2\n\n");
    
        fprintf(fpOut, "NAME                  Scores         Total Score\n");
        fprintf(fpOut, "====================  ===  ===  ===  ===========\n");
    
        fclose(fpOut);
    
        return;
    }
    
    /*===============================PrintData=====================================
    Pre:
    Post:
    */
    
    void printData(float total[], char name[][LEN], float score[][NUM_JUDGE], int numDiver)
    {
    // Local Declaration
        FILE*fpOut;
        int i,n;
    
    // Local Statments
        if(!(fpOut = fopen("output.txt", "a")))
            printf("Error writing output.txt"); //checks to see if output.txt opens
    
        for (i=0; i< numDiver;i++)
            {
             for(n = 0; n < LEN; n++)
                    fprintf(fpOut, "%c", name[i][n]);
             for(n = 0; n < 3; n++)
                    fprintf(fpOut, "  %3.1f", score[i][n]);
    
             fprintf(fpOut, "     %5.2f", total[i]);
             fprintf(fpOut, "\n");
            }
    
        fprintf(fpOut, "\n");
        fclose(fpOut);
    
        return;
    }


    but the sort function still doesnt work though is there anything wrong with the logic in sort fucntion ?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    A bubble sort usually involves two 'for' loops to sort the same array. You've got each loop doing unrelated things. Also, 'walk" goes down to zero yet you are accessing [walk - 1] which is out-of-range of the array.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    while your at it can you tell me what i did wrong for my calctotal function also and i fixed the sort function it worked now cant really see any mistake myself now since my eyeball is rolling out. i havent sleep the whole night

  6. #6
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    This bubble sort function is operating on a two-dimensional array.
    It's not clear what the correct result is.

    It loops through the divers (first index) once, so I guess we want to sort the divers for each judge (second index) independent of the other judges. OK.

    But that means it's really doing a sort per diver. But it only does one loop within the diver loop. So it's only doing one pass of the bubble sort per diver.

    There should be a loop between the two. The inner loop should be repeated until there are no swaps.

    And nonoob is right, the inner loop is accessing data outside the array bounds.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It is logically ambiguous how to sort a 2D array. Without a definition of what sorted means for two-dimensional data, you can't begin to sort it.
    Work out what results you're actually trying to achieve and give an example.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort for array of double
    By bvkim in forum C Programming
    Replies: 1
    Last Post: 05-10-2010, 01:04 AM
  2. bubble sort with an array
    By commandercool in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2005, 01:29 AM
  3. Array/Structure Bubble Sort
    By JKI in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2003, 11:59 AM
  4. bubble sort array of strings
    By the_head in forum C Programming
    Replies: 1
    Last Post: 04-02-2002, 05:15 PM