Chapter 34 More with Data Structures

34.1 Simple Exercises with Arrays

Exercise 34.1-1 Creating an Array that Contains the Average Values of its Neighboring Elements

Write a Java program that lets the user enter 100 positive numerical values into an array. Then, the program must create a new array of 98 elements. This new array must contain, in each position the average value of the three elements that exist in the current and the next two positions of the user-provided array.

Solution

Let's try to understand this exercise through an example using 10 elements.

Image

Array newArr is the new array that is created. In array newArr, the element at position ...

Get Java and Algorithmic Thinking for the Complete Beginner now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.