Hack Forums
[Java] Need Help W/ This Bit of Code Replacing Data Within a 2D Array Not Updating - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Need Help W/ This Bit of Code Replacing Data Within a 2D Array Not Updating (/showthread.php?tid=5736463)



Need Help W/ This Bit of Code Replacing Data Within a 2D Array Not Updating - J. McCarthy - Oct 13, 2017

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.


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 Function


I deleted my counter reset on accident

Never mind ^_^