192k views
0 votes
Assume the array "arr" has been declared, and its size is defined as an integer equal to the number of elements in "arr." Write a statement that assigns to variable "x" the value of the next-to-last element of the array. What is the correct statement?

a) x = arrarr_size - 1
b) x = arrarr_size - 2
c) x = arrarr_size
d) x = arrarr_size + 1

1 Answer

3 votes

Final answer:

The correct statement to assign to variable 'x' the value of the next-to-last element of the array 'arr' is: x = arr[array_size - 2]. Arrays are zero-indexed; hence subtracting 2 gives the next-to-last element index.

Step-by-step explanation:

To assign to variable x the value of the next-to-last element of the array 'arr' when the size of the array is given by the integer 'array_size', you would use the following statement:

x = arr[array_size - 2];

Arrays in most programming languages are zero-indexed, which means the first element is at index 0, the second element is at index 1, and so on. Therefore, to access the next-to-last element, you need to subtract 2 from the array size. The correct statement is thus option b) x = arr[array_size - 2].

User SmiffyKmc
by
7.9k points