Reputation: 3
I am writing a program that needs to compare two elements at a time (of a 9 element vector) to find the lowest element. To do this I created a Sort function that acts like a Bubblesort and at the end of the sort I will take the first element of the vector (Which will be the lowest). My problem is that Xcode is telling me my equation is pointing to NULL. Any ideas?
void ParkingLot::GateA(){
vector<int> AvailSpots (9);
AvailSpots[0] = 40;
AvailSpots[1] = 20;
AvailSpots[2] = 50;
AvailSpots[3] = 30;
AvailSpots[4] = 55;
AvailSpots[5] = 15;
AvailSpots[6] = 20;
AvailSpots[7] = 33;
AvailSpots[8] = 27;
vector<string> Lot (9);
Lot[0] = "A";
Lot[1] = "B";
Lot[2] = "C";
Lot[3] = "D";
Lot[4] = "E";
Lot[5] = "F";
Lot[6] = "G";
Lot[7] = "H";
Lot[8] = "I";
vector<int> Cost (9);
Cost[0] = 25.00;
Cost[1] = 22.50;
Cost[2] = 20.00;
Cost[3] = 22.50;
Cost[4] = 20.00;
Cost[5] = 17.50;
Cost[6] = 20.00;
Cost[7] = 17.50;
Cost[8] = 15.00;
vector<int> Distance (9);
Distance[0] = 10;
Distance[1] = 20;
Distance[2] = 30;
Distance[3] = 20;
Distance[4] = 30;
Distance[5] = 40;
Distance[6] = 30;
Distance[7] = 40;
Distance[8] = 50;
int choice2;
cout << "-------------------------------------------------------------------------" << endl;
cout << setw(50) << "GATE A: Lot Information" << endl;
cout << "-------------------------------------------------------------------------" << endl;
cout << "LOT ID : MAX CAPACITY : AVAILABLE SPOTS : COST($DD.CC) : DISTANCE" << endl;
cout << "-------------------------------------------------------------------------" << endl;
cout << Lot[0] << ": 40 : "<<AvailSpots[0]<<": "<<setw(20)<<" "<<Cost[0]<<" : " <<Distance[0] << endl;
cout << Lot[1] << ": 20 : "<<AvailSpots[1]<<": "<<setw(20)<<" "<<Cost[1]<<" : " <<Distance[1] << endl;
cout << Lot[2] << ": 50 : "<<AvailSpots[2]<<": "<<setw(20)<<" "<<Cost[2]<<" : " <<Distance[2] << endl;
cout << Lot[3] << ": 30 : "<<AvailSpots[3]<<": "<<setw(20)<<" "<<Cost[3]<<" : " <<Distance[3] << endl;
cout << Lot[4] << ": 55 : "<<AvailSpots[4]<<": "<<setw(20)<<" "<<Cost[4]<<" : " <<Distance[4] <<endl;
cout << Lot[5] << ": 15 : "<<AvailSpots[5]<<": "<<setw(20)<<" "<<Cost[5]<<" : " <<Distance[5] <<endl;
cout << Lot[6] << ": 20 : "<<AvailSpots[6]<<": "<<setw(20)<<" "<<Cost[6]<<" : " <<Distance[6] << endl;
cout << Lot[7] << ": 33 : "<<AvailSpots[7]<<": "<<setw(20)<<" "<<Cost[7]<<" : " <<Distance[7] << endl;
cout << Lot[8] << ": 27 : "<<AvailSpots[8]<<": "<<setw(20)<<" "<<Cost[8]<<" : " <<Distance[8] << endl;
cout << " Total : 290 : "<<TotalAvailSpots<<":" << endl;
cout << "Select a criteria to allot a parking lot :" << endl;
cout << "1. Based on Cost - Cheapest Parking Lot" << endl;
cout << "2. Based on Distance - Closest to Stadium" << endl;
cout << "0. EXIT" << endl;
cout << "Enter Option (1-2): " << endl;
cin >> choice2;
if (choice2 == 1) {
ParkingLot::Sort();
}
}
int ParkingLot::Sort(){
int cost;
for (int i = 0; i < 8; i++){
if (Cost[i] > Cost[i + 1]) { /////////////ERROR APPEARS HERE Thread1:EXC_BAD_ACCESS(code=1, address=(0x0)
ParkingLot::Swap(i, i+1);
}
}
cout << "Cost of parking lot " << Cost.front() << endl;
cin >> cost;
return cost;
}
void ParkingLot::Swap(int a, int b){
int tmp = Cost[a];
Cost[a] = Cost[b];
Cost[b] = tmp;
}
Upvotes: 0
Views: 107
Reputation: 66371
The variables you define in GateA
are local to the function and hiding any member variables with the same names.
So the Cost
vector you're using in Sort
and Swap
is empty because it is a different one from the one you built in GateA
.
Remove the line
vector<int> Cost (9);
from GateA
and instead call Cost.resize(9)
before assigning the elements, or build the vector using push_back
rather than direct indexing.
Upvotes: 2