This week's giveaway is in the Android forum. We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line! See this thread for details.

Hello friends. During vivak's javacertification exam I encountered some strange code. If I compile it I get the result but i am not able to find out what it means. So if you know please help me out. Question is ! 1) If arr[] contains only positive integer values, what does this function do? public int guessWhat( int arr[] ) { int x= 0; for( int i = 0; i < arr.length; i++ ) x = x < arr[i] ? arr[i] : x; // A return x; } a) Returns the index of the highest element in the array b) Returns true/false if there are any elements that repeat in the array c) Returns how many even numbers are in the array d) Returns the highest element in the array e) Returns the number of question marks in the array Ans is d: SO what is the meaning of code at A. Thanks in advance.

Hello, Parag. The line you have marked "A" is an example of the ternary operator. You'll see it in the form w = x ? y : z. Here are the rules concerning the ternary operator: "x" must be a boolean expression; "y" and "z" must be compatible and convertible to the same type. Similarly, "w" must be able to accept whatever "y" and "z" are. If "x" evaluates true, "y" is assigned to "w"; if "x" evaluates false, on the other hand, it is "z" that is assigned to "w". In your case, here is the body of your for loop:

What this statement says is, "If the current element of this array is greater than the value stored in placeholder variable x, then update x to be the current element of this array. Otherwise, leave x as is." As you cycle through the array, x will come to contain the largest element of the array (if, as the question states, the array is composed entirely of positive integer values). As a matter of fact, Parag, "( a < b ) ? b : a " is equivalent to "Math.max( a, b )". Notice, too, Parag, that since there are no curly braces following the for loop, only the line "x = x < arr[i] ? arr[i] : x;" statement is considered the body of the for loop; "return x;" only executes after the for loop has run its course. Hope this helps you, Parag. Art

Hey Parag, Lets put it in more simple way: public int guessWhat( int arr[] ) { int x= 0; for( int i = 0; i < arr.length; i++ ) x = x < arr[i] ? arr[i] : x; // A return x; } Code at A: Lets take a example Say arr[]={1,2,3,4} It means array has 4 elements and array length is 4. Now lets go through for loop first iteration for( int i = 0; i < arr.length; i++ ) x = x < arr[i] ? arr[i] : x; // A return x; //x=x<arr[0]?arr[0]:x;> Going from left to right x is assigned to x whihc is equivalent to arr[i] if arr[i] is greater than x and if not then it is equivalent to x itself. In this case arr[0] is 1 whihc is greater than original value of x which iz zero so x is 1 In second loop x is 1 but the arr[1] is 2 whihc is greater in third loop x is 2 but arr[2] is 3 whihc is greater in fourth loop x is 3 but arr[3] is 4 whihc is greater and therfore x becomes 4 whihc is ultimately returned.

I hope this makes it clear.

parag bharambe
Ranch Hand

Joined: Sep 01, 2000
Posts: 41

posted

0

Hi That's really lucid explination Art Metzer and hmehta. I got the concept. Thanks once again. Parag