1. Welcome to Tacoma World!

    You are currently viewing as a guest! To get full-access, you need to register for a FREE account.

    As a registered member, you’ll be able to:
    • Participate in all Tacoma discussion topics
    • Communicate privately with other Tacoma owners from around the world
    • Post your own photos in our Members Gallery
    • Access all special features of the site

Sort

Discussion in 'Technology' started by tarheelfan_08, Mar 1, 2010.

  1. Mar 5, 2010 at 8:53 AM
    #21
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    I got my code with a linear search and a selection sort to work! Now all I got to do is add my binary search and that should not be to hard! Here is my cod though guys!

    Code:
    #include "CarClass.h"
    using namespace std;
    
    //Set up Search 
    int search(Car object[], int size, int value)
    {
       int index = 0;         // Used as a subscript to search array 
       int position = -1;     // Used to record position of search value 
       bool found = false;    // Flag to indicate if the value was found 
    
       while (index < size && !found)
       {
          if (object[index].getVIN() == value) // If the value is found
          {
             found = true;          // Set the flag 
             position = index;      // Record the value's subscript 
          }
          index++;                  // Go to the next element 
       }
       return position;             // Return the position
    }// End search 
    
    
    int search(Car[], int, int);
    
    
    int main() {
            
        const int SIZE = 9;
                
        //Array of 9 cars
        Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990, 1237362727), 
                                Car("Ford", "Mustang", "Red", 2007, 49842, 7337372239),
                                Car("Chevrolet", "Beretta", "Black", 1989, 90332, 2873644922),
                                Car("Ford", "Focus", "White", 2008, 150, 9236498273),
                                Car("Voltzwagon", "Jetta", "Black", 2006, 28002, 4673992056),
                                Car("Rolls Royce", "Ghost", "Silver", 2005, 10000, 9292983855),
                                Car("Mazda", "626", "Blue", 2002, 84754, 7364646463),
                                Car("Toyota", "Camry", "Red", 2004, 50332, 2133737227),
                                Car("Bugatti", "Veyron 16.4", "White", 2010, 5, 5712893401)};
    
        
        long long int manualVIN= 2873644922;
        long long int desiredVIN;
        int posLin; // Used to search for VIN 2873644922
        int pos;  // Used for when user wil input the vin they want to search
        Car temp;
    
        //MANUAL LINEAR SEARCH FOR VIN NUMBER
        
            posLin = search(Car_array, SIZE, manualVIN); 
    
            if (posLin == -1)
                cout << "You do not have a car with the VIN Number 2873644922.\n";
            else 
            {
                cout << "You do have a car on the lot with the VIN Number 2873644922.\n";
            }
            cout << "_______________________________________________________\n";
        // END MANUAL LINEAR SEARCH FOR VIN NUMBER
    
        // Get the VIN to search for
    
         // LINEAR SEARCH FOR VIN NUMBER
            cout << "Enter a VIN number to search for: ";
            cin >> desiredVIN;
    
        // Linear Search for VIN Number
            pos = search(Car_array, SIZE, desiredVIN); 
    
            if (pos == -1)
                cout << "You do not have a car with the VIN Number " << desiredVIN << ".\n";
            else 
            {
                cout << "You do have a car on the lot with the VIN Number " << desiredVIN << ".\n";
            }
            cout << "_______________________________________________________\n";
    
         // END LINEAR SEARCH FOR VIN NUMBER
        
        // Loop to display car details
            for (int x=0; x<9; x++) {
                Car_array[x].car_details();
            }
    
            cout << "_______________________________________________________\n";
    
    
            //Selection Sort
    
            cout << " This is the Array of Cars in a selection sort!!\n\n";
    
            //temp = null;
    
    
            for (int i = 0; i < SIZE-1; i++)
                    {
                        for (int k = i+1; k < SIZE; k++)
                        {
                            if(Car_array[i].getMileage()>Car_array[k].getMileage()) //if car i's mileage is greater than car k's swap them
                            {   temp = Car_array[i]; //put the car in slot i into a temp car object. temp now holds car i
                                Car_array[i] = Car_array[k]; //assign the car at position k to position i
                                Car_array[k]= temp;  //put the temp car i into slot k. now the two objects are swapped in the array
                            }
    
                        }
                    }
                // Loop to display car details
            for (int x=0; x<9; x++) {
                Car_array[x].car_details();
            }
    
            cout << "_______________________________________________________\n";
            
            return 0;
    
    }
    
    Thanks for all of your help!
     
  2. Mar 5, 2010 at 1:32 PM
    #22
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    As EAMonkey pointed out in his previous post (http://www.tacomaworld.com/forum/technology/78292-sort.html#post1500076) the code I gave you was for a bubble sort, not a selection sort. It's easy to switch between the two if you look at his examples.
     
  3. Mar 6, 2010 at 10:10 PM
    #23
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Hey guys, I have tried everything and I can not figure out how to fix this one problem with my binary selection!

    Code:
    #include "CarClass.h"
    using namespace std;
    
    //Set up Search 
    int search(Car object[], int size, int value)
    {
       int index = 0;         // Used as a subscript to search array 
       int position = -1;     // Used to record position of search value 
       bool found = false;    // Flag to indicate if the value was found 
    
       while (index < size && !found)
       {
          if (object[index].getVIN() == value) // If the value is found
          {
             found = true;          // Set the flag 
             position = index;      // Record the value's subscript 
          }
          index++;                  // Go to the next element 
       }
       return position;             // Return the position
    }// End search 
    
    // set up Binary Search
    int binarySearch(int Car[], int size, int valueVin)
    {
        int  first = 0,                    // First array element
             last = size - 1,              // Last array element
             middle,                       // Midpoint of search
             position = -1;                // Position of search value
        bool found = false;                // Flag
    
        while (!found && first <= last)
        {
            middle = (first + last) / 2;    // Calculate midpoint
            if (Car[middle] == valueVin)     // If value is found at mid
            {
                found = true;
                position = middle;
            }
            else if (Car[middle] > valueVin) // If value is in lower half
                last = middle - 1;
            else
                first = middle + 1;          // If value is in upper half
        }
        return position;
    }
    //end binary search
    
    
    int search(Car[], int, int);
    int binarySearch(Car[], int, int);
    
    
    int main() {
        int binarySearch(Car[], int, int);
        
        const int SIZE = 9;
                
        //Array of 9 cars
        Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990, 1237362727), 
                                Car("Ford", "Mustang", "Red", 2007, 49842, 7337372239),
                                Car("Chevrolet", "Beretta", "Black", 1989, 90332, 2873644922),
                                Car("Ford", "Focus", "White", 2008, 150, 9236498273),
                                Car("Voltzwagon", "Jetta", "Black", 2006, 28002, 4673992056),
                                Car("Rolls Royce", "Ghost", "Silver", 2005, 10000, 9292983855),
                                Car("Mazda", "626", "Blue", 2002, 84754, 7364646463),
                                Car("Toyota", "Camry", "Red", 2004, 50332, 2133737227),
                                Car("Bugatti", "Veyron 16.4", "White", 2010, 5, 5712893401)};
    
        
        long long int manualVIN= 2873644922;
        long long int desiredVIN;
        int posLin; // Used to search for VIN 2873644922
        int pos;  // Used for when user wil input the vin they want to search
        Car temp;
    
        //MANUAL LINEAR SEARCH FOR VIN NUMBER
        
            posLin = search(Car_array, SIZE, manualVIN); 
    
            if (posLin == -1)
                cout << "You do not have a car with the VIN Number 2873644922.\n";
            else 
            {
                cout << "You do have a car on the lot with the VIN Number 2873644922.\n";
            }
            cout << "_______________________________________________________\n";
        // END MANUAL LINEAR SEARCH FOR VIN NUMBER
    
        // Get the VIN to search for
    
         // LINEAR SEARCH FOR VIN NUMBER
            cout << "Enter a VIN number to search for: ";
            cin >> desiredVIN;
    
        // Linear Search for VIN Number
            pos = search(Car_array, SIZE, desiredVIN); 
    
            if (pos == -1)
                cout << "You do not have a car with the VIN Number " << desiredVIN << ".\n";
            else 
            {
                cout << "You do have a car on the lot with the VIN Number " << desiredVIN << ".\n";
            }
            cout << "_______________________________________________________\n";
    
         // END LINEAR SEARCH FOR VIN NUMBER
        
        // Loop to display car details
            for (int x=0; x<9; x++) {
                Car_array[x].car_details();
            }
    
            cout << "_______________________________________________________\n";
    
    
            //Selection Sort
    
            cout << " This is the Array of Cars in a selection sort!!\n\n";
    
            //temp = null;
    
    
            for (int i = 0; i < SIZE-1; i++)
                    {
                        for (int k = i+1; k < SIZE; k++)
                        {
                            if(Car_array[i].getMileage()>Car_array[k].getMileage()) //if car i's mileage is greater than car k's swap them
                            {   temp = Car_array[i]; //put the car in slot i into a temp car object. temp now holds car i
                                Car_array[i] = Car_array[k]; //assign the car at position k to position i
                                Car_array[k]= temp;  //put the temp car i into slot k. now the two objects are swapped in the array
                            }
    
                        }
                    }
                // Loop to display car details
            for (int x=0; x<9; x++) {
                Car_array[x].car_details();
            }
    
            cout << "_______________________________________________________\n";
    
    
    int binarySearch(Car_array, SIZE, binaryVin);
    
                                  
        long long int binaryVin = 7337372239;          // Holds the ID to search for
        int results;        // Holds the search results
        
        // Get an employee ID to search for
    
        
        // Search for the ID
        results = binarySearch(Car_array, SIZE, binaryVin);
        
        // If binarySearch returned -1, the ID was not found
        if (results == -1)
            cout << "That number does not exist in the array.\n";
        else
        {  // Otherwise results contains the subscript of
           // the specified employee ID in the array
            cout << "ID " << binaryVin << " was found in element "
                 << results << " of the array.\n";
        }
        return 0;
    }
    
    ERROR:

    1>\car.cpp(144): error C2065: 'binaryVin' : undeclared identifier
    1>\car.cpp(144): error C2365: 'binarySearch' : redefinition; previous definition was 'function'
    1>\car.cpp(55) : see declaration of 'binarySearch'
    1>\car.cpp(144): error C2078: too many initializers
     
  4. Mar 6, 2010 at 10:39 PM
    #24
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Ok I fixed my code and got it running, but for some reason when it does the binary search it tells me that the VIN is not found when it is clearly there! Why is it not working??

    Code:
    #include "CarClass.h"
    using namespace std;
    
    //Set up Search 
    int search(Car object[], int size, int value)
    {
       int index = 0;         // Used as a subscript to search array 
       int position = -1;     // Used to record position of search value 
       bool found = false;    // Flag to indicate if the value was found 
    
       while (index < size && !found)
       {
          if (object[index].getVIN() == value) // If the value is found
          {
             found = true;          // Set the flag 
             position = index;      // Record the value's subscript 
          }
          index++;                  // Go to the next element 
       }
       return position;             // Return the position
    }// End search 
    
    // set up Binary Search
    int binarySearch(Car object[], int size, int valueVin)
    {
        int  first = 0,                    // First array element
             last = size - 1,              // Last array element
             middle,                       // Midpoint of search
             position = -1;                // Position of search value
        bool found = false;                // Flag
    
        while (!found && first <= last)
        {
            middle = (first + last) / 2;    // Calculate midpoint
            if (object[middle].getVIN() == valueVin)     // If value is found at mid
            {
                found = true;
                position = middle;
            }
            else if (object[middle].getVIN() > valueVin) // If value is in lower half
                last = middle - 1;
            else
                first = middle + 1;          // If value is in upper half
        }
        return position;
    }
    //end binary search
    
    
    int search(Car[], int, int);
    int binarySearch(Car[], int, int);
    
    
    int main() {
    
        
        const int SIZE = 9;
                
        //Array of 9 cars
        Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990, 1237362727), 
                                Car("Ford", "Mustang", "Red", 2007, 49842, 7337372239),
                                Car("Chevrolet", "Beretta", "Black", 1989, 90332, 2873644922),
                                Car("Ford", "Focus", "White", 2008, 150, 9236498273),
                                Car("Voltzwagon", "Jetta", "Black", 2006, 28002, 4673992056),
                                Car("Rolls Royce", "Ghost", "Silver", 2005, 10000, 9292983855),
                                Car("Mazda", "626", "Blue", 2002, 84754, 7364646463),
                                Car("Toyota", "Camry", "Red", 2004, 50332, 2133737227),
                                Car("Bugatti", "Veyron 16.4", "White", 2010, 5, 5712893401)};
    
        
        long long int manualVIN= 2873644922;
        long long int desiredVIN;
        int posLin; // Used to search for VIN 2873644922
        int pos;  // Used for when user wil input the vin they want to search
        Car temp;
    
        //MANUAL LINEAR SEARCH FOR VIN NUMBER
        
            posLin = search(Car_array, SIZE, manualVIN); 
    
            if (posLin == -1)
                cout << "You do not have a car with the VIN Number 2873644922.\n";
            else 
            {
                cout << "You do have a car on the lot with the VIN Number 2873644922.\n";
            }
            cout << "_______________________________________________________\n";
        // END MANUAL LINEAR SEARCH FOR VIN NUMBER
    
        // Get the VIN to search for
    
         // LINEAR SEARCH FOR VIN NUMBER
            cout << "Enter a VIN number to search for: ";
            cin >> desiredVIN;
    
        // Linear Search for VIN Number
            pos = search(Car_array, SIZE, desiredVIN); 
    
            if (pos == -1)
                cout << "You do not have a car with the VIN Number " << desiredVIN << ".\n";
            else 
            {
                cout << "You do have a car on the lot with the VIN Number " << desiredVIN << ".\n";
            }
            cout << "_______________________________________________________\n";
    
         // END LINEAR SEARCH FOR VIN NUMBER
        
        // Loop to display car details
            for (int x=0; x<9; x++) {
                Car_array[x].car_details();
            }
    
            cout << "_______________________________________________________\n";
    
    
            //Selection Sort
    
            cout << " This is the Array of Cars in a selection sort!!\n\n";
    
            //temp = null;
    
    
            for (int i = 0; i < SIZE-1; i++)
                    {
                        for (int k = i+1; k < SIZE; k++)
                        {
                            if(Car_array[i].getMileage()>Car_array[k].getMileage()) //if car i's mileage is greater than car k's swap them
                            {   temp = Car_array[i]; //put the car in slot i into a temp car object. temp now holds car i
                                Car_array[i] = Car_array[k]; //assign the car at position k to position i
                                Car_array[k]= temp;  //put the temp car i into slot k. now the two objects are swapped in the array
                            }
    
                        }
                    }
                // Loop to display car details
            for (int x=0; x<9; x++) {
                Car_array[x].car_details();
            }
    
            cout << "_______________________________________________________\n";
    
                                  
        long long int binaryVin = 7337372239;          // Holds the ID to search for
        int results;        // used to search for vin
        
        // Search forVIN
        results = binarySearch(Car_array, SIZE, binaryVin);
        
        // If binarySearch returned -1, the VIN was not found
        if (results == -1)
            cout << "That VIN does not exist in the array.\n";
        else
        {  
            cout << "Vin " << binaryVin << " was found in element "
                 << results << " of the array.\n";
        }
        return 0;
    }
    
     
  5. Mar 6, 2010 at 11:38 PM
    #25
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    I got it done!!!!
    Code:
    #include "CarClass.h"
    using namespace std;
    
    //Set up Search 
    int search(Car object[], int size, int value)
    {
       int index = 0;         // Used as a subscript to search array 
       int position = -1;     // Used to record position of search value 
       bool found = false;    // Flag to indicate if the value was found 
    
       while (index < size && !found)
       {
          if (object[index].getVIN() == value) // If the value is found
          {
             found = true;          // Set the flag 
             position = index;      // Record the value's subscript 
          }
          index++;                  // Go to the next element 
       }
       return position;             // Return the position
    }// End search 
    
    // set up Binary Search
    int binarySearch(Car object[], int size, int valueVin)
    {
        int  first = 0,                    // First array element
             last = size - 1,              // Last array element
             middle,                       // Midpoint of search
             position = -1;                // Position of search value
        bool found = false;                // Flag
    
        while (!found && first <= last)
        {
            middle = (first + last) / 2;    // Calculate midpoint
            if (object[middle].getVIN() == valueVin)     // If value is found at mid
            {
                found = true;
                position = middle;
            }
            else if (object[middle].getVIN() > valueVin) // If value is in lower half
                last = middle - 1;
            else
                first = middle + 1;          // If value is in upper half
        }
        return position;
    }
    //end binary search
    
    
    int search(Car[], int, int);
    int binarySearch(Car[], int, int);
    
    
    int main() {
    
        
        const int SIZE = 9;
                
        //Array of 9 cars
        Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990, 1237362727), 
                                Car("Ford", "Mustang", "Red", 2007, 49842, 7337372239),
                                Car("Chevrolet", "Beretta", "Black", 1989, 90332, 2873644922),
                                Car("Ford", "Focus", "White", 2008, 150, 9236498273),
                                Car("Voltzwagon", "Jetta", "Black", 2006, 28002, 4673992056),
                                Car("Rolls Royce", "Ghost", "Silver", 2005, 10000, 9292983855),
                                Car("Mazda", "626", "Blue", 2002, 84754, 7364646463),
                                Car("Toyota", "Camry", "Red", 2004, 50332, 2133737227),
                                Car("Bugatti", "Veyron 16.4", "White", 2010, 5, 5712893401)};
    
        
        long long int manualVIN= 2873644922;
        long long int desiredVIN;
        int posLin; // Used to search for VIN 2873644922
        int pos;  // Used for when user wil input the vin they want to search
        Car temp;
    
        //MANUAL LINEAR SEARCH FOR VIN NUMBER
        
            posLin = search(Car_array, SIZE, manualVIN); 
    
            if (posLin == -1)
                cout << "You do not have a car with the VIN Number 2873644922.\n";
            else 
            {
                cout << "You do have a car on the lot with the VIN Number 2873644922.\n";
            }
            cout << "_______________________________________________________\n";
        // END MANUAL LINEAR SEARCH FOR VIN NUMBER
    
        // Get the VIN to search for
    
         // LINEAR SEARCH FOR VIN NUMBER
            cout << "Enter a VIN number to search for: ";
            cin >> desiredVIN;
    
        // Linear Search for VIN Number
            pos = search(Car_array, SIZE, desiredVIN); 
    
            if (pos == -1)
                cout << "You do not have a car with the VIN Number " << desiredVIN << ".\n";
            else 
            {
                cout << "You do have a car on the lot with the VIN Number " << desiredVIN << ".\n";
            }
            cout << "_______________________________________________________\n";
    
         // END LINEAR SEARCH FOR VIN NUMBER
        
        // Loop to display car details
            for (int x=0; x<9; x++) {
                Car_array[x].car_details();
            }
    
            cout << "_______________________________________________________\n";
    
    
            //Selection Sort
    
            cout << " This is the Array of Cars in a selection sort!!\n\n";
    
            //temp = null;
    
    
            for (int i = 0; i < SIZE-1; i++)
                    {
                        for (int k = i+1; k < SIZE; k++)
                        {
                            if(Car_array[i].getMileage()>Car_array[k].getMileage()) //if car i's mileage is greater than car k's swap them
                            {   temp = Car_array[i]; //put the car in slot i into a temp car object. temp now holds car i
                                Car_array[i] = Car_array[k]; //assign the car at position k to position i
                                Car_array[k]= temp;  //put the temp car i into slot k. now the two objects are swapped in the array
                            }
    
                        }
                    }
                // Loop to display car details
            for (int x=0; x<9; x++) {
                Car_array[x].car_details();
            }
    
            cout << "_______________________________________________________\n";
    
                                  
        long long int binaryVin = 7337372239;          // Holds the ID to search for
        int results;        // used to search for vin
        
        // Search forVIN
        results = binarySearch(Car_array, SIZE, binaryVin);
        
        // If binarySearch returned 1, the VIN was not found
        if (results == 1)
            cout << "That VIN does not exist in the array.\n";
        else
        {  
            cout << "Vin " << binaryVin << " was found in the array.\n";
        }
        
            cout << "_______________________________________________________\n";
                    // Loop to display car details
            for (int x=0; x<9; x++) {
                Car_array[x].car_details();
            }
    
    
    
        return 0;
    }
    
     
  6. Mar 7, 2010 at 7:55 AM
    #26
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    Have you tried searching for each item in the array? My guess based on the code is that you got lucky and hit one. A binary search requires a sorted list (like a phone book). Otherwise you can't guarantee that the vin resides in the upper or the lower half.

    You'll need to sort based on the vin number first.
     

Products Discussed in

To Top