Suppose that “final” integer SIZE has been properly declared and initialized, and
that an array named sample has been declared as follows:
int[] sample = new int[SIZE];
Write one or more Java statements to perform the following. Note that you cannot
change the above declaration and that each task is independent of the other three:
(a) Initialize all array elements to the value 5.
(b) Swap the first value in the array with the last value in the array
(c) Change any negative values to positive values (of the same magnitude)
(d) Print the contents of the locations in the array with odd-numbered index
1
Expert's answer
2017-08-01T06:09:06-0400
Answers:
a) for (int i=0; i<SIZE; i++) sample[i] = 5;
b) int temp = sample[0]; sample[0] = sample[SIZE-1]; sample[SIZE-1] = temp;
c) for (int i=0; i<SIZE; i++) if (sample[i] < 0) sample[i] = -sample[i];
d) for (int i=1; i<SIZE; i+=2) System.out.println(sample[i]);
Comments
Leave a comment