I'm trying to write a java program to test following code
12,8,23,6,5,17,20,9
m=2;
n=0;
while m<array length
repeat
ar[m]=ar[n]+ar[n+1];
m++;
n++;
End while
So this was my source code
public class Main {
public static void main(String [] args) {
int []x= {12,8,23,6,5,17,0,9};
int m=2;
int n=0;
while(m<x.length) {
x[m]=x[n]+x[n+1];
m++;
n++;
System.out.println(x);
}
}
}
And it generated something like this as the output
[I@3d4eac69
[I@3d4eac69
[I@3d4eac69
[I@3d4eac69
[I@3d4eac69
[I@3d4eac69
Also this was my teacher's answer
public class Main {
public static void main(String [] args) {
int []x= {12,8,23,6,5,17,0,9};
int []y=algo(x); **//need an explanation**
}
public static int[] algo(int[]ar) { **//need an explanation**
int m=2;
int n=0;
while(m<ar.length) {
ar[m]=ar[n]+ar[n+1];
m++;
n++;
}
return ar;
}
}
However there is no generated output for teacher's code.However answer should be something like this 12 8 20 28 48 76 124 200 Please explain which one is the best code and how do they works.Also I have commented some lines of my teacher's code where I can't understand . Please explain them too.