126k views
4 votes
Integer numValues is read from input, representing the number of floating-point numbers to be read next. Then, the remaining numbers are read and stored into array scoresArray. Initialize the array modifiedArray to be half the size of scoresArray. Write a loop that iterates through modifiedArray and assigns each element in modifiedArray with the corresponding element in the first half of scoresArray.

Ex: If the input is:

4
67.5 55.0 42.5 52.5
then the output is:

Original scores: 67.5 55.0 42.5 52.5
First half of the scores: 67.5 55.0
Note: Input array always has an even number of elements.

import java.util.Scanner;

public class Score {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double[] scoresArray;
double[] modifiedArray;
int numValues;
int i;

numValues = scnr.nextInt();

scoresArray = new double[numValues];

for (i = 0; i < scoresArray.length; ++i) {
scoresArray[i] = scnr.nextDouble();
}

/* Your code goes here */

System.out.print("Original scores: ");
for (i = 0; i < scoresArray.length; ++i) {
System.out.printf("%.1f ", scoresArray[i]);
}
System.out.println();

System.out.print("First half of the scores: ");
for (i = 0; i < modifiedArray.length; ++i) {
System.out.printf("%.1f ", modifiedArray[i]);
}
System.out.println();
}
}

1 Answer

2 votes

Final answer:

The Java programming question requires initializing an array, modifiedArray, with half the size of scoresArray and then copying the first half of scoresArray into modifiedArray using a loop.

Step-by-step explanation:

The question pertains to a problem in Java programming where an array needs to be manipulated according to certain specifications. In this case, after reading an integer numValues which represents the number of floating-point numbers, the code initializes scoresArray with these numbers. Then, the task is to create another array, modifiedArray, which is half the size of scoresArray and copy the first half of scoresArray into it.

To achieve this, initialize modifiedArray with half the length of scoresArray using the code:

modifiedArray = new double[scoresArray.length / 2];

Next, write a loop to copy the elements:

for (i = 0; i < modifiedArray.length; ++i) {
modifiedArray[i] = scoresArray[i];
}

Now, the first half of scoresArray will be stored in modifiedArray. After this, you can proceed to print out the contents of both arrays as instructed in the problem statement.

User AmbroseChapel
by
7.7k points