Arrays - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Arrays

Description:

Array subscripts start at 0 and run to one less than the declared size of the array. ... Reads quiz scores for each student into the two-dimensional array grade ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 13
Provided by: shirle73
Category:
Tags: arrays | run | up

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
  • ITK 169
  • Fall 2003

2
Single Dimension Arrays
  • In C an array consisting of 5 variables of type
    int is declared
  • int score5
  • The value in the brackets is called the declared
    size of the array.
  • Such a declaration creates 5 int type variables
    which are accessed as
  • score0, score1, score2, score3,
    score4
  • The number in the brackets is called an index or
    subscript.
  • Array subscripts start at 0 and run to one less
    than the declared size of the array.
  • All indexed variables in a particular array must
    have the same type.
  • This type is called the base type of the array.
  • Be careful not to confuse the declared size in a
    declaration of an array with the index value in
    an array reference.
  • int array_namedeclared_size
  • array_nameindex // 0 lt index lt
    declared_size

3
Be Very Careful!
  • The most common programming error when using
    arrays is the attempt to reference a non-existent
    array index.
  • The array definition
  • int a6
  • declares ONLY the indexed variables a0 through
    a5.
  • An index value outside 0 to 5 is an out of range
    error, i.e., illegal.

4
Arrays in Functions
  • Arrays are passed by reference automatically (DO
    NOT use ).void my_funct(int anArray )
  • If you do not want the array to be changed you
    must hold it constant.void my_funct(int const
    anArray )
  • An array cannot be a return type.

5
PracticeGiven a Square Array
  • Write a function to find the sum of the main
    diagonal of an array. (Pass the array in and hold
    it constant. Return the sum.)
  • Change the function to find the sum of the second
    diagonal.
  • Change the function to find the sum of a single
    row (passing in the index of the row).

6
More Practice
  • void fill_array(int myArr , int size, int
    number_used)
  • //Precondition size is the declared size of the
    array myArr.
  • //Postcondition number_used is the number of
    values stored in myArr.
  • // myArr0 through myArrnumber_used-1 have
    been filled
  • // with nonnegative integers read from the
    keyboard.
  • int search(const int myArr , int number_used,
    int target)
  • //Precondition number_used is lt the declared
    size of myArr.
  • //Also, myArr0 through myArrnumber_used -1
    have values.
  • //Returns the first index such that myArrindex
    target,
  • //provided there is such an index, otherwise
    returns -1.

7
Multidimensional Arrays
  • It is useful to have an array with more than one
    index. This is called a multidimensional array.
  • Such an array is declared as following
  • char page30100
  • This array has 30 rows and 100 columns.
  • There are 30100 indexed variables for this
    array. The indexed variables for this array are
  • page00, page00, . . . .
    page099
  • page10, page11, . . . .
    page199
  • page20, page21, . . . .
    page299
  • . .
    .
  • . .
    .
  • . .
    .
  • page290, page291, . . . page2999

8
Multidimensional Arrays as Parameters
  • When passing a multidimensional array into a
    function, the first dimension is not given a
    size, but the second MUST have the size.void
    myFunct(char page 100)
  • Remember, this array is passed by reference and
    changes will be returned to the calling
    function.

9
(No Transcript)
10
Practice
  • const int NUMBER_STUDENTS 4, NUMBER_QUIZZES
    3
  • //Reads quiz scores for each student into the
    two-dimensional array grade
  • void read_scores(int grade NUMBER_QUIZZES)
  • //Precondition Global constant NUMBER_STUDENTS
    and NUMBER_QUIZZES
  • //are the dimensions of the array grade.
  • //Postcondition Each of the indexed variables
    gradest_num-1, quiz_num-1 contains the score
    for student st_num on quiz quiz_num.
  • // Computes the average score for each student
    and
  • //the average score for each quiz. Displays the
    quiz scores and the averages.
  • void compute_st_ave(const int gradeNUMBER_QUIZZ
    ES, double st_ave)
  • //Precondition Global constant NUMBER_STUDENTS
    and NUMBER_QUIZZES
  • //are the dimensions of the array grade. Each of
    the indexed variables
  • //gradest_num-1, quiz_num-1 contains the score
    for student st_num on quiz
  • //quiz_num.
  • //Postcondition Each st_avest_num-1 contains
    the average for student number
  • //matching the row number of the grade
    array.
  • Write a third function to compute the quiz
    averages. (see next slide).

11
(No Transcript)
12
Arraysand Class Objects
  • Need practice problem for an array of objects
  • Need practice problem for a class with an array
    member
Write a Comment
User Comments (0)
About PowerShow.com