Java Reference
In-Depth Information
int
[] v=
;
System . out . println (
"Seeking for element 6: Position "
+
DichotomicSearch(v,6) ) ;
System . out . println (
"Seeking for element 80: Position "
+
DichotomicSearch(v,80) ) ;
System . out . println (
"Seeking for element 33: Position "
+
DichotomicSearch(v,33) ) ;
{
1,6,9 ,12
,45 , 67, 76, 80, 95
}
}
}
We get the following console output:
Seeking for element 6: Position 1
Seeking for element 80: Position 7
Seeking for element 33: Position -1
4.8 Exercises
Exercise 4.1 (Array of strings)
Write a static function
DisplayArray
that reports the number of
elements in an array of strings, and displays in the console output
all string elements. Give another function
DisplayReverseArray
that
displays the array in reverse order.
Exercise 4.2 (Pass-by-value array arguments)
Explain why the following
permute
function does
not
work:
Program 4.17
Permuting strings and Java's pass-by-reference
class
ExoArray
{
static void
permute( String s1 ,
String s2 )
{
String tmp=s1 ;
s1=s2 ;
s2=tmp;
}
public static void
main( String args [ ] )
{
String [] array=
{
"shark"
,
"dog"
,
"cat"
,
"crocodile"
}
;
permute(array [0] , array [1]) ;
System . out . println ( array [0]+
""
+array [1]) ;
}
}
Give a static function
static void permute(String [] tab, int i,
int j)
that allows one to permute the element at index position
i
with
Search WWH ::
Custom Search