Select Sorting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jackly84
    New Member
    • Mar 2010
    • 16

    Select Sorting

    i have a Select Sorting i having trouble With..
    Code:
    template <class dType>
    void selectSort( int x[], int n)
    {
        int smallPos, smallest;
        for (int i=0; i <n; i++)
        {
            smallPos = i;
            smallest = x[ smallPos];
            
            for (int j=i +1; j < n ;j ++)
                
                if (x[j] < smallest)
                {
                    smallPos = j;
                    smallest = x [ smallPos];
                    
                }
            
        }
    }
    Inside my Main & inside my Global
    Code:
    void selectSort( int x[], int n);
    Code:
    else if ( choice == 2)
            
            {
                selectSort(x, n);
                for( int i=0; i < n; i++)
                    cout << "Select Sorting is Done" << n[i] << endl; 
            }
    the problem im having is i want to be able to sort my array but inside the main, its saying Semantic Issue, use of undeclared identifier 'X' and its saying Subscripted value is not an array,pointer or vector for
    Code:
    cout << "Select Sorting is Done" << n[i]
    I'm using a Struct for the array.
    and i have
    Code:
     student record[N_STUDENT];
        int n;
        int i;
        
        for (int n = 0; n < N_STUDENT;n++)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Where do you define that x is an array of students ? Or did you mean to sort your records array instead?

    Comment

    • jackly84
      New Member
      • Mar 2010
      • 16

      #3
      I want to Sort the Student Records, by Student ID. get them in Order.

      Code:
       cout << " Enter Student Number: ";
              cin >> record[n].student_number;
      so if i enter in 292, 2032, 200, 190, etc.. it comes out like 190,200,292,303 2 etc.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        So are you proposing to write your own sort or to use one that comes with C++?

        Comment

        • jackly84
          New Member
          • Mar 2010
          • 16

          #5
          Hoping to get the above select sort working but with no joy

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You might try a bubble sort.

            The code for that s a Google exercise.

            The gist is that you start with element 0 of your array and compare to subsequet elements until you find one that is less. Then you swap element 0 with that element. And continue to the end of the arry swapping as necessary. When you get to the end the lowest element is in positiion 0. Then just repat this whole mess starting with element 1 then element 2, etc.

            Comment

            • jackly84
              New Member
              • Mar 2010
              • 16

              #7
              this is my Array
              Code:
              struct student
              {
              	int student_number;              // Student number of student
                  string studentname;             // Name of student
              	string Address;                //Address of Studnet
                  int CourseCode;               //Course Code 
                  string CourseName;           // Name of Course 
              };
              
              
              /*****************************************************************************/
              
              int main ()
              {
                  
                  student record[N_STUDENT];
                  int n;
                  int i;
                  
                  for (int n = 0; n < N_STUDENT;n++)
                  {
                      cout << " Enter Student Number: ";
                      cin >> record[n].student_number;
              
                      while (!(cin >> i))
                      {
                          cout << " Error _ enter a number ? ";
                          cin.clear();             // clear error condition
                          cin.ignore(1000,'\n');   // remove faulty characters
                      }
                      cout << "Enter Student  Name: ";
                      cin >> record[n].studentname;
                      
                      cout << "Enter Student Address: ";
                      cin >> record[n].Address;
                      
                      cout << " Enter Course Code: ";
                      cin >> record[n].CourseCode;
              Should i replace x in the Select Sort with N_Students? for this Select Sort to work ?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Code:
                void selectSort( int x[], int n) 
                {
                etc...
                is a function. It has two arguments: a) an array of int named x and b) an int named n

                That means you call this function using the address of an int and another int for the number of array elements:

                Code:
                int array[100];
                int len = 100;
                etc...
                selectSort( array, len);
                Remember, the calling function is using array and len but the function sees these as x and n.

                Therefore selectSort will not work with an array of student. For that you need a selectSort expecting this:

                Code:
                void selectSort( studentt x[], int n)
                {
                etc...
                Code:
                From your code:
                template <class dType> 
                void selectSort( int x[], int n) 
                { 
                etc...
                I suspect you want the array to be an array of dType. LIke this:

                Code:
                template <class dType> 
                void selectSort( dType x[], int n) 
                { 
                etc...
                See if this gets you any further.

                Comment

                • jackly84
                  New Member
                  • Mar 2010
                  • 16

                  #9
                  ok
                  Code:
                      template <class dType> 
                      void selectSort( dType x[], int n) 
                      { 
                      etc...
                  is giving me no errors
                  when i did
                  Code:
                  else if ( choice == 2)
                          {
                              selectSort( x ,n)
                              for( int i=0; i < n; i++)
                                  cout << "Select Sorting is Done" << n[i] << endl; 
                          }
                          else if ( choice == 1)
                          for (int n = 0; n < N_STUDENT;n++)
                          {
                              cout << "Student Number: " << record[n].student_number << endl;
                              cout << "Student  Name: " << record[n].studentname << endl;
                              cout << "Student Address: " << record[n].Address << endl;
                              cout << "Course Code: " << record[n].CourseCode << endl;
                              cout << "Course Name: " << record[n].CourseName << endl;
                             
                          }
                          return 0;
                  use of undeclared identifier 'X'
                  if i put selectSort( student ,n)
                  it gives me Student does not refer to a value
                  if i place int X; inside main.
                  No matching function for call to 'SelectSort'

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    You have a problem here:

                    Code:
                    template <class dType> 
                    void selectSort( dType x[], int n) 
                    { 
                        int smallPos, smallest; 
                        for (int i=0; i <n; i++) 
                        { 
                            smallPos = i;   
                            smallest = x[ smallPos]; <-------!!!
                      
                            for (int j=i +1; j < n ;j ++) 
                      
                                if (x[j] < smallest) 
                                { 
                                    smallPos = j; 
                                    smallest = x [ smallPos]; 
                      
                                } 
                      
                        } 
                    }
                    smallest is an int. But x[smallPos] is a dType. In this case a student. You cannot assign a student to n int.

                    That means thar smallest also hase to be a dType.

                    Once smallest becomes a dType, then this will fail:

                    Code:
                    if (x[j] < smallest) 
                    {
                    etc...
                    because there is no function that can compare two dType using a < operator. In this case tou need to write an operator< for the student:

                    Code:
                    bool operator<(student& rhs, student&lhs)
                    {
                         return true if rhs is less then lhs;
                    }
                    I leave the guts of this function to you.

                    Remember, when you use a template the compiler makes a copy of the template and changes all of the dType to student and that resulting function s the one that is called and is the one that must compile.

                    Once you get this fixed you can call the template function this way:

                    Code:
                    selectSort(record, N_STUDENT);

                    Comment

                    Working...