Arrays part 1
Overview of Topics
Declaring Arrays
Loading Arrays
Partially Filled Arrays
Array Defined
An array is used to process a collection
of data all of which is of the same data type (Integer, Decimal, String, etc.). First well look at single dimensional arrays (one column, many rows). Think of a single dimensional array as a list of variables.
Declaring Array
int intQty1, intQty2, intQty3; int[ ] intQty = new int[3]; //3 integer variables
memory to allocate. Arrays will usually be class-level because after values are loaded in we dont want to lose the values.
5
Array Subscript
dataType[ ] arrayName = new dataType[arraySize];
Arrays are allocated consecutive memory. Each element is referenced using a subscript. Subscript are integers. The number of elements that are created is arraySize. The first element in the array is referenced with a value of zero [0]. The last element is referenced with a subscript value of [arraySize 1]. A subscript is also referred to as an index. Short variable names for subscripts are acceptable.
6
Memory Map
Address 1010 1020 1030 Variable intQty1 intQty2 intQty3 Value 0 0 0
1040
1050 1060 1070
intQty[0]
intQty[1] intQty[2] decPrice
0
0 0 0
7
referenced an element past the end of the array, the program would throw an exception (run-time error). The programmer must make sure that the logic in the program does not allow the subscript to exceed the array size. This is called being out of range.
8
it will abort the execution when it happens. It aborts because the subscript would be referencing some other section of memory than what was allocated for the array. The programmer is responsible for preventing the out range error. We can use some of the Array Class properties and methods to manage arrays, arrayName.Length arrayName.GetUpperBound(0). The For Each (foreach) command may also be used to walk through arrays.
9
Array Processing
1. 2.
3.
Process Array
Use individual elements in calculations or as arguments. Send entire arrays to methods for processing. Sort, Search, Display Use a lot of For loops or For Each loops.
11
Declare Array
//Arrays are declared at the class level, //so they can be referenced by all methods. int[ ] cintTestScores = new int[20]; int cintNumberOfStudents; //We can load up to 20 scores, but we will //save how many tests are actually loaded in cintNumberOfStudents.
12
13
Load Array
//Loads Array with scores saved in a data file. private void btnLoadArray_Click( ) { FileStream studentFile = new FileStream("cs12ex.txt", FileMode.Open); StreamReader studentStreamReader = new StreamReader(studentFile); int i = 0; //subscript initialized to zero
cs12ex.txt 50 40 100 30 10 20
while (studentStreamReader.Peek() != -1) { if (i < cintTestScores.Length) { cintTestScores[i] = int.Parse(studentStreamReader.ReadLine( )); i ++; //Increment subscript by one } else { MessageBox.Show (Array Size Exceeded); break; //Get of out of loop; Array is full. } } cintNumberOfStudents = i; //Save how many students were loaded studentFile.Close( ); //Close file }
15
Loaded Arrays
Position 1 2 3 4 5 6 7 20 Address 1010 1014 1018 1022 1026 1030 1034 1086 Index [0] [1] [2] [3] [4] [5] [6] [19] Value 50 40 100 30 10 20 0 0
16
19
were filled by going up to GetUpperBound(0). Up to 20 scores could be loaded, but in the example only 6 scores were actually loaded. When the array is not full, it is considered a partially filled array. The for loops need to be modified to only process the number scores loaded. The number of scores loaded are counted in the load routine, and the count should then be saved in a variable like cintNumberOfStudents. This variable should then be used when processing the arrays.
20
for (i = 0; i < cintNumberOfStudents; i++) //process partially filled array { intSum += cintTestScores[i]; } txtAverage.Text = (intSum / cintNumberOfStudents).ToString(N0);
22
loaded at declaration. When the values are provided at declaration, do not include the size. The size is determined by the number of values provided. The values are enclosed in braces and not parenthesis. decimal[ ] cdecPERCENT_RANGE = new decimal[ ] {90D, 80D, 70D, 60D, 0D};
string[ ] cstrLETTER_GRADE = new string[ ] {"A", "B", "C", "D", "F"};
C# does NOT allow const for arrays.
23
Summary
Declaring Arrays
Loading Arrays
Partially Filled Arrays
24