4 Replies Latest reply: Mar 26, 2008 3:58 PM by 807601 RSS

    Sorting 1 dimension of a 2d array

    807601
      int events = 22;
      int times = 10;
              
      double [][] storetimes = new double [events][times]; 
      int sort_event = io.inputInt();
                  double [] sort_array = new double [10];
                  for(int i = 0; i < 9; i++)
                  {
                      for(int j = 0; j < 9; j++)
                      {
                          storetimes[sort_event] = sort_array[j]; 
      }
      }

      java.util.Arrays.sort(sort_array);
      for(int i = 0; i < 10; i++)
      {
      System.out.println(sort_array[i]);
      }
      }
      I want the code to take the i values from a certain sort_event value and place them its own 1 dimensional array, yet when I try with a sort_event value that has many i values, it returns 0 10 times.  I have no idea what is wrong with this code. Any idea's?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: Sorting 1 dimension of a 2d array
          807601
          I don't see where you place any values into the members of sort_array[].

          [edit] Or you have the line "storetimes[sort_event] = sort_array[j];" backward?

          Edited by: darb on Mar 26, 2008 1:24 PM
          • 2. Re: Sorting 1 dimension of a 2d array
            807601
            I do but it's in another long and very tedious method. I'll give it to you right here.
            //adds 1 time to the array based on user input
                            System.out.println("What event would you like to add to?");
                            int event_add = io.inputInt();
                            System.out.println("What slot would you like it added to?");
                            int slot_add = io.inputInt();
                            System.out.println("Enter a time in seconds");
                            double time_add = io.inputDouble(); 
                            storetimes [event_add][slot_add]= time_add;        
                             
                        
                    //error checking.  Checks bewtween world record times and average times of about a 6 year old swimmer.
                        if(event_add == 1 && time_add > 18.00 && time_add < 55.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        
                        else if(event_add == 2 && time_add > 42.00 && time_add < 130.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        
                        else if(event_add == 3 && time_add > 90.00 && time_add < 240.00   )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        
                        else if(event_add == 4 && time_add > 248.00 && time_add < 540.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        
                        else if(event_add == 5 && time_add > 420.00 && time_add < 1300.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        
                        else if(event_add == 6 && time_add > 870.00 && time_add < 2100.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 7 && time_add > 20.00 && time_add < 60.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 8 && time_add > 44.00 && time_add < 150.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 9 && time_add > 99.00 && time_add < 300.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 10 && time_add > 24.00 && time_add < 60.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 11 && time_add > 52.00 && time_add < 150.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 12 && time_add > 111.00 && time_add < 300.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 13 && time_add > 23.00 && time_add < 60.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 14 && time_add > 44.00 && time_add < 120.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 15 && time_add > 96.00 && time_add < 300.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 16 && time_add > 52.00 && time_add < 135.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 17 && time_add > 100.00 && time_add < 300.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 18 && time_add > 216.00 && time_add < 500.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 19 && time_add > 84.00 && time_add < 150.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 20 && time_add > 76.00 && time_add < 150.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 21 && time_add > 182.00 && time_add < 360.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else if(event_add == 22 && time_add > 169.00 && time_add < 360.00 )
                        {
                            storetimes [event_add][slot_add]= time_add;
                            System.out.println("Your time added was " + time_add);
                        }
                        else
                        {
                            System.out.println("That is an invalid input...The time doesnt make sense!  Try again");
                        }
            • 3. Re: Sorting 1 dimension of a 2d array
              807601
              ksa.gringo wrote:
              I do but it's in another long and very tedious method. I'll give it to you right here.
              I give you my honesty award of the day for that. However, the variable sort_array appears nowhere in it, unless my browser's find function is broken.

              As well, in your first less tedious code example ;-), you declare the variable and straight away use it. There's no opportunity for the second method to be called, even if it did populate it.

              At least one of us is confused, and I very well may be one of them...
              • 4. Re: Sorting 1 dimension of a 2d array
                807601
                The sort_array array isn't really in the second example of code because it was created to have the values from storetimes[sort_event][ i ] values put into it. I want the corresponding i values from storetimes[sort_event][ i ] for sort_array[ j ]. I appreciate the help though, and the sense of humor. Kinda rare on these boards...:p