Docsity

C# Arrays: Declaration, Loading, Processing, Sorting, and Searching, Slides of C programming

Partial preview of the text

Download C# Arrays: Declaration, Loading, Processing, Sorting, and Searching and more Slides C programming in PDF only on Docsity!

CIS162AD - C#

Arrays – Part 2

13_arrays_sorting.ppt

Overview of Topics

  • Declaring Arrays
  • Loading Arrays
  • Processing Arrays
  • Sorting Arrays
  • Searching Arrays
  • Parallel Arrays
  • Multi-dimensional Arrays

Array Management

  • If during execution, the subscript value referenced an element past the end of the array, the program would throw a subscript out range 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.
  • We can use some built in methods to manage arrays, arrayName.Length arrayName.GetUpperBound(0)
  • For partially filled arrays we must count how many items are loaded and then use the count when processing the arrays.

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.

Array Memory Map

Position Address Index Value

1 1010 [0] 0

2 1014 [1] 0

3 1018 [2] 0

4 1022 [3] 0

5 1026 [4] 0

6 1030 [5] 0

7 1034 [6] 0

… … … …

20 1086 [19] 0

Loading Partially Filled Arrays

//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 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 }

cs12ex.txt 50 40 100 30 10 20

Partially Filled Arrays

  • Up to 20 scores could be loaded, but only 6

scores are actually loaded.

  • When the array is not full, it is considered a

partially filled array.

  • When processing arrays, we need to make

sure we only process the number scores

loaded.

  • The number of scores loaded were counted in

the load routine, and the count was saved in

cintNumberOfStudents.

Sorting Arrays

  • Data is always being sorted.
  • It is important that we understand how sorts work.
  • There are various sort algorithms.
  • We’ll only be looking at the simple selection sort.
  • Selection Sort Algorithm:
    • Find the lowest value in the array.
    • Move it to the top of the array.
    • Find the next lowest value, and move it to the 2 nd position.
    • Continue until the end of the array is reached.

Sort Algorithm Demonstration

const int intARRAY_SIZE = 20; int[ ] cintTestScores = new int[intArraySize]; int cintNumberOfStudents;

Declared Array - cintTestScores

Address Subscript Value 1010 [0] 0 1014 [1] 0 1018 [2] 0 1022 [3] 0 1026 [4] 0 1030 [5] 0 1034 [6] 0 … … … 1086 [19] 0

Sort Ascending 1 st^ position

Subscript Value [0] 50 [1] 40 [2] 100 [3] 30 [4] 10 [5] 20 [6] 0 … … [19] 0

Move the value in the 1st position to holdValue = 50.

Find the lowest value and move it to the 1st position.

Move holdValue to the position that the lowest value was found, swap values.

Sort Ascending 2nd position

Subscript Value [0] 10 [1] 40 [2] 100 [3] 30 [4] 50 [5] 20 [6] 0 … … [19] 0

Move the value in the 2 nd^ position to holdValue = 40.

Find the next lowest value and move it to the 2nd position.

Move holdValue to the position that the lowest value was found.

Sort Ascending 4th position

Subscript Value [0] 10 [1] 20 [2] 30 [3] 100 [4] 50 [5] 40 [6] 0 … … [19] 0

Move the value in the 4th position to holdValue = 100.

Find the next lowest value and move it to the 4th position.

Move holdValue to the position that the lowest value was found.

Sort Ascending 5th position

Subscript Value [0] 10 [1] 20 [2] 30 [3] 40 [4] 50 [5] 100 [6] 0 … … [19] 0

Move the value in the 5th position to holdValue = 50.

A lower value than 50 will not be found, but the comparison must still be made.