Let's say we have 10 students and we need to store their grades are integers. We could define 10 variables say:
grade_student1, grade_student2, grade_student3
We could store our values in these variables. However it is a bit messy to create separate variables to hold 10 similar values that may be used together.
As an example to find the average of the grades we have to do:
int average = ( grade1 + grade2 + grade3 + grade4 ... + grade10 ) / 10
A cleaner option is to use the data structures arrays. We can declare an array as:
int[] studentGrades ;
This states that "studentGrades" can hold integer values. How can we store values in the array ? We can use the array subscript notation. Ex:
studentGrades[0] = 50 ;
studentGrades[1] = 66 ;
studentGrades[9] = 86 ;
Now the average can be coded as :
int average = 0 ;
int total = 0 ;
for( int i1=0 ; i1< 10 ; i1++ )
total += studentGrades[i1]
average = total / 10 ;
File: arr1.cs
using System;
class arr1
{
static void Main(string[] args)
{
int grade_student1= 10 , grade_student2= 20 , grade_student3= 30 , grade_student4 = 40 , grade_student5= 50 ,
grade_student6= 60 , grade_student7= 70 , grade_student8=80, grade_student9=90 , grade_student10 =100 ;
double total = 0 ;
total = grade_student1 +
grade_student2 + grade_student3 + grade_student4 + grade_student5 +
grade_student6 + grade_student7 + grade_student8 + grade_student9 + grade_student10 ;
Console.WriteLine( "{0}", (total / 10.0) );
int[] studentGrades = { 10 , 20 , 30 , 40 , 50 , 60 , 70, 80 , 90, 100 } ;
total = 0 ;
for( int i1=0 ; i1<10 ; i1++ )
{
total = total + studentGrades[i1] ;
}
Console.WriteLine( "Average is : {0}", (total / 10.0) );
}
}
We can initialize an array with values when we declare it. From the above coe:
int[] studentGrades = { 10 , 20 , ...
We can also initialize it with values after the declaration.
File: "arr1a.cs"
using System;
class arr1
{
static void Main(string[] args)
{
int grade_student1= 10 , grade_student2= 20 , grade_student3= 30 , grade_student4 = 40 , grade_student5= 50 ,
grade_student6= 60 , grade_student7= 70 , grade_student8=80, grade_student9=90 , grade_student10 =100 ;
double total = 0 ;
total = grade_student1 +
grade_student2 + grade_student3 + grade_student4 + grade_student5 +
grade_student6 + grade_student7 + grade_student8 + grade_student9 + grade_student10 ;
Console.WriteLine( "{0}", (total / 10.0) );
int[] studentGrades = new int[10] ;
studentGrades[0] = 10 ; studentGrades[1] = 20 ;
studentGrades[2] = 30 ; studentGrades[3] = 40 ;
studentGrades[4] = 50 ; studentGrades[5] = 60 ;
studentGrades[6] = 70 ; studentGrades[7] = 80 ;
studentGrades[8] = 90 ; studentGrades[9] = 100 ;
total = 0 ;
for( int i1=0 ; i1<10 ; i1++ )
{
total = total + studentGrades[i1] ;
}
Console.WriteLine( "Average is : {0}", (total / 10.0) );
}
}
In the above we have used the statement:
int[] studentGrades = new int[10] ;
This states that "studentGrades" is an array of size 10 that can hold integers.The individual elements can be accessed using the notation "[index]" . The indexes start with 0 . For an array of size 10 the indexes will be from 0 to 9 .
File: "arr2.cs"
using System ;
using System.Threading ;
class arr2
{
static void Main()
{
int[] studentGrades = new int[5] ;
studentGrades[0] = 10 ;
studentGrades[1] = 20 ;
for( int i1=0 ; i1<5 ; i1++ )
{
Console.WriteLine( studentGrades[i1] );
} //for
}
}
Output:
10
20
0
0
0
In the above example we initialized only the first 2 elements and printed out the array. The rest of the elements were initialized with default values which for the case of integers is 0 .
File: "arr3.cs"
using System ;
using System.Threading ;
class arr3
{
static void Main()
{
int[] studentGrades = new int[5] ;
studentGrades[0] = 10 ;
studentGrades[1] = 20 ;
for( int i1=0 ; i1<10 ; i1++ )
{
Console.WriteLine( studentGrades[i1] );
} //for
}
}
Output:
10
20
0
0
0
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at arr3.Main()
In the above example we tried to access an index outside the range of 5 and C# throws an Exception.
File: "arr4.cs"
using System ;
using System.Threading ;
class arr4
{
static void Main()
{
int[] studentGrades = new int[5] ;
studentGrades[0] = 10 ;
studentGrades[1] = 20 ;
studentGrades[2] = 30 ;
studentGrades[3] = 40 ;
studentGrades[4] = 50 ;
for( int i1=0 ; i1<5 ; i1++ )
{
Console.WriteLine( studentGrades[i1] );
} //for
foreach( int grade in studentGrades )
Console.WriteLine( grade );
}
}
Output:
10
20
30
40
50
10
20
30
40
50
We can use the "foreach" construct to iterate through the elements in the array. The construct is written as:
foreach( type variable in array )
{
//Do something with the variable
}
The variable should be the same type as the array type. The "variable" takes on the values in the array.
We can print the contents of the array using the "String.Join" method .
Console.WriteLine( String.Join(" ", myarray1 ) );
The "Join" method takes the separator as the first operand and a container as the next operand.
Array Properties and Methods
An array has certain properties an the "Array" class has static helper methods that we can call.
Two Dimensional Arrays
Ex1)
Fill in the code for the function "findMinMax" which will print the minimum and maximum of the integral values in an array.
using System ;
using System.Threading ;
class ex1
{
public static void findMinMax( int[] arr1 )
{
// TO DO
Console.WriteLine( "Min:{0} Max:{1}" , min , max ) ;
}
static void Main()
{
int[] myarray1 = {10,20,30,40,50 } ;
findMinMax( myarray1 ) ;
}
}
Soln1
using System ;
using System.Threading ;
class ex1
{
public static void findMinMax( int[] arr1 )
{
if( arr1.Length == 0 )
return ;
int min = arr1[0] ; int max = arr1[0] ;
for( int i1=1 ; i1< arr1.Length ; i1++ )
{
if ( arr1[i1] < min )
min = arr1[ i1 ] ;
if ( arr1[i1] > max )
max = arr1[ i1 ] ;
}//for
Console.WriteLine( "Min:{0} Max:{1}" , min , max ) ;
}
static void Main()
{
int[] myarray1 = {10,20,30,40,50 } ;
findMinMax( myarray1 ) ;
}
}