Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Need Help W/ This Bit of Code Replacing Data Within a 2D Array Not Updating

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 ^_^


Possibly Related Threads...
Thread / Author Replies Views Last Post
by HackersApr 14, 2024 12:50 PM 14 665 Apr 14, 2024 12:50 PM
Last Post: Hackers
[C#] by gigajewJul 18, 2021 11:17 AM 26 2,917 Jul 18, 2021 11:17 AM
Last Post: gigajew
[Python] by DocDec 9, 2020 12:23 PM 8 262 Dec 9, 2020 12:23 PM
Last Post: synchronizing