178k views
4 votes
Make sure to read all of these specifications carefully. Write a function, named array_shifter, that accepts an array of doubles and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.

User Guruprasad
by
5.4k points

1 Answer

2 votes

Answer:

import java.util.Arrays;

public class num2 {

public static void main(String[] args) {

//Create and initialize the first array

double [ ]arr = {3.5, 5.6, 4.5, 6.7};

// Get the length of the array

int n = arr.length;

//Call the method Array Shifter inside the output statement

System.out.println(Arrays.toString(array_shifter(arr,n)));

}

static double [] array_shifter(double [ ] doubleArray, int n){

double [] narr = new double[n+1];

//Loop through from index 1

for(int i =0; i<narr.length-1; i++){

narr[i] = doubleArray[i];

}

//Put a new element at the last index

narr[narr.length-1] = 1.1;

return narr;

}

}

Step-by-step explanation:

This is implemented in Java

Read detailed comments in the solution

The method array_shifter creates a new array that is one element larger than the array it received as argument and returns it

User Rex Kerr
by
5.0k points