Valravn
Valravn

Reputation: 17

java 2d array change highest value of each row to zero

I'm trying to change the highest value of each row in a 2d array to 0. The problem I'm having is that it changes the highest value to zero, but it also changes the values following the highest value to zero. Here's my code:

public class Test2 {
  public static double[][] array1 = //array to be used
   {{7.51, 9.57, 6.28, 5.29, 8.7},
    {8.07, 6.54, 5.44, 8.78, 8.66},
    {9.34, 9.73, 7.19, 6.87, 6.48}};
  public static void main(String[] args) {
    double h = array1[0][0];// highest value 

    for ( int a = 0; a < array1.length; a++){ //loop through array and change highest values in each row to zero
      h = array1[a][0];
      for(int b = 0; b < array1[a].length; b++) {
        if ( array1[a][b] > h){
          array1[a][b] = 0;
          h = array1[a][b];
        }
      }
    }
    System.out.println(); //print array with highest values in each row now changed to zero
    for (int x = 0; x < array1.length; x++){
     System.out.println();
     for (int y = 0; y < array1[x].length; y++){
      System.out.print(array1[x][y] + " "); 
     }
    }    
   }
  }

The current output is this:

7.51 0.0 0.0 0.0 0.0 
8.07 6.54 5.44 0.0 0.0 
9.34 0.0 0.0 0.0 0.0 

While the desired output is this:

7.51 0.0 6.28 5.29 8.7 
8.07 6.54 5.44 0.0 8.66 
9.34 0.0 7.19 6.87 6.48 

How can I make it change only the max value and not the others?

Upvotes: 1

Views: 83

Answers (4)

nLee
nLee

Reputation: 1385

In your code:

for ( int a = 0; a < array1.length; a++){ //loop through array and change highest values in each row to zero
        h = array1[a][0];
        for(int b = 0; b < array1[a].length; b++) {
            if ( array1[a][b] > h){
                array1[a][b] = 0;
                h = array1[a][b];
            }
        }
    }

instead of setting array[a][b] = 0 you should first set h = array[a][b]. Then you should keep track of the b index by setting it in a variable, yIndex = b; finally, set the array at location [a][yIndex] to 0

So you should have something like this:

    for ( int a = 0; a < array1.length; a++){ //loop through array and change highest values in each row to zero
        h = array1[a][0];
        for(int b = 0; b < array1[a].length; b++) {
            if ( array1[a][b] > h){
                h = array1[a][b];
                int yindex = b;
                array1[a][yindex] = 0;
            }
        }
    }

Upvotes: 0

Mr Zorn
Mr Zorn

Reputation: 1913

public static double[][] array1 = // array to be used
    { { 7.51, 9.57, 6.28, 5.29, 8.7 }, { 8.07, 6.54, 5.44, 8.78, 8.66 }, { 9.34, 9.73, 7.19, 6.87, 6.48 } };

    public static void main(String[] args) {

        //loop through each row
        for(int x = 0; x<array1.length; x++){
            //make sure not empty (won't be an issue with the supplied array, but could be)
            if(array1[x].length > 0){
                //take the number as a starting place for your highest index
                //you don't want to pick 0 as the highest number in case all of the numbers are negative
                int highestIndex = 0;
                double highestNumber = array1[x][0];

                //look at the rest of the numbers and see if there are any that are higher
                for(int y = 1; y<array1[x].length; y++){
                    if(array1[x][y] > highestNumber){
                        highestIndex = y;
                        highestNumber = array1[x][y];
                    }
                }

                //set whatever is the highest to 0
                array1[x][highestIndex] = 0;
            }
        }

        System.out.println(); // print array with highest values in each row now
                                // changed to zero
        for (int x = 0; x < array1.length; x++) {
            System.out.println();
            for (int y = 0; y < array1[x].length; y++) {
                System.out.print(array1[x][y] + " ");
            }
        }
    }

Upvotes: 0

Shadow
Shadow

Reputation: 4016

I think you're overcomplicating how you are removing the highest value. I would do it something like this:

int highestIndex = 0;
for (int i = 0; i < array1.length; i++)
{
    for (int j = 0; j < array1[i].length; j++)
        if (array1[i][j] > array1[i][highestIndex])
            highestIndex = j;
    array1[i][highestIndex] = 0;
}

What this does is loop over each row, find the index of the highest value, and then after that, sets just that index to 0.

Upvotes: 0

rgettman
rgettman

Reputation: 178333

Here's what the code is doing for each row.

  1. Get the first value as h.
  2. If the current value is greater than h, set it to 0 and then set h to that value, which is now 0.
  3. All subsequent values are positive, so they are greater than h (0), so they get set to 0 also.

What you should do instead:

  1. Get the maximum value of the row, noting down the index of the maximum value.
  2. At the end of the row, set the maximum value to 0 using the index stored in step 1.

Upvotes: 2

Related Questions