Thread: help with sort 2D array

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

    help with sort 2D array

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MAX 50
    #define MAX_COL 20
    #define SCORE 5
    int  getData(FILE* spData, char name[][MAX_COL], float difficulty[], float score[][SCORE]);
    void SelectionSort(FILE* spData, float score[][SCORE ], int);
    //int SelectionSort(FILE* spData, char name[][MAX_COL], float difficulty[], float score[][SCORE ], int);
    //int Caltotal()
    void printData(FILE* spData, char name[][MAX_COL], float difficulty[], float score[][SCORE], int);
    int main(void)
    
    {
     // variable declarations
        FILE*spData;
        char name[MAX][MAX_COL];
        float difficulty[MAX_COL];
        float score[MAX][SCORE];
        int numDiver = 0;
    	int last = MAX_COL;
     //statement
        if((spData=fopen("DIV1.txt","r"))== NULL)
         {
              printf("\n Error opennning file!\n");
              exit(100);
         }
        else
        {
            printf("opened file!\n");
        }
    
     numDiver = getData(spData,name, difficulty, score);
     SelectionSort(spData, score, last);
     printData(spData, name, difficulty, score, numDiver);
    
        fclose(spData);
        int i;
        scanf("%d", &i);
    
    return 0;
    }
    
    /*===============================getdata=====================================
    Pre: SpData is oppened,
    Post:
    */
    
    int getData(FILE* spData, char name[][MAX_COL], float difficulty[], float score[][SCORE])
    
    {
         int i=0;
         int j=0;
    
    
         while (i < MAX && (fscanf(spData,"%c", &name[i][0]) != EOF))
         {
    
    
    
    
                for(j = 1; j < MAX_COL; j++)
                {
                    fscanf(spData,"%c", &name[i][j]);
                }
    
                fscanf(spData,"%f", &difficulty[i]);
                  for(j = 0; j < SCORE; j++)
                      fscanf(spData,"%f\n", &score[i][j]);
    
                i++;
            }
    
        return i;
    
    }
    
    
    /*===============================BubbleSort=====================================
    Pre:
    Post:
    */
    void SelectionSort(FILE* spData, float score[][SCORE ], int last)
    {
    // Local Declerations
       float temp;
       int i;
       int j;
       int smallest;
       int walk;
    
    
    
       for(i = 0; i < MAX; i++)
    
       { printf("Processing row %d\n", i);
    
           for(j = 0; j < last; j++)
           {
               smallest = j;
    
               // inner loop
               for (walk = j+1; walk <= last; walk++)
               {printf("  - Comparing column %d\n", walk);
                   if (score[i][walk]< score[i][smallest])
                       smallest = walk;
                    // smallest selected
                       temp =  score[i][j];
                       score[i][j] = score[i][smallest];
                       score[i][smallest] = temp;
               }
           }
    
       }
    
    
    
       return;
    }
    
    
    
    /*===============================PrintData=====================================
    Pre:
    Post:
    */
    
    void printData(FILE* spData, char name[][MAX_COL], float difficulty[], float score[][SCORE], int numDiver)
    
    {
            int i;
            int j;
    
            for(i = 0; i < numDiver; i++)
            {
    
                for (j = 0; j< MAX_COL; j++)
                    printf("%c", name[i][j]);
    
                printf(" %1.1f", difficulty[i]);
    
    
                for (j = 0; j< SCORE; j++)
                    printf(" %1.1f", score[i][j]);
    
                printf("\n\n");
            }
    
        return;
    }
    can anyone help me to fix my sort function's definition> i wanted it to print out this

    KNIFE JACK 1.3 6.0 5.1 6.3 5.9 6.5
    WILLIAMSON FLIP A 1.4 3.5 4.1 4.7 7.2 3.8
    SOMMER TODD 1.2 8.0 9.1 8.1 9.3 9.0
    SWAN MIKE 1.1 4.3 2.1 9.9 6.2 7.0

    but in the sorted order from smallest to highest the name and the first column don't need to be sorted

    my sort function some how didn't process from the first column and also start process row at row 49

    which wasn't what it suppose to do
    Attached Files Attached Files
    Last edited by khoavo123; 02-07-2012 at 10:56 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first problem is where you're reading input from the file, and you assume that all names are the same length (when clearly they are not).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    well im not sorting the name array though and also every work fine if you take you sort function out
    also i dont know why the data is not in order when i post it on here
    Last edited by khoavo123; 02-07-2012 at 10:49 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you do this in main
    Code:
     numDiver = getData(spData,name, difficulty, score);
    ///!!! SelectionSort(spData, score, last);
     printData(spData, name, difficulty, score, numDiver);
    that is, just read the data and then print the data, does it all look good to you?

    Attach your input file example as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-26-2010, 09:02 AM
  2. Replies: 4
    Last Post: 12-06-2009, 12:27 PM
  3. How to sort an array?
    By Gerti in forum C Programming
    Replies: 1
    Last Post: 11-26-2005, 05:12 AM
  4. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM
  5. how to sort a 2D array
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 05-06-2002, 05:49 AM