susie y
susie y

Reputation: 31

Create new array from method that takes two parameters java

I am new to programming and trying to learn Java and I am trying to do some Java questions that I find quite tough for a beginner. The question asks to write a method that takes a double c and and array v of type double as it's parameters. The method should return a new array of double formed by multiplying all the elements of array v by c.

I really have no idea to do this and if anyone could help on this I'd appreciate it.

I have written some code but I don't understand what I am supposed to do exactly.

public static double times( double c, double [] v)

int i =0;
for( i =0; i < v .length; i++){
myArray =(c * v[i]);
    i++;
}

}

public class Main {

    public static void main(String[] args) {

    double [] v={5.1,5.2,3.0,4.0};
    double c= 4.1;

    System.out.println(times(v,c));

Upvotes: 0

Views: 1232

Answers (4)

Butiri Dan
Butiri Dan

Reputation: 1772

Also be careful what you print. I believe you want to print the new values not the array reference.

public static void main(String[] args) {
        double[] v = {5.1, 5.2, 3.0, 4.0};
        double c = 4.1;
        double[] newV = times(c, v);

        System.out.print("Array address: ");
        System.out.println(newV);
        System.out.print("Array as string: ");
        System.out.println(Arrays.toString(newV));
        System.out.print("Array values for: ");
        for (int index = 0; index < newV.length; ++index) {
            System.out.println(newV[index]);
        }
        System.out.print("Array values foreach: ");
        for (double value : newV) {
            System.out.println(value);
        }
}

Upvotes: 0

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

Read carefully your problem.

I added comments to the code so you understand what you did wrongly:

  // Return a double[] instead of double
  public static double[] times( double c, double [] v)

    // Create a new double array
    double[] myArray = new double[v.length];
    for (int i = 0; i < v.length; i++) {
      // Set each element of the new array equals to the old array element in 
      // The same position multiplied by c
      myArray[i] = c * v[i];   // Parenthesis are not needed here
      // i++ is not needed because you already add 1 to i in the for instruction
    }

    // Return the new array
    return myArray;
 }

Upvotes: 0

mihir KHANDHAR
mihir KHANDHAR

Reputation: 1

The answer mentioned above is correct but you could do the same in the same array i.e double[] v, instead of creating a new array, just for optimization scenario

Upvotes: 0

Strelok
Strelok

Reputation: 51461

It’s a good start but your method should return an array of doubles: double[].

public static double[] times( double c, double [] v)
  double[] myArray = new double[v.length]; // this is a new array
  int i =0;
  for( i =0; i < v .length; i++){
    myArray[i] =(c * v[i]); // assign new values to your array
    //  i++; << don’t need this line as your for loop is already incrementing i
  }
  return myArray;
}

Upvotes: 1

Related Questions