QUESTION IMAGE
Question
given the integer array dailyscores with arr_vals elements, write a for loop to output the integers in the second half of dailyscores. separate the integers with a dash surrounded by spaces (\ - \).
ex: if the input is 47 60 82 68, then the output is:
82 - 68
1 import java.util.scanner;
2
3 public class scoreinfo {
4 public static void main(string args) {
5 scanner scnr = new scanner(system.in);
6 final int arr_vals = 4;
7 int dailyscores = new intarr_vals;
8 int i;
9
10 for (i = 0; i < dailyscores.length; ++i) {
11 dailyscoresi = scnr.nextint();
12 }
13
14 for (/ your code goes here /) {
15 / your code goes here /
16 }
Step1: Determine the starting index of the second half
The starting index of the second half of an array with n elements is n/2. In Java, for an array dailyScores, the length is dailyScores.length. So the starting index is dailyScores.length / 2.
Step2: Iterate through the second half
Use a for loop to iterate from the starting index (inclusive) to the end of the array (exclusive). For each element in this range, print the element. If it is not the last element, also print " - ".
for (i = dailyScores.length / 2; i < dailyScores.length; ++i) {
System.out.print(dailyScores[i]);
if (i < dailyScores.length - 1) {
System.out.print(" - ");
}
}Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
for (i = dailyScores.length / 2; i < dailyScores.length; ++i) {
System.out.print(dailyScores[i]);
if (i < dailyScores.length - 1) {
System.out.print(" - ");
}
}