How to apply selection sort in a two-dimensional array

This is a discussion on How to apply selection sort in a two-dimensional array within the C++ Programming forums, part of the General Programming Boards category; hi, I am having a bit of a problem applying selection sort in a two -dimensional array.. trying to apply ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    33

    How to apply selection sort in a two-dimensional array

    hi,

    I am having a bit of a problem applying selection sort in a two -dimensional array..

    trying to apply selection sort only to array[i][4] (only on the fourth column..

    so if the array is so while not sorted.

    Code:
    100 80 90 3
    101 90 70 2
    102 78 60 4
    it should become
    Code:
    102 78 60 4
    100 80 90 3
    101 90 70 2
    Thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So when you run your sort, you'll need to swap all the columns in your array, not just the fourth one.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    yes exactly

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Unless you meant, sort the forth column. In that case I think the easiest thing to do is make a 1D array of ray[][4], sort it, then put it back in the values for that column.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    but the thing is the values of the other columns will have to change accordingly

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So swap all the columns in your array, not just the fourth. What is difficult about this?

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    I get an error?

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cmath>
    
    #define width 5
    
    void SelectionSort(double array[][5], int height);
    using namespace std;
    
    int main()
    {
        int height;
        int i,j,lab,midterm,final,number;
        float avglab,avgmid,avgfinal,totallab,totalmid,totalfinal;
    
        cout<<"Number of students: ";
        cin>>height;
    
        cout<<"\n";
    
        double array[height][5];
    
        for(i=0;i<height;i++)
        {
            cout<<"Please enter student number: ";
            cin>>number;
            array[i][0]=number;
        }
        cout<<"\n";
        cout<<"Please enter Lab scores.\n";
        for(i=0;i<height;i++)
        {
            cout<<"Student ";printf("%.0f",array[i][0]);cout<<": ";
            cin>>lab;
            array[i][1]=lab;
    
        }
        cout<<"\n";
        cout<<"Please enter Midterm scores.\n";
        for(i=0;i<height;i++)
        {
            cout<<"Student ";printf("%.0f",array[i][0]);cout<<": ";
            cin>>midterm;
            array[i][2]=midterm;
    
        }
        cout<<"\n";
        cout<<"Please enter Final scores.\n";
        for(i=0;i<height;i++)
        {
            cout<<"Student ";printf("%.0f",array[i][0]);cout<<": ";
            cin>>final;
            array[i][3]=final;
    
        }
        cout<<"\n";
        for(i=0;i<height;i++)
        {
            totallab+=array[i][1];
            totalmid+=array[i][2];
            totalfinal+=array[i][3];
        }
        avglab=totallab/height;
        avgmid=totalmid/height;
        avgfinal=totalfinal/height;
        for(i=0;i<height;i++)
        {
            array[i][4]=array[i][1]*0.35+array[i][2]*0.25+array[i][3]*0.40;
        }
        SelectionSort(array[i][5]);
        cout<<"Student-num     "<<"Labs   "<<"MidTerm  "<<"Final  "<<"C-Grade";
        cout<<"\n----------------------------------------------";
        for(i=0;i<height;i++)
        {
            cout<<"\n";
    
            for(j=0;j<width;j++)
            {
                printf("%.0f",array[i][j]);
                cout<<"\t";
            }
    
        }
        cout<<"\n";
        cout<<"\n\n";
        cout<<"Average of Labs: "<<avglab;
        cout<<"\nAverage of Midterms: "<<avgmid;
        cout<<"\nAverage of Finals: "<<avgfinal;
        cout<<"\n\n\n";
    
        for(i=0;i<height;i++)
        {
            if(50>array[i][4])
            {
            cout<<"Student ";printf("%.0f",array[i][0]);cout<<" is ........ed!!\n";
            }
        }
    
    
        return 0;
    
    
    }
    void SelectionSort(int array[][5], int height)
    {
        int i,j,min,temp;
        for(i=0;i<height-1;i++)
        {
            min=i;
            for(j=i+1;j<height;j++)
            {
                if(array[j][5]<array[min][5])
                min=j;
            }
            temp=array[i][5];
            array[i][5]=array[min][5];
            array[min][5]=temp;
        }
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Array index [5] does not exist. Don't use it. You should be checking array index [4], and when you swap, you should swap [0], [1], [2], [3], and [4].

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    What does this mean ?
    error: cannot convert `double' to `double (*)[4]' for argument `1' to `void SelectionSort(double (*)[4], int)'|

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to pass an array, not a single solitary number.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    I thought i was doing that... how?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    By saying
    Code:
    selectionSort(array)
    instead of
    Code:
    selectionSort(array[i][5])

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    By saying
    Code:
    selectionSort(array)
    instead of
    Code:
    selectionSort(array[i][5])
    result:

    Code:
    |7|error: too few arguments to function `void SelectionSort(double (*)[5], int)'|
    |71|error: at this point in file|

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And you also need to pass the height along with.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    And you also need to pass the height along with.
    Code:
    SelectionSort(array, height);
    Code:
    |71|undefined reference to `SelectionSort(double (*) [5], int)'|

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-28-2009, 03:15 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21