ECE 161 WEEK 4









What arrays are.
Using arrays
Declaring an array initializing and referring to
individual elements of an array
Usage of for statement in through arrays
Passing arrays to methods.
Multidimensional arrays
Variable length methods
Reading command line into the program
1
ECE 161 WEEK 10
Arrays
30.11.05
Prof Dr. Ziya B. Güvenç
Res. Asst. Barbaros Preveze
Arrays
 Array is a group of variables containing elements that all have the same
type.
 Each element of the array is called index or subscript.
 The index must be a nonnegative number.
 Starts with number 0
 Shown by [ … ]
 A program can have more than one arrays.
 A value can be assigned to any index of array by writing
a) c[6 ]= 7 or
c[2+4] =7 or
b) a=3;
b=4;
c[a+b] =7
Name of the array
c[0]=-5
[1]=2
c[2]=-35
c[3]=4
c[4]=65
c[5]=83
c[6]=92
c[7]=81
c[8]=1
c[9]=2
c[10]=3
c[11]=4
2
Index of the array
If we want to add first (c[0]) and third (c[2]) index contents we write
Sum= c[0] +c[2];
Or if we want to assign the sum of second (c(1)) and seventh(c(6)) index
contents to eleventh (c[10] ) index.
Declaring and creating arrays
Since array is a class type we have to create an object from this class. But
before this, we have to declare an array variable in order to assign the array that
we will create.
i.e.
int c[ ] = new int[ 12 ];
or
int c[ ] = new int[12];
type of each array index is declared as integer.
Note that :
double[ ] array1,array2 ;
is equivalent to
double array1[ ];
double array2[ ];
The example programs below implements arrays for different purposes.
3
The program below creates an array called “array”, and initializes each index to
zero (0).
public class InitArray
{
public static void main( String args[] )
{
int array[];
array = new int[ 10 ];
// int array[] = new int[10];
System.out.printf( "%s%8s\n", "Index", "Value" );
for ( int counter = 0; counter < array.length; counter++ )
System.out.printf( "%5d%8d\n", counter, array[ counter ] );
}
4
The array below uses an initializer to initialize the array elements.
public class InitArray
{
public static void main( String args[] )
{
// initializer list specifies the value for each element
int myarray[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
// output each array element's value
for ( int counter = 0; counter < myarray.length; counter++ )
System.out.printf( "%5d%8d\n", counter, myarray[ counter ] );
} // end main
} // end class InitArray
5
The program below creates an array with 10 elements , sets each items to
2,4,6,8,10, ….by using for statement and prints them to the screen.
public class InitArray
{
public static void main( String args[] )
{
final int ARRAY_LENGTH = 10; // constant
int array[] = new int[ ARRAY_LENGTH ]; // create array
// calculate value for each array element
for ( int counter = 0; counter < array.length; counter++ )
array[ counter ] = 2 + 2 * counter;
System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
// output each array element's value
for ( int counter = 0; counter < array.length; counter++ )
System.out.printf( "%5d%8d\n", counter, array[ counter ] );
} // end main
} // end class InitArray
6
The program below adds each elements of the array and display the total value
on the screen.
public class SumArray
{
public static void main( String args[] )
{
int array[] = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };
int total = 0;
// add each element's value to total
for ( int counter = 0; counter < array.length; counter++ )
total += array[ counter ];
System.out.printf( "Total of array elements: %d\n", total );
} // end main
} // end class SumArray
7
The program below draws a grade distribution (curve ) of a class, for a set of
given grades.
public class BarChart
{
public static void main( String args[] )
{
int array[] = { 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 };
System.out.println( "Grade distribution:" );
// for each array element, output a bar of the chart
for ( int counter = 0; counter < array.length; counter++ )
{
// output bar label ( "00-09: ", ..., "90-99: ", "100: " )
if ( counter == 10 )
System.out.printf( "%5d: ", 100 );
else
System.out.printf( "%02d-%02d: ",
counter * 10, counter * 10 + 9 );
// print bar of asterisks
for ( int stars = 0; stars < array[ counter ]; stars++ )
System.out.print( "*" );
System.out.println(); // start a new line of output
} // end outer for
} // end main
} // end class BarChart
8
The program bellows rolls a dice randomly 6000 times calculates the frequency
of each face and displays the results on the screen.
import java.util.Random;
public class RollDie
{
public static void main( String args[] )
{
Random randomNumbers = new Random(); // random number generator
int frequency[] = new int[ 7 ]; // array of frequency counters
// roll die 6000 times; use die value as frequency index
for ( int roll = 1; roll <= 6000; roll++ )
++frequency[ 1 + randomNumbers.nextInt( 6 ) ];
System.out.printf( "%s%10s\n", "Face", "Frequency" );
// output each array element's value
for ( int face = 1; face < frequency.length; face++ )
System.out.printf( "%4d%10d\n", face, frequency[ face ] );
} // end main
} // end class RollDie
9
Lab work :
Write a program that
 Creates an array called “myclass” with 50 elements (i.e. 50 indexes )
 Generates a rundom number from 0 to 100
 Assigns randomly generated grades (from 0 to 100) to these indexes
 Calculates and displays the average of the class
 Draws the grade distribution curve of this class.
We will continue with “Arrays” next week …
10