Arrays: Declaring, Loading, and Processing in C#, Slides of C programming

An overview of arrays in c# programming language, including how to declare and load single dimensional arrays, partially filled arrays, and constant arrays. It also covers the use of array elements as arguments and the array class properties and methods for managing arrays.

Typology: Slides

2012/2013

Uploaded on 04/27/2013

farooq
farooq 🇮🇳

4.3

(94)

203 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays part 1
12_arrays_loading.ppt
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

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

Arrays – part 1

12_arrays_loading.ppt

Overview of Topics

  • Declaring Arrays
  • Loading Arrays
  • Partially Filled Arrays
  • Arrays Elements as Arguments
  • Declaring and Loading Constant 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 we’ll 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

dataType[ ] arrayName = new dataType[arraySize];

  • An array of 3 elements of type integer is created.
  • The arraySize is used by C# to determine how much

memory to allocate.

  • Arrays will usually be class-level because after values

are loaded in we don’t want to lose the values.

Memory Map

  • 1010 intQty1 Address Variable Value
  • 1020 intQty2
  • 1030 intQty3
  • 1040 intQty[0]
  • 1050 intQty[1]
  • 1060 intQty[2]
  • 1070 decPrice

Subscript Out Of Range

  • If during execution, the subscript value

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.

Array Class Properties & Methods

int[ ] intQty = new int[3]; //3 integer variables

  • arrayName.Length
    • intQty.Length is equal to 3.
    • intQty.Length is the number of entries that can be loaded.
    • The last valid subscript value is one less than Length.
  • arrayName.GetUpperBound(0).
    • intQty.GetUpperBound(0) is equal to 2.
    • intQty.GetUpperBound(0) is last valid subscript value.
  • Depending the loop structure, we may use:
    • less than Length or
    • Equal to GetUpperBound(0)

Array Processing

  1. Declare Array
  2. Load Array
    • After creating the array, data must be loaded.
    • Use arrayName.Length or arrayName.GetUpperBound(0) to prevent out of range errors.
    • Note: Arrays can be declared and loaded with constant values.
  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.

Declare Array with a Const

// Arrays can be declared using a constant for the size.

const int intARRAY_SIZE = 20;

int[ ] cintTestScores = new int[i ntArraySize ];

//We can still load up to 20 scores

cintTestScores 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

Loaded Arrays

Position Address Index Value

1 1010 [0] 50

2 1014 [1] 40

3 1018 [2] 100

4 1022 [3] 30

5 1026 [4] 10

6 1030 [5] 20

7 1034 [6] 0

… … … …

20 1086 [19] 0

Process Array – Individual Elements

private void btnProcessArray_Click( )

{ int i; int intSum = 0;

for (i = 0; i <= cintTestScores.GetUpperBound(0); i++) { intSum += cintTestScores[i]; }

txtSum.Text = intSum.ToString(“N0”);

}

Individual Elements as Arguments

private void btnProcessArray_Click( )

{ int i; decimal decPercent;

for (i = 0; i <= cintTestScores.GetUpperBound(0); i++) { decPercent = calcPercent( cintTestScores[i] ); txtPercent.Text = decPercent.ToString(“N0”); }

}

private decimal calcPercent(int intScore )

{ return (intScore / 100);

} Docsity.com

Partially Filled Arrays

  • In the for loop on the prior slide it was assumed that the arrays 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.