Reputation: 25
Here I need to write a method called powArray that takes a double array a
and returns a new array that contains the elements of a
squared. Generalize it to take a second argument and raise the elements of a
to given power.
I tried to write it, but got 0 result, can someone write this and explain how it work, so I could write it in future.
public class Task {
public static void main(String[] args) {
}
public static double powArray (double a[]){
for (int i = 1; i < a.length; i++) {
a[i] = Math.pow(a[i], 2.0);
System.out.print(a[i]);
return powArray(a);
}
return -1;
}
}
Also, after compile got nothing, only clear console w/o any numbers, etc.
Upvotes: 0
Views: 12922
Reputation: 59978
It seams you are making a recursive method because you call your method in the return return powArray(a);
, so this will call your method again and again until your condition is wrong and return -1.
... i need to write a method called powArray that takes a double array, a, and returns a new array
The first part is correct method called powArray that takes a double array, but the second is not return an array it return a double, instead your method signature should look like :
public static double[] powArray (double a[]){
//------------------^^
Now the return part should be like so ;
public static double[] powArray (double a[]){
for (int i = 0; i < a.length; i++) {
a[i] = Math.pow(a[i], 2.0);
}
return a;//<<-------------return the result
}
Test
If you try to make this call in your main method :
public static void main(String[] args) {
double[] a = new double[]{5, 6, 8, 7};
System.out.println(Arrays.toString(powArray(a)));
}
The output :
[25.0, 36.0, 64.0, 49.0]
Upvotes: 1
Reputation: 845
As you said, " i need to write a method called powArray that takes a double array,a, and returns a new array that contains the elements of a squared. " You are need to improve following things,
Take a following example:
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] squareArry = powArray(new double[]{10,20,30});
}
public static double[] powArray (double a[]){
double[] b = new double[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = Math.pow(a[i], 2.0);
System.out.print(b[i]);
}
return b;
}
If you want to use recursion then returning an array not required, because you will change the value of array passed itself. Take a close look of following example with recursion:
public static void main(String[] args) {
// TODO Auto-generated method stub
double a[] = new double[]{2,5,4,6};
powArray(a,0);
for(double value:a){
System.out.println(value);
}
}
public static void powArray(double a[], int index) {
if (index < a.length) {
a[index] = Math.pow(a[index], 2.0);
powArray(a, ++index);
}
}
Upvotes: 0
Reputation: 353
Adding to the Sandeep Kakote's answer :
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] squareArry = powArray(new double[]{10,20,30},3);
}
public static double[] powArray (double a[],int z){
double[] b = new double[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = Math.pow(a[i], z);
System.out.print(b[i]);
}
return b;
}
Upvotes: 1
Reputation: 460
You can use the following snippet below, but this does not take into consideration your generic case, that you will pass the power, currently it will do for power of 2.
public class Task {
public static void main(String[] args) {
double[] arr = {2.0,3.0,4.0};
powArray(arr);
for(double a : arr){
System.out.println(a);
}
}
public static void powArray (double a[]){
for (int i = 0; i < a.length; i++) {
a[i] = Math.pow(a[i], 2.0);
}
}
}
The main method should not be empty as it is the starting point. Since powArray is a static method it can be called directly in your main method. The powArray should return a double array but it is just returning a double Your for loop starts from 1 but goes to arraylength - 1 which will run for n-1 elements instead of n elements Within each loop iteration you are returning and the return statement is making a recursive call(Calling the same method again, which will result in stack overflow)
Upvotes: -1