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
|
#include "CarClass.h"
using namespace std;
int search(Car[], string, 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)};
int desiredVin;
int manualVIN= 2873644922;
int pos;
char doAgain;
pos = search(Car_array, SIZE, manualVIN);
//Linear Search for VIN Number
if (pos == -1)
cout << "You did not have a car with the VIN Number 2873644922.\n\n";
else
{
cout << "You do have a car on the lot with the VIN Number 2873644922.\n\n";
}
return 0;
}
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, or -1
}// End search
|