C++ homework assistance bubble and selection sort with structures

This is a discussion on C++ homework assistance bubble and selection sort with structures within the C++ Programming forums, part of the General Programming Boards category; My lab assignment is to sort an array of structures that contains employee data and sort them using bubble and ...

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

    C++ homework assistance bubble and selection sort with structures

    My lab assignment is to sort an array of structures that contains employee data and sort them using bubble and selection sort functions. It writes the data to an output file. The code that i wrote only sorts the employee ID numbers, the name and city of the employee remain in the same order. My lab instructor said that the name and city have to be joined with the ID number, but i dont have a clue as to how to do that. I have made many attempts to try to get this to work but i receive errors such as "invalid conversion from char to const char when i try to strcat the name and city to the ID variable. Any assistance would be helpful.

    Code:
     
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<cstring>
    using namespace std;
    
    // Needed for sufficient size for arrays
    const int Name_Length = 15;
    const int City_Length = 15;
    // Structure declared to store employee data
     struct EmployeeInfo
      {
           int EmployeeID;//will store employee's IDs
           char EmployeeName[Name_Length];// will store employee's names
           char City[City_Length];// will store employee's city
       };
     void OpenOutputFile (ofstream&, char[]);
     void PrintHeading (ofstream&);
     void showArray(ofstream&,EmployeeInfo [], int);// displays original unsorted data
     void BubbleSortArray(EmployeeInfo [], int);// bubblesorts the data
     void selectionSortArray(EmployeeInfo [], int);//selectionsorts the data
    
    int main()
    {
    
        char variable;
        const int Num_Workers = 50;
           //Initialized the employee structure with employee data
          EmployeeInfo Employees[Num_Workers] = {
                                                {735, "Ann J.Weeks" ,"Corpus Christi"},
                                                {341, "Tom Wonder" ,"Denver"},
                                                {646, "Mat Gonzales", "Ft Worth"},
                                                {229, "Art Smith", "Mustang Mott"},
                                                {842, "Ed Moreno", "Durango"},
                                                {620, "I.M.Blimpee", "Beeville"},
                                                {741, "Ed White", "Red River"},
                                                {222, "May Thomas", "Ft Worth"},
                                                {165, "Joe Bell", "Newark"},
                                                {182, "Alice Beach", "San Jose"},
                                                {943, "Tom Thumb", "Dallas"},
                                                {150, "Nan Newman", "Victoria"},
                                                {250, "Dan Duran", "Corsicana"},
                                                {350, "Michael Nyu", "Las Vegas"},
                                                {228, "Al DeLeon", "Brownsville"},
                                                {344, "Ted Tildon", "Miami"},
                                                {828, "M.J.Barns", "Cleveland"},
                                                {110, "Dick Reyes", "New York"},
                                                {987, "Ralph Dietz", "Cuero"},
                                                {777, "Ann Hatter", "Big Springs"},
                                                {191, "M.K.Moore", "Atlanta"},
                                                {545, "Mack Foyt", "Daytona"},
                                                {878, "Kim Mills ", "Austin"},
                                                {900, "Roy Shaw", "Paradise"},
                                                {351, "Herb Rilley", "San Francisco"},
                                                {291, "Ann Woorley", "Seattle"},
                                                {854, "T.N.Teal","Baltimore"},
                                                {404, "Sean New", "Lubbock"},
                                                {607, "Tom Russell", "Salt Lake City"},
                                                {305, "Bill Black", "Jackson"},
                                                {199, "Sue Shuman", "Chicago"},
                                                {395, "Nan Newman", "Bullhead City"},
                                                {809, "Avis Hall", "Helena"},
                                                {504, "Tammera Tool", "South Bend"},
                                                {841, "Nancy Gomez", "Miami"},
                                                {149, "Mel Dale", "Oshkosh"},
                                                {492, "Nell Morgan", "Atlanta"},
                                                {613, "Dan Morales", "Jackson"},
                                                {386, "Sue Bush", "Evansville"},
                                                {929, "Keith Aaron", "LayFayette"},
                                                {481, "Tom Herron", "Denver"},
                                                {853, "Paul Driver", "Chicago"},
                                                {729, "Gayle Turner", "San Jose"},
                                                {205, "A.D.Daniel", "Beeville"},
                                                {482, "Irma Perez", "Charlotte"},
                                                {774, "Don Guzmann", "New Braunfels"},
                                                {338, "Mac Deaver", "Plano"},
                                                {194, "Hope Allen", "Carbondale"},
                                                {743, "Raul Pena", "Cleveland"},
                                                {108, "Merri Clark", "Sugarland"}
                                              };
         EmployeeInfo Employees2[Num_Workers] = {
                                                {735, "Ann J.Weeks" ,"Corpus Christi"},
                                                {341, "Tom Wonder" ,"Denver"},
                                                {646, "Mat Gonzales", "Ft Worth"},
                                                {229, "Art Smith", "Mustang Mott"},
                                                {842, "Ed Moreno", "Durango"},
                                                {620, "I.M.Blimpee", "Beeville"},
                                                {741, "Ed White", "Red River"},
                                                {222, "May Thomas", "Ft Worth"},
                                                {165, "Joe Bell", "Newark"},
                                                {182, "Alice Beach", "San Jose"},
                                                {943, "Tom Thumb", "Dallas"},
                                                {150, "Nan Newman", "Victoria"},
                                                {250, "Dan Duran", "Corsicana"},
                                                {350, "Michael Nyu", "Las Vegas"},
                                                {228, "Al DeLeon", "Brownsville"},
                                                {344, "Ted Tildon", "Miami"},
                                                {828, "M.J.Barns", "Cleveland"},
                                                {110, "Dick Reyes", "New York"},
                                                {987, "Ralph Dietz", "Cuero"},
                                                {777, "Ann Hatter", "Big Springs"},
                                                {191, "M.K.Moore", "Atlanta"},
                                                {545, "Mack Foyt", "Daytona"},
                                                {878, "Kim Mills ", "Austin"},
                                                {900, "Roy Shaw", "Paradise"},
                                                {351, "Herb Rilley", "San Francisco"},
                                                {291, "Ann Woorley", "Seattle"},
                                                {854, "T.N.Teal","Baltimore"},
                                                {404, "Sean New", "Lubbock"},
                                                {607, "Tom Russell", "Salt Lake City"},
                                                {305, "Bill Black", "Jackson"},
                                                {199, "Sue Shuman", "Chicago"},
                                                {395, "Nan Newman", "Bullhead City"},
                                                {809, "Avis Hall", "Helena"},
                                                {504, "Tammera Tool", "South Bend"},
                                                {841, "Nancy Gomez", "Miami"},
                                                {149, "Mel Dale", "Oshkosh"},
                                                {492, "Nell Morgan", "Atlanta"},
                                                {613, "Dan Morales", "Jackson"},
                                                {386, "Sue Bush", "Evansville"},
                                                {929, "Keith Aaron", "LayFayette"},
                                                {481, "Tom Herron", "Denver"},
                                                {853, "Paul Driver", "Chicago"},
                                                {729, "Gayle Turner", "San Jose"},
                                                {205, "A.D.Daniel", "Beeville"},
                                                {482, "Irma Perez", "Charlotte"},
                                                {774, "Don Guzmann", "New Braunfels"},
                                                {338, "Mac Deaver", "Plano"},
                                                {194, "Hope Allen", "Carbondale"},
                                                {743, "Raul Pena", "Cleveland"},
                                                {108, "Merri Clark", "Sugarland"}
                                              };
    
    
    
    
    //Output file stream
      ofstream dataFileOut;
     //Input file name
    
    //Output file name
       char outputFile[80] = "RSLab06.out";
    
     OpenOutputFile(dataFileOut, outputFile);
    
     PrintHeading(dataFileOut);
    
     showArray(dataFileOut,Employees, Num_Workers);//displays original unsorted data
    
     BubbleSortArray(Employees, Num_Workers);// bubblesorts the data
    
     dataFileOut << "\nData after Bubble sort----------------------------" << endl;
    
     showArray(dataFileOut,Employees, Num_Workers);//displays sorted data
    
     dataFileOut << "\nData after Selection sort--------------------------" << endl;
    
     selectionSortArray(Employees2, Num_Workers);
    
     showArray(dataFileOut,Employees2, Num_Workers);//displays sorted data
    
    
     system("pause");
    return 0;
    }
    //Function definition of showArray
    void showArray(ofstream& dataFileOut,EmployeeInfo array[], int size)
    {
         for (int count = 0; count < size; count++)
         {
             dataFileOut << array[count].EmployeeID << " ";
             dataFileOut << "\t\t" << array[count].EmployeeName << setw(2) << " ";
             dataFileOut << "\t\t" << array[count].City << " ";
             dataFileOut << endl;
             }
             }
    //Function definition of the bubble sort function
    void BubbleSortArray(EmployeeInfo array[], int size)
     {
         bool swap;
         int temp;
         
         do
         {
                  swap = false;
                  for (int count = 0; count < (size-1); count++)
                  {
                      // Determines the condition if the data on the left is
                      // greater than the right, it will swap all the way through
                      // until the data is sorted. This is done for the ID, Name,
                      // and the city of the employees.
    
                      if (array[count].EmployeeID > array[count+1].EmployeeID)
                       {
                      temp = array[count].EmployeeID;//stores data into a temp variable
    
                      array[count].EmployeeID = array[count+1].EmployeeID;// assigns
                      // the data of the right element into the left element
    
                      array[count+1].EmployeeID = temp;// assigns the copied data
                      // that was stored into the temp variable into the right.
    
                      swap = true;
                         }
    					}
    					
                         }while (swap);
                         }
    // Function definition of the selection sort function
    void selectionSortArray(EmployeeInfo array[], int size)
    {
    	  int startScan, minIndex, minValue;
    
    	  for(startScan = 0; startScan < (size-1); startScan++)
    	  {
    			minIndex = startScan;
    			minValue = array[startScan].EmployeeID;
    			for( int index = startScan + 1; index < size; index++)
    			{
    				  if (array[index].EmployeeID < minValue)
    				  {
    					  minValue = array[index].EmployeeID;
    					  minIndex = index;
    					  }
    					  }
    					  array[minIndex].EmployeeID = array[startScan].EmployeeID;
    					  array[startScan].EmployeeID = minValue;
    					  }
    					  }
    
    
    
    void OpenOutputFile (ofstream& dataFileOut, char outputFile[])
    {
    //Open output file
       dataFileOut.open(outputFile);
    
    //If not successful open, display message and exit with error
       if (!dataFileOut)
      {
          cout << "\n\n\n\t\t\t Error opening file!"
          << outputFile << endl;
          exit(1);
       }
    } //End OpenOutputFile
    
    void PrintHeading(ofstream& dataFileOut)
    {
           dataFileOut <<  "Employee ID|" << "Employee Name|\t" << " City|"
           << endl;
    }

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    First things first, the layout of that code is appalling. I suggest writing it more like this:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstring>
    using namespace std;
    
    // Needed for sufficient size for arrays
    const int Name_Length = 15;
    const int City_Length = 15;
    
    // Structure declared to store employee data
    struct EmployeeInfo
    {
        int EmployeeID; // will store employee's IDs
        char EmployeeName[Name_Length]; // will store employee's names
        char City[City_Length]; // will store employee's city
    };
    
    void OpenOutputFile(ofstream&, char[]);
    void PrintHeading(ofstream&);
    void showArray(ofstream&, EmployeeInfo[], int); // displays original unsorted data
    void BubbleSortArray(EmployeeInfo[], int); // bubblesorts the data
    void selectionSortArray(EmployeeInfo[], int); // selectionsorts the data
    
    int main()
    {
        char variable;
        const int Num_Workers = 50;
        
        // Initialized the employee structure with employee data
        EmployeeInfo Employees[Num_Workers] = {
            {735, "Ann J.Weeks" ,"Corpus Christi"},
            {341, "Tom Wonder" ,"Denver"},
            {646, "Mat Gonzales", "Ft Worth"},
            {229, "Art Smith", "Mustang Mott"},
            {842, "Ed Moreno", "Durango"},
            {620, "I.M.Blimpee", "Beeville"},
            {741, "Ed White", "Red River"},
            {222, "May Thomas", "Ft Worth"},
            {165, "Joe Bell", "Newark"},
            {182, "Alice Beach", "San Jose"},
            {943, "Tom Thumb", "Dallas"},
            {150, "Nan Newman", "Victoria"},
            {250, "Dan Duran", "Corsicana"},
            {350, "Michael Nyu", "Las Vegas"},
            {228, "Al DeLeon", "Brownsville"},
            {344, "Ted Tildon", "Miami"},
            {828, "M.J.Barns", "Cleveland"},
            {110, "Dick Reyes", "New York"},
            {987, "Ralph Dietz", "Cuero"},
            {777, "Ann Hatter", "Big Springs"},
            {191, "M.K.Moore", "Atlanta"},
            {545, "Mack Foyt", "Daytona"},
            {878, "Kim Mills ", "Austin"},
            {900, "Roy Shaw", "Paradise"},
            {351, "Herb Rilley", "San Francisco"},
            {291, "Ann Woorley", "Seattle"},
            {854, "T.N.Teal","Baltimore"},
            {404, "Sean New", "Lubbock"},
            {607, "Tom Russell", "Salt Lake City"},
            {305, "Bill Black", "Jackson"},
            {199, "Sue Shuman", "Chicago"},
            {395, "Nan Newman", "Bullhead City"},
            {809, "Avis Hall", "Helena"},
            {504, "Tammera Tool", "South Bend"},
            {841, "Nancy Gomez", "Miami"},
            {149, "Mel Dale", "Oshkosh"},
            {492, "Nell Morgan", "Atlanta"},
            {613, "Dan Morales", "Jackson"},
            {386, "Sue Bush", "Evansville"},
            {929, "Keith Aaron", "LayFayette"},
            {481, "Tom Herron", "Denver"},
            {853, "Paul Driver", "Chicago"},
            {729, "Gayle Turner", "San Jose"},
            {205, "A.D.Daniel", "Beeville"},
            {482, "Irma Perez", "Charlotte"},
            {774, "Don Guzmann", "New Braunfels"},
            {338, "Mac Deaver", "Plano"},
            {194, "Hope Allen", "Carbondale"},
            {743, "Raul Pena", "Cleveland"},
            {108, "Merri Clark", "Sugarland"}};
        
        EmployeeInfo Employees2[Num_Workers] = {
            {735, "Ann J.Weeks" ,"Corpus Christi"},
            {341, "Tom Wonder" ,"Denver"},
            {646, "Mat Gonzales", "Ft Worth"},
            {229, "Art Smith", "Mustang Mott"},
            {842, "Ed Moreno", "Durango"},
            {620, "I.M.Blimpee", "Beeville"},
            {741, "Ed White", "Red River"},
            {222, "May Thomas", "Ft Worth"},
            {165, "Joe Bell", "Newark"},
            {182, "Alice Beach", "San Jose"},
            {943, "Tom Thumb", "Dallas"},
            {150, "Nan Newman", "Victoria"},
            {250, "Dan Duran", "Corsicana"},
            {350, "Michael Nyu", "Las Vegas"},
            {228, "Al DeLeon", "Brownsville"},
            {344, "Ted Tildon", "Miami"},
            {828, "M.J.Barns", "Cleveland"},
            {110, "Dick Reyes", "New York"},
            {987, "Ralph Dietz", "Cuero"},
            {777, "Ann Hatter", "Big Springs"},
            {191, "M.K.Moore", "Atlanta"},
            {545, "Mack Foyt", "Daytona"},
            {878, "Kim Mills ", "Austin"},
            {900, "Roy Shaw", "Paradise"},
            {351, "Herb Rilley", "San Francisco"},
            {291, "Ann Woorley", "Seattle"},
            {854, "T.N.Teal","Baltimore"},
            {404, "Sean New", "Lubbock"},
            {607, "Tom Russell", "Salt Lake City"},
            {305, "Bill Black", "Jackson"},
            {199, "Sue Shuman", "Chicago"},
            {395, "Nan Newman", "Bullhead City"},
            {809, "Avis Hall", "Helena"},
            {504, "Tammera Tool", "South Bend"},
            {841, "Nancy Gomez", "Miami"},
            {149, "Mel Dale", "Oshkosh"},
            {492, "Nell Morgan", "Atlanta"},
            {613, "Dan Morales", "Jackson"},
            {386, "Sue Bush", "Evansville"},
            {929, "Keith Aaron", "LayFayette"},
            {481, "Tom Herron", "Denver"},
            {853, "Paul Driver", "Chicago"},
            {729, "Gayle Turner", "San Jose"},
            {205, "A.D.Daniel", "Beeville"},
            {482, "Irma Perez", "Charlotte"},
            {774, "Don Guzmann", "New Braunfels"},
            {338, "Mac Deaver", "Plano"},
            {194, "Hope Allen", "Carbondale"},
            {743, "Raul Pena", "Cleveland"},
            {108, "Merri Clark", "Sugarland"}};
        
        // Output file stream
        ofstream dataFileOut;
        
        // Input file name
        
        // Output file name
        char outputFile[80] = "RSLab06.out";
        
        OpenOutputFile(dataFileOut, outputFile);
        
        PrintHeading(dataFileOut);
        
        showArray(dataFileOut, Employees, Num_Workers); // displays original unsorted data
        
        BubbleSortArray(Employees, Num_Workers); // bubblesorts the data
        
        dataFileOut << "\nData after Bubble sort----------------------------" << endl;
        
        showArray(dataFileOut, Employees, Num_Workers); // displays sorted data
        
        dataFileOut << "\nData after Selection sort--------------------------" << endl;
        
        selectionSortArray(Employees2, Num_Workers);
        
        showArray(dataFileOut,Employees2, Num_Workers); // displays sorted data
        
        //system("pause");
        
        return 0;
    }
    
    // Function definition of showArray
    void showArray(ofstream& dataFileOut,EmployeeInfo array[], int size)
    {
        for (int count = 0; count < size; count++)
        {
            dataFileOut << array[count].EmployeeID << " ";
            dataFileOut << "\t\t" << array[count].EmployeeName << setw(2) << " ";
            dataFileOut << "\t\t" << array[count].City << " ";
            dataFileOut << endl;
        }
    }
    
    // Function definition of the bubble sort function
    void BubbleSortArray(EmployeeInfo array[], int size)
    {
        bool swap;
        int temp;
        
        do
        {
            swap = false;
            for (int count = 0; count < (size-1); count++)
            {
                // Determines the condition if the data on the left is
                // greater than the right, it will swap all the way through
                // until the data is sorted. This is done for the ID, Name,
                // and the city of the employees.
                
                if (array[count].EmployeeID > array[count+1].EmployeeID)
                {
                    temp = array[count].EmployeeID; // stores data into a temp variable
                    
                    array[count].EmployeeID = array[count+1].EmployeeID;// assigns
                    // the data of the right element into the left element
                    
                    array[count+1].EmployeeID = temp;// assigns the copied data
                    // that was stored into the temp variable into the right.
                    
                    swap = true;
                }
            }
        }
        while (swap);
    }
    
    // Function definition of the selection sort function
    void selectionSortArray(EmployeeInfo array[], int size)
    {
        int startScan, minIndex, minValue;
        
        for(startScan = 0; startScan < (size-1); startScan++)
        {
            minIndex = startScan;
            minValue = array[startScan].EmployeeID;
            for (int index = startScan + 1; index < size; index++)
            {
                if (array[index].EmployeeID < minValue)
                {
                    minValue = array[index].EmployeeID;
                    minIndex = index;
                }
            }
            
            array[minIndex].EmployeeID = array[startScan].EmployeeID;
            array[startScan].EmployeeID = minValue;
        }
    }
    
    void OpenOutputFile(ofstream& dataFileOut, char outputFile[])
    {
        // Open output file
        dataFileOut.open(outputFile);
        
        // If not successful open, display message and exit with error
        if (!dataFileOut)
        {
            cout << "\n\n\n\t\t\t Error opening file!" << outputFile << endl;
            exit(1);
        }
    }
    
    void PrintHeading(ofstream& dataFileOut)
    {
        dataFileOut <<  "Employee ID|" << "Employee Name|\t" << " City|" << endl;
    }
    Now, as to the matter of comparing names and cities.

    My lab instructor said that the name and city have to be joined with the ID number
    I hope this lab instructor is just another student and not a professor. That is not an appropriate way to sort with two fields. It doesn't seem you've made it clear exactly how the employees should be sorted, if it's ID, then name, then city, that's the same as just sorting by ID (since you'll never have two equal IDs). Let me give you an example though. First of all, make a function to handle the comparison:

    Code:
    bool CompareEmployees(const EmployeeInfo& a, const EmployeeInfo& b)
    {
        ...
    }
    The function should return true iff a < b. For this example, we'll sort by city, then by name. To do this, first compare cities, if a's city comes before b's city, return true, if a's city comes after b's, return false, otherwise the cities are equal, so compare the names - if a's name comes before b's name, return true, otherwise return false.

    i receive errors such as invalid conversion from char to const char when i try to strcat the name and city to the ID variable
    Like I said, you shouldn't have to use strcat, but if you give an example of the actual code that produces this error, I should be able to help you.
    Last edited by Mozza314; 04-02-2011 at 09:48 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Quote Originally Posted by Mozza314 View Post
    First things first, the layout of that code is appalling. I suggest writing it more like this:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstring>
    using namespace std;
    
    // Needed for sufficient size for arrays
    const int Name_Length = 15;
    const int City_Length = 15;
    
    // Structure declared to store employee data
    struct EmployeeInfo
    {
        int EmployeeID; // will store employee's IDs
        char EmployeeName[Name_Length]; // will store employee's names
        char City[City_Length]; // will store employee's city
    };
    
    void OpenOutputFile(ofstream&, char[]);
    void PrintHeading(ofstream&);
    void showArray(ofstream&, EmployeeInfo[], int); // displays original unsorted data
    void BubbleSortArray(EmployeeInfo[], int); // bubblesorts the data
    void selectionSortArray(EmployeeInfo[], int); // selectionsorts the data
    
    int main()
    {
        char variable;
        const int Num_Workers = 50;
        
        // Initialized the employee structure with employee data
        EmployeeInfo Employees[Num_Workers] = {
            {735, "Ann J.Weeks" ,"Corpus Christi"},
            {341, "Tom Wonder" ,"Denver"},
            {646, "Mat Gonzales", "Ft Worth"},
            {229, "Art Smith", "Mustang Mott"},
            {842, "Ed Moreno", "Durango"},
            {620, "I.M.Blimpee", "Beeville"},
            {741, "Ed White", "Red River"},
            {222, "May Thomas", "Ft Worth"},
            {165, "Joe Bell", "Newark"},
            {182, "Alice Beach", "San Jose"},
            {943, "Tom Thumb", "Dallas"},
            {150, "Nan Newman", "Victoria"},
            {250, "Dan Duran", "Corsicana"},
            {350, "Michael Nyu", "Las Vegas"},
            {228, "Al DeLeon", "Brownsville"},
            {344, "Ted Tildon", "Miami"},
            {828, "M.J.Barns", "Cleveland"},
            {110, "Dick Reyes", "New York"},
            {987, "Ralph Dietz", "Cuero"},
            {777, "Ann Hatter", "Big Springs"},
            {191, "M.K.Moore", "Atlanta"},
            {545, "Mack Foyt", "Daytona"},
            {878, "Kim Mills ", "Austin"},
            {900, "Roy Shaw", "Paradise"},
            {351, "Herb Rilley", "San Francisco"},
            {291, "Ann Woorley", "Seattle"},
            {854, "T.N.Teal","Baltimore"},
            {404, "Sean New", "Lubbock"},
            {607, "Tom Russell", "Salt Lake City"},
            {305, "Bill Black", "Jackson"},
            {199, "Sue Shuman", "Chicago"},
            {395, "Nan Newman", "Bullhead City"},
            {809, "Avis Hall", "Helena"},
            {504, "Tammera Tool", "South Bend"},
            {841, "Nancy Gomez", "Miami"},
            {149, "Mel Dale", "Oshkosh"},
            {492, "Nell Morgan", "Atlanta"},
            {613, "Dan Morales", "Jackson"},
            {386, "Sue Bush", "Evansville"},
            {929, "Keith Aaron", "LayFayette"},
            {481, "Tom Herron", "Denver"},
            {853, "Paul Driver", "Chicago"},
            {729, "Gayle Turner", "San Jose"},
            {205, "A.D.Daniel", "Beeville"},
            {482, "Irma Perez", "Charlotte"},
            {774, "Don Guzmann", "New Braunfels"},
            {338, "Mac Deaver", "Plano"},
            {194, "Hope Allen", "Carbondale"},
            {743, "Raul Pena", "Cleveland"},
            {108, "Merri Clark", "Sugarland"}};
        
        EmployeeInfo Employees2[Num_Workers] = {
            {735, "Ann J.Weeks" ,"Corpus Christi"},
            {341, "Tom Wonder" ,"Denver"},
            {646, "Mat Gonzales", "Ft Worth"},
            {229, "Art Smith", "Mustang Mott"},
            {842, "Ed Moreno", "Durango"},
            {620, "I.M.Blimpee", "Beeville"},
            {741, "Ed White", "Red River"},
            {222, "May Thomas", "Ft Worth"},
            {165, "Joe Bell", "Newark"},
            {182, "Alice Beach", "San Jose"},
            {943, "Tom Thumb", "Dallas"},
            {150, "Nan Newman", "Victoria"},
            {250, "Dan Duran", "Corsicana"},
            {350, "Michael Nyu", "Las Vegas"},
            {228, "Al DeLeon", "Brownsville"},
            {344, "Ted Tildon", "Miami"},
            {828, "M.J.Barns", "Cleveland"},
            {110, "Dick Reyes", "New York"},
            {987, "Ralph Dietz", "Cuero"},
            {777, "Ann Hatter", "Big Springs"},
            {191, "M.K.Moore", "Atlanta"},
            {545, "Mack Foyt", "Daytona"},
            {878, "Kim Mills ", "Austin"},
            {900, "Roy Shaw", "Paradise"},
            {351, "Herb Rilley", "San Francisco"},
            {291, "Ann Woorley", "Seattle"},
            {854, "T.N.Teal","Baltimore"},
            {404, "Sean New", "Lubbock"},
            {607, "Tom Russell", "Salt Lake City"},
            {305, "Bill Black", "Jackson"},
            {199, "Sue Shuman", "Chicago"},
            {395, "Nan Newman", "Bullhead City"},
            {809, "Avis Hall", "Helena"},
            {504, "Tammera Tool", "South Bend"},
            {841, "Nancy Gomez", "Miami"},
            {149, "Mel Dale", "Oshkosh"},
            {492, "Nell Morgan", "Atlanta"},
            {613, "Dan Morales", "Jackson"},
            {386, "Sue Bush", "Evansville"},
            {929, "Keith Aaron", "LayFayette"},
            {481, "Tom Herron", "Denver"},
            {853, "Paul Driver", "Chicago"},
            {729, "Gayle Turner", "San Jose"},
            {205, "A.D.Daniel", "Beeville"},
            {482, "Irma Perez", "Charlotte"},
            {774, "Don Guzmann", "New Braunfels"},
            {338, "Mac Deaver", "Plano"},
            {194, "Hope Allen", "Carbondale"},
            {743, "Raul Pena", "Cleveland"},
            {108, "Merri Clark", "Sugarland"}};
        
        // Output file stream
        ofstream dataFileOut;
        
        // Input file name
        
        // Output file name
        char outputFile[80] = "RSLab06.out";
        
        OpenOutputFile(dataFileOut, outputFile);
        
        PrintHeading(dataFileOut);
        
        showArray(dataFileOut, Employees, Num_Workers); // displays original unsorted data
        
        BubbleSortArray(Employees, Num_Workers); // bubblesorts the data
        
        dataFileOut << "\nData after Bubble sort----------------------------" << endl;
        
        showArray(dataFileOut, Employees, Num_Workers); // displays sorted data
        
        dataFileOut << "\nData after Selection sort--------------------------" << endl;
        
        selectionSortArray(Employees2, Num_Workers);
        
        showArray(dataFileOut,Employees2, Num_Workers); // displays sorted data
        
        //system("pause");
        
        return 0;
    }
    
    // Function definition of showArray
    void showArray(ofstream& dataFileOut,EmployeeInfo array[], int size)
    {
        for (int count = 0; count < size; count++)
        {
            dataFileOut << array[count].EmployeeID << " ";
            dataFileOut << "\t\t" << array[count].EmployeeName << setw(2) << " ";
            dataFileOut << "\t\t" << array[count].City << " ";
            dataFileOut << endl;
        }
    }
    
    // Function definition of the bubble sort function
    void BubbleSortArray(EmployeeInfo array[], int size)
    {
        bool swap;
        int temp;
        
        do
        {
            swap = false;
            for (int count = 0; count < (size-1); count++)
            {
                // Determines the condition if the data on the left is
                // greater than the right, it will swap all the way through
                // until the data is sorted. This is done for the ID, Name,
                // and the city of the employees.
                
                if (array[count].EmployeeID > array[count+1].EmployeeID)
                {
                    temp = array[count].EmployeeID; // stores data into a temp variable
                    
                    array[count].EmployeeID = array[count+1].EmployeeID;// assigns
                    // the data of the right element into the left element
                    
                    array[count+1].EmployeeID = temp;// assigns the copied data
                    // that was stored into the temp variable into the right.
                    
                    swap = true;
                }
            }
        }
        while (swap);
    }
    
    // Function definition of the selection sort function
    void selectionSortArray(EmployeeInfo array[], int size)
    {
        int startScan, minIndex, minValue;
        
        for(startScan = 0; startScan < (size-1); startScan++)
        {
            minIndex = startScan;
            minValue = array[startScan].EmployeeID;
            for (int index = startScan + 1; index < size; index++)
            {
                if (array[index].EmployeeID < minValue)
                {
                    minValue = array[index].EmployeeID;
                    minIndex = index;
                }
            }
            
            array[minIndex].EmployeeID = array[startScan].EmployeeID;
            array[startScan].EmployeeID = minValue;
        }
    }
    
    void OpenOutputFile(ofstream& dataFileOut, char outputFile[])
    {
        // Open output file
        dataFileOut.open(outputFile);
        
        // If not successful open, display message and exit with error
        if (!dataFileOut)
        {
            cout << "\n\n\n\t\t\t Error opening file!" << outputFile << endl;
            exit(1);
        }
    }
    
    void PrintHeading(ofstream& dataFileOut)
    {
        dataFileOut <<  "Employee ID|" << "Employee Name|\t" << " City|" << endl;
    }
    Now, as to the matter of comparing names and cities.



    I hope this lab instructor is just another student and not a professor. That is not an appropriate way to sort with two fields. It doesn't seem you've made it clear exactly how the employees should be sorted, if it's ID, then name, then city, that's the same as just sorting by ID (since you'll never have two equal IDs). Let me give you an example though. First of all, make a function to handle the comparison:

    Code:
    bool CompareEmployees(const EmployeeInfo& a, const EmployeeInfo& b)
    {
        ...
    }
    The function should return true iff a < b. For this example, we'll sort by city, then by name. To do this, first compare cities, if a's city comes before b's city, return true, if a's city comes after b's, return false, otherwise the cities are equal, so compare the names - if a's name comes before b's name, return true, otherwise return false.



    Like I said, you shouldn't have to use strcat, but if you give an example of the actual code that produces this error, I should be able to help you.
    yea the instructor is a student. The instructor stated that the ID is to be compared only and not the name or city. the first employee is {735, "Ann J.Weeks" ,"Corpus Christi"}, so when the bubble sort executes it will sort the employees in ascending order in which the first employee will now be {108, "Merri Clark", "Sugarland"}. Im confused as to how to make the name and city move together with the employee ID.

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by odaness View Post
    Im confused as to how to make the name and city move together with the employee ID.
    Just swap the entire EmployeeInfo structures. You can do something like this:

    Code:
    EmployeeInfo temp = employees[i];
    employees[i] = employees[j];
    employees[j] = temp;
    Better yet (if you're allowed) you can #include <algorithm> and use std::swap:

    Code:
    swap(employees[i], employees[j]);

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Is there a reason you are not using std::string instead of char?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    @Mozza314, we haven't been introduced to #include<algorithm> yet.
    @Elysia we are writing code with topics that have been covered so far and std::string has not been covered yet in our lesson plan.
    Thanks for your support though guys! I will try out your suggestions and see how it goes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 04:21 PM
  3. Bubble Sort / selection problem
    By Drew in forum C++ Programming
    Replies: 7
    Last Post: 08-26-2003, 03:23 PM
  4. selection sort?
    By dharh in forum C Programming
    Replies: 4
    Last Post: 03-17-2002, 01:23 PM

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