(See: click here )
(See: click here )
|
public class MinArray1 { public static void main(String[] args) { double[] a = { 2.3, 3.4 , 4.5, 5.6, 1.2, 7.8, 8.9 }; // 7 elements int i; // array index double min; // Current min value min = a[0]; // Initial min. value for ( i = 1 ; i < a.length ; i++ ) { if ( a[i] < min ) { min = a[i]; // Found a smaller min. value } } System.out.println( min ); } } |
|
|
Example:
public class MinArray1 { public static void main(String[] args) { double[] a = { 2.3, 3.4 , 4.5, 5.6, 1.2, 7.8, 8.9 }; // 7 elements double[] b = { -8.8, 9.7, -14.6, 89.8 }; // 4 elements int i; // array index double min; // Current min value /* ----------------- min of array a -------------------- */ min = a[0]; // Initial min. value for ( i = 1 ; i < a.length ; i++ ) { if ( a[i] < min ) { min = a[i]; // Found a smaller min. value } } System.out.println( min ); /* ----------------- min of array b -------------------- */ min = b[0]; // Initial min. value for ( i = 1 ; i < b.length ; i++ ) { if ( b[i] < min ) { min = b[i]; // Found a smaller min. value } } System.out.println( min ); } } |
How to run the program:
|
Output of the program:
1.2 -14.6 |
|
Requirement:
|
|
public static double minArray( double[] a ) { int i; // array index double min; // Current min value min = a[0]; // Initial min. value for ( i = 1 ; i < a.length ; i++ ) { if ( a[i] < min ) { min = a[i]; // Found a smaller min. value } } return(min); // Return the min found (instead of printing it) } |
Explanation:
|
public static void main(String[] args) { double[] a = { 2.3, 3.4 , 4.5, 5.6, 1.2, 7.8, 8.9 }; // 7 elements double[] b = { -8.8, 9.7, -14.6, 89.8 }; // 4 elements int i; // Array index double min; // Current min value /* ----------------- min of array a -------------------- */ min = minArray( a ) ; // Find min value in array a System.out.println( min ); /* ----------------- min of array b -------------------- */ min = minArray( b ) ; // Find min value in array b System.out.println( min ); } |
public class ArrayParam1 { public static double minArray( double[] a ) { int i; // array index double min; // Current min value min = a[0]; // Initial min. value for ( i = 1 ; i < a.length ; i++ ) { if ( a[i] < min ) { min = a[i]; // Found a smaller min. value } } return(min); // Return the min found (instead of printing it) } public static void main(String[] args) { double[] a = { 2.3, 3.4 , 4.5, 5.6, 1.2, 7.8, 8.9 }; // 7 elements double[] b = { -8.8, 9.7, -14.6, 89.8 }; // 4 elements int i; // Array index double min; // Current min value /* ----------------- min of array a -------------------- */ min = minArray( a ) ; // Find min value in array a System.out.println( min ); /* ----------------- min of array b -------------------- */ min = minArray( b ) ; // Find min value in array b System.out.println( min ); } } |
How to run the program:
|
We learned in this webpage that:
|
public class SimpleParam { public static void updateParam( double x ) { x = 9999; // Update parameter variable } public static void main(String[] args) { double a = 2.3; System.out.println("Variable before calling updateArray:"); System.out.println( a ); // Prints 2.3 updateParam( a ); // ****** Call updataParam System.out.println("Variable AFTER calling updateArray:"); System.out.println( a ); // Prints 2.3 } } |
Output:
2.3 2.3 (did not update the actual parameter) (This is the pass-by-value mechanism) |
How to run the program:
|
public class ArrayParam2 { public static void updateParam( double[] x ) { x[0] = 9999; // Update one of the array element } public static void main(String[] args) { double[] a = { 2.3, 3.4 , 4.5 }; System.out.println("Array before calling updateParam:"); for (int i = 0; i < a.length; i++) System.out.println( a[i] ); updateParam( a ); // Call updateParam System.out.println("Array AFTER calling updateParam:"); for (int i = 0; i < a.length; i++) System.out.println( a[i] ); } } |
$64,000 question:
|
Output:
Array before calling updateArray: 2.3 3.4 4.5 Array AFTER calling updateArray: 9999.0 3.4 4.5 |
Can you explain why a[0] was updated ???
How to run the program:
|
|
|
The array a[0] is also changed because it is the same array element !!!!
|