CIS162AD C - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CIS162AD C

Description:

Load Array. After creating the ... We can load up to 20 scores, but we will //save how many tests are actually loaded in ... Declare and Load Constant Arrays ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 25
Provided by: JuanMa4
Category:
Tags: cis162ad | loadup

less

Transcript and Presenter's Notes

Title: CIS162AD C


1
CIS162AD - C
  • Arrays part 112_arrays_loading.ppt

2
Overview of Topics
  • Declaring Arrays
  • Loading Arrays
  • Partially Filled Arrays
  • Arrays Elements as Arguments
  • Declaring and Loading Constant Arrays

3
Arrays are Tables
  • Array is a different word for a table.
  • A table is made up of columns and rows.
  • Just like an Excel spreadsheet.

4
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.

5
Declaring Array
  • int intQty1, intQty2, intQty3int intQty
    new int3 //3 integer variablesdataType
    arrayName new dataTypearraySize
  • 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 dont want to lose the
    values.

6
Array Subscript
  • dataType arrayName new dataTypearraySize
  • 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.

7
Memory Map
8
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.

9
Preventing Out of Range
  • C will not prevent a subscript out of range
    error, but 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 built in methods to manage
    arrays, arrayName.Length arrayName.GetUpperBound
    (0).
  • The For Each (foreach) command may also be used
    to walk through arrays.

10
Array Properties
  • int intQty new int3 //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)

11
Array Processing
  • Declare Array
  • 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.
  • 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.

12
Declare Array
  • //Arrays are declared at the class level,
  • //so they can be referenced by all methods.int
    cintTestScores new int20int
    cintNumberOfStudents
  • //We can load up to 20 scores, but we will
  • //save how many tests are actually loaded in
    cintNumberOfStudents.

13
Declare Array with a Const
  • //Arrays can be declared using a constant for the
    size.const int intARRAY_SIZE 20int
    cintTestScores new intintArraySize//We can
    still load up to 20 scores

14
cintTestScores Array Memory Map
15
Load Array
cs12ex.txt 5040100301020
  • //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
    lt cintTestScores.Length)
    cintTestScoresi 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
    loadedstudentFile.Close( ) //Close
    file

16
Loaded Arrays
17
Process Array Individual Elements
  • private void btnProcessArray_Click( )
  • int iint intSum 0
  • for (i 0 i lt cintTestScores.GetUpperBound(0)
    i)
  • intSum cintTestScoresi
  • txtSum.Text intSum.ToString(N0)

18
For Each - Example
  • private void btnProcessArray_Click( )
  • int i //subscript not neededint intSum
    0
  • foreach (int intTestScore in cintTestScores)
  • intSum intTestScore
  • txtSum.Text intSum.ToString(N0)

19
Individual Elements as Arguments
  • private void btnProcessArray_Click( )
  • int idecimal decPercent
  • for (i 0 i lt cintTestScores.GetUpperBound(0)
    i)
  • decPercent calcPercent(cintTestScoresi)
  • txtPercent.Text decPercent.ToString(N0)
  • private decimal calcPercent(int intScore)
  • return (intScore / 100)

20
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.

21
Load Array Partially Filled
  • //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
    lt cintTestScores.Length)
    cintTestScoresi 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
    loadedstudentFile.Close( ) //Close
    file

22
Processing Partially Filled Arrays
  • private void btnProcessArray_Click( )
  • int iint intSum
  • //for (i 0 i lt cintTestScores.GetUpperBound(
    0) i) //process entire arrayfor (i 0 i
    lt mintNumberOfStudents i) //process partially
    filled array
  • intSum cintTestScoresi
  • txtAverage.Text (intSum / mintNumberOfStudents)
    .ToString(N0)

23
Declare and Load Constant Arrays
  • Arrays that will hold constants or some initial
    values can be 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,
    0Dstring cstrLETTER_GRADE new string
    "A", "B", "C", "D", "F"
  • C does NOT allow const for arrays.

24
Summary
  • Declaring Arrays
  • Loading Arrays
  • Partially Filled Arrays
  • Arrays Elements as Arguments
  • Declaring and Loading Constant Arrays
Write a Comment
User Comments (0)
About PowerShow.com