Respuesta :
Answer:
The program written in Java is as follows
import java.util.Scanner;
public class mutilate {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int n;
System.out.print("Array Length: ");
n = input.nextInt();
while(n<1 || n > 103) {
System.out.print("Array Length: ");
n = input.nextInt();
}
int a []= new int [n];
int b []= new int [n];
System.out.print("Enter Elements of the Array: ");
for(int i =0;i<n;i++) {
a[i] = input.nextInt();
}
System.out.print("Output: ");
for(int i =0;i<n;i++) {
if(i == 0) {
b[i] = 0+a[i]+a[i+1];
}
else if(i == n-1) {
b[i] = a[i - 1]+a[i]+0;
}
else {
b[i] = a[i - 1]+a[i]+a[i+1];
}
System.out.print(b[i]+" ");
}
}
}
Explanation:
This line allows the program accepts user input
Scanner input = new Scanner(System.in);
This line declares integer n
int n;
The next two line prompts user for length of the array and also accepts input
System.out.print("Array Length: ");
n = input.nextInt();
The following while iteration ensures that the user input is between 1 and 103
while(n<1 || n > 103) {
System.out.print("Array Length: ");
n = input.nextInt();
}
The next two lines declares array a and b
int a []= new int [n];
int b []= new int [n];
The next for iteration allows user enter values for array a
System.out.print("Enter Elements of the Array: ");
for(int i =0;i<n;i++) {
a[i] = input.nextInt();
}
The next for iteration calculates and prints the values for array b based on the instruction in the question
System.out.print("Output: ");
for(int i =0;i<n;i++) {
if(i == 0) {
b[i] = 0+a[i]+a[i+1];
}
else if(i == n-1) {
b[i] = a[i - 1]+a[i]+0;
}
else {
b[i] = a[i - 1]+a[i]+a[i+1];
}
System.out.print(b[i]+" ");
}