(a is some array (any type) ) int i; for ( i = 0; i < a.length; i++ ) { // statements in the for-loop body will be // executed ONCE for each array element a[i] } |
to:
|
|
Make a note of this when we go through the examples !
|
|
sum = 0.0; for all elements a[0], a[1], a[2], ... of array a do { add a[i] to sum; } print sum |
public class SumArray1 { public static void main(String[] args) { double[] a = { 2.3, 3.4 , 4.5, 5.6, 6.7, 7.8, 8.9 }; // 7 elements int i; // array index double sum; // Running sum for ( i = 0 ; i < a.length ; i++ ) { sum = sum + a[i]; // Add a[i] to the running sum // Common error: sum = a[i] // This will store a[i] into sum } System.out.println( sum ); } } |
How to run the program:
|
import java.util.Scanner; public class SumArray2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; double[] a; System.out.print("How many numbers in input: "); n = in.nextInt(); // n = # values a = new double[n]; // Create an array of n elements int i; /* ------------------ Read in the values ------------------ */ for ( i = 0; i < a.length; i++ ) { System.out.print("Enter a number: "); a[i] = in.nextDouble(); // Read in number } /* ------------------ Compute the sum ------------------ */ double sum; sum = 0.0; for ( i = 0; i < a.length; i++ ) { sum = sum + a[i]; // Add a[i] to the running sum // Common error: sum = a[i] // This will store a[i] into sum } System.out.println( sum ); } } |
The program section in red takes care of reading in the input data into the array a
How to run the program:
|
|
|
|
Examples:
|
|
Solution:
|
Example:
min = a[0]; // Assume a[0] is the minimum for all elements a[1], a[2], ... of array a do { if ( a[i] < min ) { min = a[i]; // We found a smaller minimum } } print min; |
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 ); } } |
How to run the program:
|
|
|
Modified solution:
|
Example:
min_i = 0; // Assume elem 0 (a[0]) is the minimum for all element a[1], a[2], ... of array a do { if ( a[i] < a[min_i] ) { min_i = i; // We found a smaller minimum, update min_i } } print min_i; |
public class MinArray2 { 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 int min_i; // Current index with min value min_i = 0; // Assume a[0] is the current min. value for ( i = 1 ; i < a.length ; i++ ) { if ( a[i] < a[min_i] ) { min_i = i; // Found a smaller min. value, update min_i } } System.out.println( min_i ); } } |
Note:
|
How to run the program:
|
|
|
Facts:
|
public class MinArray2 { 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 int min_i; // Current index with min value min_i = 0; // Assume a[0] is the current min. value for ( i = 1 ; i < a.length ; i++ ) { if ( a[i] < a[min_i] ) // ****** Inefficient ****** { min_i = i; // Found a smaller min. value, update min_i } } System.out.println( min_i ); } } |
We can imporve the efficiency by use a simple variable (min) to store a[min_i] and use that variable in the comparison:
public class MinArray3 { 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 int min_i; // Current index with min value double min; // min = a[min_i] for efficiency min_i = 0; // Assume a[0] is the current min. value min = a[0]; // For efficiency for ( i = 1 ; i < a.length ; i++ ) { if ( a[i] < min ) { min_i = i; // Found a smaller min. value, update min_i min = a[i]; // For efficiency } } System.out.println( min_i ); } } |
Note:
|
How to run the program:
|