I am having trouble with my code. The code is able to open both of the text files, however, when I try to run it I get a EXC_BAD_ACCESS error.
The objective of this code is to run the assembly line algorithm for an assembly with 3 lines (located in the text files).
I'm coding in xcode, not sure if that makes a difference.
Any help or suggestions would be great, thanks :)
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
#define NUM_STATIONS 20
#define MAX_STATIONS 20
int bookk = 0;
//Returns the min value between x and y and z
int min(int x, int y, int z){
bookk++;
return x < y ? (x < z ? x : z) : (y < z ? y : z);
}
int minLines1(int a, int b, int c){
return a < b ? (a < c ? a : c) : (b < c ? b : c);
}
int minLines2(int d, int f, int g){
return d < f ? (d < g ? d : g) : (f < g ? f : g);
}
int minLines3(int h, int k, int l){
return h < k ? (h < l ? h : l) : (k < l ? k : l);
}
//Function that returns the best possible path
int assemblyLine(int *e, int *x, int a[][MAX_STATIONS], int t[][MAX_STATIONS]){
int times1[NUM_STATIONS];
int times2[NUM_STATIONS];
int times3[NUM_STATIONS];
int i;
//Initial time for line 1
times1[0] = e[0] + a[0][0];
bookk++;
//Initial time for line 2
times2[0] = e[1] + a[1][0];
bookk++;
//Initial time for line 3
times3[0] = e[2] + a[2][0];
bookk++;
for (i = 1; i < sizeof(times1)/4; ++i){
cout << "Station: " << i << " - Line: " << endl;
times1[i] = min(times1[i-1] + a[0][i], times2[i-1] + t[1][i-1] + a[0][i], times3[i-1] + t[2][i-1] + a[0][i]);
bookk++;
//cout << "Time1[" << i << "] = " << times1[i] << endl;
times2[i] = min(times2[i-1] + a[1][i], times1[i-1] + t[0][i-1] + a[1][i], times3[i-1] + t[2][i-1] + a[1][i]);
bookk++;
//cout << "Time2[" << i << "] = " << times2[i] << endl;
times3[i] = min(times3[i-1] + a[2][i], times1[i-1] + t[0][i-1] + a[2][i], times2[i-1] + t[1][i-1] + a[2][i]);
bookk++;
int lineUsed = minLines1(times1[i], times2[i], times3[i]);
if(lineUsed == 1){
cout << "Station: " << i << " - Line: " << minLines1(times1[i-1] + a[0][i], times2[i-1] + t[1][i-1] + a[0][i], times3[i-1] + t[2][i-1] + a[0][i]) << endl;
}
if(lineUsed == 2){
cout << "Station: " << i << " - Line: " << minLines2(times2[i-1] + a[1][i], times1[i-1] + t[0][i-1] + a[1][i], times3[i-1] + t[2][i-1] + a[1][i]) << endl;
}
if(lineUsed == 3){
cout << "Station: " << i << " - Line: " << minLines3(times3[i-1] + a[2][i], times2[i-1] + t[0][i-1] + a[2][i], times2[i-1] + t[1][i-1] + a[2][i]) << endl;
}
}
return min(times1[NUM_STATIONS-1] + x[0], times2[NUM_STATIONS-1] + x[1], times3[NUM_STATIONS-1] + x[2]);
}
int main(){
ifstream entryExitProcessFile("ProcessTimes.txt", ios::in);
ifstream transitions_file("ThreeLaneTransitions.txt", ios::in);
string line;
int linecount = 0;
//Entry and exit vector
int e[3];
int x[3];
//Process vector
int a[3][MAX_STATIONS];
//Transition vector
int t[3][MAX_STATIONS];
if (entryExitProcessFile.is_open()){
cout << "File open!" << endl;
//First line -> entry vector e
entryExitProcessFile >> e[0];
entryExitProcessFile >> e[1];
entryExitProcessFile >> e[2];
//cout << "Vector e[0]:" << e[0] << endl;
//cout << "Vector e[1]:" << e[1] << endl;
while(getline(entryExitProcessFile, line)){
//Second Line -> exit vector x
if (linecount == 0){
entryExitProcessFile >> x[0];
entryExitProcessFile >> x[1];
entryExitProcessFile >> x[2];
//cout << "Vector x[0]:" << x[0] << endl;
//cout << "Vector x[1]:" << x[1] << endl;
//cout << "Vector x[2]:" << x[2] << endl;
linecount++;
}
else{
//Other lines -> process vector a
//First line assignment
entryExitProcessFile >> a[0][linecount-1];
//cout << "Vector a[0][" << linecount-1 << "]:" << a[0][linecount-1] << endl;
//Second line assignment
entryExitProcessFile >> a[1][linecount-1];
//cout << "Vector a[1][" << linecount-1 << "]:" << a[1][linecount-1] << endl;
//Third line assignment
entryExitProcessFile >> a[2][linecount-1];
//cout << "Vector a[1][" << linecount-1 << "]:" << a[2][linecount-1] << endl;
linecount++;
}
}
entryExitProcessFile.close();
}
else cout << "Unable to open file." << endl;
if(transitions_file.is_open()){
cout << "File open!" << endl;
linecount = 0;
//First line assignment
transitions_file >> t[0][0];
transitions_file >> t[1][0];
transitions_file >> t[2][0];
//cout << "Vector t[0][0]:" << t[0][0] << endl;
//cout << "Vector t[1][0]:" << t[1][0] << endl;
linecount++;
while(getline(transitions_file, line)){
//Other lines assignments
transitions_file >> t[0][linecount];
//cout << "Vector t[0][" << linecount << "]:" << t[0][linecount] << endl;
transitions_file >> t[1][linecount];
//cout << "Vector t[1][" << linecount << "]:" << t[1][linecount] << endl;
transitions_file >> t[2][linecount];
//cout << "Vector t[1][" << linecount << "]:" << t[2][linecount] << endl;
linecount++;
}
transitions_file.close();
}
else cout << "Unable to open file." << endl;
cout << "Best possible path cost: " << assemblyLine(e, x, a, t) << endl;
cout << "Number of bookkeepings couted: " << bookk << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire