230k views
4 votes
Assume that x is a variable that has been declared as an int and been given a value. Assume that twice is a method (in the same class as the caller) that receives a single integer parameter and returns twice its value. (So if you pass 7 to twice it will return 14. Thus the expression twice(7) has the value 14. Write an expression whose value is eight times that of x without using the standard Java arithmetic operators (*,+, etc.). Instead, use calls to twice to accomplish this. In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the twice() function-- no other functions or operators.

1 Answer

2 votes

Answer:

Arrays are indexed from 0 in pretty much every language, except stupid Microsoft languages.

Lets say that ARR_SIZE is 5 for example.

So you have an array of 5 elements. They are indexed from 0 - 4.

arr[0] // First element

arr[4] // Last element

Therefore arr[ ARR_SIZE -1 ] is the last element of the array.

That's why for loops to iterate through arrays are wrote like:

for(int i = 0; i < ARR_SIZE; ++i)

User Sysanin
by
4.5k points