1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#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
//Sort Array
void selectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
int search(Car[], int, int);
void selectionSort(Car[], int, int);
void showArray(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
//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";
//Sort the Array
selectionSort(Car_array, SIZE, Car::getMileage);
//Display
showArray();
//Show Array
void showArray();
{
for (int x=0; x<9; x++) {
Car_array[x].car_details();
}
}
return 0;
}
|