| Example:
Fill an
array with 10 integer values. Pass the array to a method that will add up
the values and return the sum. (In this example, no original data
in the array was altered. The array information was
simply used to find the sum.)
import java.io.*;
import BreezyGUI.*;
public class FindSum
{
public static void main (String [ ] args)
{
int [ ]
number = new int [ 10];
// instantiate the array
int i;
int sum=0;
for ( i = 0; i < 10; i++ )
// fill the array
number[ i ] =
Console.readInt("Enter number: " );
int sum = find_sum(number);
// invoke the method
System.out.println("The sum is" +sum
+ ".");
}
public static
int find_sum(int [ ]
value) //method definition to find sum
{
int i, total = 0;
for(i=0; i<10; i++)
{
total = total + value[ i ];
}
return (total);
}
|