Oct 13, 2017 01:12 PM
(This post was last modified: Oct 13, 2017 01:33 PM by J. McCarthy.)
Okay so basically I have a 2D array of size n*n
It's fully populated and I'm trying to wipe out an entire row/column for each position within 1->n that I visit. Problem is that it's getting hung up and re-visiting a position that should have been replaced with a zero after the third iteration. I don't get why in my code it's not recognizing it as a zero in the loop iteration though?
Any thoughts?
I'm sure it's a stupid mistake in my loop just can't find it.
I deleted my counter reset on accident
Never mind ^_^
It's fully populated and I'm trying to wipe out an entire row/column for each position within 1->n that I visit. Problem is that it's getting hung up and re-visiting a position that should have been replaced with a zero after the third iteration. I don't get why in my code it's not recognizing it as a zero in the loop iteration though?
Any thoughts?
I'm sure it's a stupid mistake in my loop just can't find it.
Code
//~~~~~~~~ Start 2D Array Cost Function
int DistToSave = 10000; //Stores The Distance You'll Save (Greedy Distance)
int DistToSaveIndex=999; //999 For QA Test
int RowIndex = 0;
System.out.println("N IS EQUAL TO " + n);
for(int TimesToRun = 0; TimesToRun < 2; TimesToRun++){ //Times To Run Loop
//Greedy//
for(int Counter = 0; Counter < n; Counter++) {
if(DataArray[RowIndex][Counter] != 0) {
if(DataArray[RowIndex][Counter] < DistToSave) {
DistToSave = DataArray[RowIndex][Counter]; //Set Current Smallest Value To DistToSave
DistToSaveIndex = Counter;
}
}
}
AnswerArray[0][TimesToRun] = DistToSave;
AnswerArray[1][TimesToRun] = DistToSaveIndex;
System.out.println("The Smallest Value In Row is " + DistToSave + " Index is " + DistToSaveIndex);
for(int Counter = 0; Counter < n; Counter++) {
DataArray[RowIndex][Counter] = 0;
DataArray[Counter][RowIndex] = 0;
}
RowIndex = DistToSaveIndex; //Move To Next Location Using Index
} //End Times To Run Loop
//PRINT OUT 2D ARRAY
for(int s = 0; s<n; s++){
for(int w = 0; w<n; w++)
{
System.out.print(DataArray[s][w] + " ");
}
System.out.println();
}
//Print Out Answer Array
System.out.println();
System.out.println();
System.out.println();
System.out.println("Top Is Distance To Travel & Bottom Is Index of Visited Node");
System.out.println();
for(int s = 0; s<2; s++){
for(int w = 0; w<n; w++)
{
System.out.print(AnswerArray[s][w] + " ");
}
System.out.println();
}
System.out.println();
System.out.println();
//END PRINT OUT 2D ARRAY
//~~~~~~~~ End 2D Array Cost FunctionI deleted my counter reset on accident
Never mind ^_^

