The following for loop halts execution at the first cycle, and I have no clue why. I have figured out by placing couts in it that it stops at the push_back. It was working before, then I tried modifying it, then I did Ctrl-z to get it back to this state, and now it suddenly halts at push_back when it didn't seem to before. What is going on?
#include <iostream>
#include <vector>
using namespace std;
void decode(int[], int, int[][2]);
void displayArray(int[], int);
int main()
{
const int SIZE = 12;
int test[SIZE-2] = {3,9,1,4,8,0,11,5,1,8};
int edgeList[SIZE-1][2];
for (int i = 0; i < SIZE -1; i++)
{
edgeList[i][0] = -1;
edgeList[i][1] = -1;
}
decode(test, SIZE, edgeList);
return 0;
}
void decode(int inputString[], int size, int edgeList[][2])
{
int** arrayC = new int*[size - 2];
for(int i = 0; i < size - 2; i++)
arrayC[i] = new int[2];
for (int i = 0; i < size -2 ; i++)
{
arrayC[i][0] = i+1;
arrayC[i][1] = inputString[i];
}
for (int i = 0; i < size - 2; i++)
{
displayArray(arrayC[i], 2);
}
for (int i = 0; i < size - 1; i++)
{
vector<int> currentCycle;
int *visited = new int[size - 2];
for(int j = 0; j < size - 1; j++)
{
visited[j] = 0;
}
bool repeat = false;
int currentIndex = i;
while(!repeat)
{
int curElem = arrayC[currentIndex][0];
if (!visited[curElem] && curElem != 0 && curElem != size - 1)
{
cout << curElem << endl;
currentCycle.emplace_back(curElem);
visited[curElem] = 1;
}
else
{
repeat = true;
}
currentIndex = arrayC[currentIndex][1] - 1;
if (currentIndex == -1 || currentIndex == size -2)
{
repeat = true;
currentCycle.push_back(-1);
}
}
for (int i = 0; i < currentCycle.size(); i++)
cout << currentCycle[i] << " ";
cout << endl;
delete visited;
}
}
void displayArray(int array[], int size)
{
for (int i = 0; i < size; i++)
{
cout << array[i] << " ";
}
cout << endl;
}