Skip to content
Advertisement

Adding elements from one 2d array to another 2d array Java

I’m trying to add specific elements from one 2d array to another , the process of adding is that I’m choosing the smallest element in the first row of the array and add that element to the other 2d array at the same position so for example :

2 22 3

5 1 54

7 3 10

20 22 21

here the smallest element in the first row is 2 so 2 should be added to the other 2d array at the same position , and for the second row 1 is the smallest element so we add 1 to the other 2d array as well , the smallest element in the third row is 3 , and the final row the fourth row the smallest element is 20 but we cannot pick 20 as the smallest because we check the sum of columns in the second 2d array which was initialized with 0’s , so here if we pick 20 then the sum of its column is 21 because 20 + 1 + 0 + 0 = 21 , if we pick 22 then 22 + 3 + 1 + 0 = 26 and if we pick 21 then 21 + 0 + 0 + 0 = 21 ,so here 21 is the smallest because the sum of its column is the smallest :

1 0 0

0 1 0

0 3 0

0 0 21

and the process goes on for the other rows and columns if there are any.

This is what I’ve made so far :

import java.util.Arrays;
public class Main
{
//Method which finds the sum of columns
public static int[] sumOfCol(int[][] tempArr){
    int size = tempArr[0].length; // Replace it with the size of maximum length inner 
array
    int temp[] = new int[size];

    for (int i = 0; i < tempArr.length; i++){
        for (int j = 0; j < tempArr[i].length; j++){
            temp[j] += tempArr[i][j];
        }
    }
    System.out.println(Arrays.toString(temp));
    return temp;
}
//The algorithm
public static void algorithm(int[][] tempArr,int[][] tempArr2){
    for (int i = 0; i < tempArr.length; i++) {
        int minimum = tempArr[i][0];
        for (int j = 0; j < tempArr[0].length; j++) {
            if (tempArr[i][j] < minimum) {
                minimum = tempArr[i][j];
                tempArr2[i][j] = minimum;
            }
        }
    }
}
//To generate teh Array
public static void generateArray(int[][] arr){
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 3; j++) {
            arr[i][j] = (int) ((Math.random() * (25 - 1)) + 1);
        }
    }
}
//To print an array
public static void printArray(int[][] arr){
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[0].length; j++) {
            System.out.print(arr[i][j] + " ");
        }
        System.out.println();
    }
}
public static void main(String[] args) {
    int [][] tempArr = new int [5][3];
    int[][] tempArr2 = new int[tempArr.length][tempArr[0].length];
    //Initilise second array with 0's
    for (int i = 0; i < tempArr2.length; i++) {
        for (int j = 0; j < tempArr2[0].length; j++) {
            tempArr2[i][j] = 0;
        }
    }
    generateArray(tempArr);
    System.out.println("Array before executing the algorithm : ");
    printArray(tempArr);
    algorithm(tempArr,tempArr2);
    System.out.println("Array after executing the algorithm : ");
    printArray(tempArr2);
}
}

Advertisement

Answer

I wasn’t able to find the error in your code but found another way to do what you are looking for:

    //The algorithm
    public static int[][] algorithm(int[][] tempArr){
        // create the empty array.
        int[][] modifiedArr = new int[tempArr.length][tempArr[0].length];
        for (int i = 0; i < modifiedArr.length; i++) {
            for (int j = 0; j < modifiedArr[0].length; j++) {
                modifiedArr[i][j] = 0;
            }
        }
        // loop through all rows and change one value in each row on modifiedArr in a way that leads to the column sum being miniumum.
        for (int i = 0; i < tempArr.length; i++) {
            setMinimumColumn(tempArr,modifiedArr,i);
        }
        return modifiedArr;
    }

    static void setMinimumColumn(int[][] originalArr, int[][] modifiedArr, int row){
        int minColumn = 0;
        int minColumnSum = Integer.MAX_VALUE;
        for (int i=0; i <originalArr[row].length ;i++){
            modifiedArr[row][i] = originalArr[row][i];
            int columnSum = getColumnSum(modifiedArr,i);
            if (columnSum < minColumnSum) {
                minColumn = i;
                minColumnSum = columnSum;
            }
            modifiedArr[row][i] = 0;
        }
        modifiedArr[row][minColumn] = originalArr[row][minColumn];
    }

    static int getColumnSum(int[][] arr, int column){
        int sum = 0;
        for (int i = 0; i<arr.length; i++){
            for (int j = 0; j<arr[0].length; j++) {
                if (j == column){
                    sum+=arr[i][j];
                }
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        int [][] tempArr = new int [5][3];
        generateArray(tempArr);
        
        System.out.println("Array before executing the algorithm : ");
        printArray(tempArr);

        System.out.println("Array after executing the algorithm : ");
        printArray(algorithm(tempArr));
    }

Basically, you start by looping through the rows. Every row is given to the setMinimumColumn method, which loops through all element in that row and tries putting them in modifiedArr in a way that minimizes the column sum. Doing this for all columns gives you the answer.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement