Home > database >  Multiply an element in a Nested Array by certain value and then add the results together
Multiply an element in a Nested Array by certain value and then add the results together

Time:06-26

I am trying to multiply each "column" of an array by a specific number, and then sum up the results for each "row".

I was able to figure out how to do this for a single array, but I am having trouble figuring this out for a 2d-array.

I need to do this recursively, so no loops. Can anybody provide a pseudocode, or explain what steps I should be taking for each column?

public static int didIt(int[] d, int n) {        
    //base or terminating condition
    if (n <= 0) {
        return 0;
    }
    if (n == 1) {
        return didIt(d, n - 1)   d[n - 1] * 10;
    }
    if (n == 2) {
        return didIt(d, n - 1)   d[n - 1] * 50;
    }
    if (n == 3) {
        return didIt(d, n - 1)   d[n - 1] * 22;
    }
    if (n == 4) {
        return didIt(d, n - 1)   d[n - 1] * 7;
    }
    if (n == 5) {
        return didIt(d, n - 1)   d[n - 1] * 45;
    } else {
        return didIt(d, n - 1)   d[n - 1];
    }
}

public static void main(String[] args) {

    int[] array2 = {1, 2, 3, 4, 5};

    System.out.println(didIt(array2, array2.length));
}

How would I turn this code into a 2d-array version?

CodePudding user response:

Update

Since you need every "row" to be treated in the same way you can recursively add up the total calculated for every nested array by moving position in the matrix with every recursive call:

public static int arrSum(int[] arr, int pos) {
    if (pos == arr.length) { // base case
        return 0;
    }
    
    return arr[pos]   arrSum(arr, pos   1); // recursive case
}

Your condition logic can be brushed up like that:

public static int arrSum(int[] arr, int pos) {

    if (pos == arr.length) { // base case
        return 0;
    }
    
    if (pos == 0) {
        return 10 * arr[pos]   arrSum(arr, pos   1);
    }
    if (pos == 1) {
        return 50 * arr[pos]   arrSum(arr, pos   1);
    }
    if (pos == 2) {
        return 22 * arr[pos]   arrSum(arr, pos   1);
    }
    if (pos == 3) {
        return 7 * arr[pos]   arrSum(arr, pos   1);
    }
    if (pos == 4) {
        return 45 * arr[pos]   arrSum(arr, pos   1);
    }
    
    return arr[pos]   arrSum(arr, pos   1);
}

This part is related to the Initial version of the Question

You don't need redundant conditional logic repeated for every hard-coded value of n.

As well as there's no need to multiply each element of the array by 10, instead we can multiply by 10 the overall sum.

That's how your recursive method that calculates the array sum can be fixed:

public static int arrSum(int[] arr, int pos) {
    if (pos == arr.length) { // base case
        return 0;
    }
    
    return arr[pos]   arrSum(arr, pos   1); // recursive case
}

And that how you can calculate the sum of the elements of a nested array (the logic is almost the same):

public static int matrixSum(int[][] matrix, int pos) {
    if (pos == matrix.length) { // base case
        return 0;
    }
    
    return arrSum(matrix[pos], 0)   matrixSum(matrix, pos   1); // recursive case
}

main()

public static void main(String[] args) {
    int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    System.out.println(matrixSum(matrix, 0) * 10);
}

Output:

450 // sum of numbers from 1 to 9 equels 45, and * 10 = 450

Sidenote: in Java there's no 2d-arrays (so this term isn't correct accurate). We can create a nested array - an array that is composed of other arrays.

CodePudding user response:

You can still use same logic; just change how you can call your didIt method:

public static void main(String[] args) {
        int[][] arr = { { 1, 1, 1, 1 },
                { 2, 2, 2, 2 },
                { 3, 3, 3, 3 } };
        
        for(int a[]: arr) {
           System.out.print(didIt(a,a.length));
        }

No need to change anything in didIt.

Also, I assume your didIt which you showed here is for demonstraion purpose, otherwise there is plenty of repeated code in if which you can able to avoid.

  • Related