-7
public class part2 {
    public static void main(String[] args) {
        int[][] a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int[][] b = {{10, 12, 13}, {14, 15, 16}, {17, 18, 19}};
        double[][] x = matrix(a, b);
        for (int i = 0; i < x.length; i++) {
            System.out.println(x[i][i]);
        }


    }

    public static double[][] matrix(int[][] a, int[][] b) {
        double[][] c = new double[b.length][b[0].length];
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[0].length; j++) {
                c[i][j] = (4 * a[i][j]) / (3 * b[i][j]);
            }
        }
        return c;
    }
}

I'm stuck with these simple code so I'm trying to access to my method and print out the result can someone please help me about this?

My Result has to be like this 0.13 0.22 0.31,0.38 0.44 0.50,0.55 0.59 0.63 the code has to multiply array a with 4 and array b with 3 then has to devide them to get these results

THEIMPe
  • 11
  • 2
  • 1
    Java != JavaScript – VLAZ Mar 21 '19 at 14:42
  • 2
    I get the output `0.0 0.0 0.0`. – luk2302 Mar 21 '19 at 14:45
  • 1
    What is the problem exactly? How do you recognize that there is *something* wrong with it? Are you getting any compilation error, exception at runtime, or unexpected results (what result did you expect, why, and what you see instead)? – Pshemo Mar 21 '19 at 14:45
  • 2
    Your program produces output when I run it. – khelwood Mar 21 '19 at 14:45
  • 1
    When you say the result is not printing, what does that mean? By my account, it will print all zeros. – Vinay Avasthi Mar 21 '19 at 14:45
  • My Result has to be like this `0.13 0.22 0.31,0.38 0.44 0.50,0.55 0.59 0.63` the code has to multiply array a with 4 and array b with 3 then has to devide them to get these results – THEIMPe Mar 21 '19 at 14:50

3 Answers3

2

Look at following line.

        c[i][j]=(4*a[i][j])/(3*b[i][j]);

What you are doing is integer division and your denominator is alway greater than numerator. So answer will always be all zeros.

Vinay Avasthi
  • 849
  • 5
  • 13
  • While this explains the problem, it would be nice to also provide some solution like using `4.0` or `4.` or `4d` instead of 4. – Pshemo Mar 21 '19 at 14:53
  • Thank you for your help about this denominator and numerator thing I missed that :) – THEIMPe Mar 21 '19 at 14:56
0

Your problem is that you are storing a integer division value in a double datatype. As you may know an integer can not store any decimals. A quick fix you could do something like this.

c[i][j]=(4.0*a[i][j])/(3.0*b[i][j]);
0

You need to change

c[i][j] = (4 * a[i][j]) / (3 * b[i][j]);

with

c[i][j] = (double)(4 * a[i][j]) / (3 * b[i][j]);

you'll get in the output:

0.13333333333333333
0.4444444444444444
0.631578947368421
Mahdi Khardani
  • 54
  • 1
  • 10