Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming with Arrays Write a short program to calculate the second derivative

ID: 3800621 • Letter: P

Question

Programming with Arrays

Write a short program to calculate the second derivative of an array of doubles. Starting with
Lab.java (shown below), finish the program to compute and print the
discrete second derivative of the array read in by adding code everywhere it says // [Add code here]. The first line of input will tell how many values are in the array.
Next, the data points will be give, one per line.

import java.util.*;
public class Lab
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);


// Read in the number of data points
int numDataPoints = scan.nextInt();


// Create an array to hold the data points, and another to
// hold the second derivative
double data[] = new double[numDataPoints];
double secondDerivative[] = // [Add code here]


// Read in all of the data points using a for loop
// [Add code here]


// Print out the data using printArray
printArray(data);
System.out.println();


// Create the second derivatives and store them in the
// secondDerivative array.
// (Since the first and last elements of the array do
// not have neighbors, set their second derivatives to 0.)
// [add code here]


// Print the second derivative array by calling printArray
// [add code here]
}


public static void printArray(double[] arr)
{
// Print the values of arr on a single line with spaces between them.
// [Add code here]
}
}


Entering Data and Running the Program
Now we need some data. Type in, or copy the following input file, and call it “labinput.txt”:
9
1.1
2.2
3.3
2.2
1.1
7.0
8.0
9.1
10.0
Run your second derivative program with the input file you have just entered:
java Lab < labinput.txt
Run the program again, redirecting (use >) the output to labout.txt.

Explanation / Answer

Scanner sc=new Scanner(System.in);

System.out.print("Enter the d value: ");

int d = sc.nextInt();

System.out.print("Enter "+(d+1)+" coefficients: ");

double[] C = new double[d+1];

for(int i=0; i<C.length;i++) {

    C[i]=sc.nextDouble();

}

double derv[] = new double[C.length-1];

for(int i=0;i<derv.length;i++){

    derv[i] = C[i]*(C.length - 1 -i );

}