Lect 11P. 1 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Lect 11P. 1

Description:

Arrays Lecture 11 – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 11
Provided by: RickF162
Category:

less

Transcript and Presenter's Notes

Title: Lect 11P. 1


1
Arrays
  • Lecture 11

2
What Is an Array?
  • An array is a group of items of the same type
    under one variable name. It is an ordered list
    of elements where each is of the same data type.
  • As an example, the days of the week can be
    represented as a one-dimensional array named day
    where Sunday is the first element, Monday the
    second, and so on.

3
Thinking About Arrays
  • The days of the month can be thought of as a
    two-dimensional array. Looking at a calendar,
    each row represents a week (which can be
    represented as a one-dimensional array), and the
    entire month is composed of an array of arrays,
    that is, a two-dimensional array.

4
Arrays
  • Taking the concept further, the days of the year
    can be represented as a three-dimensional array,
    that is, an array of arrays of arrays. The first
    dimension is the day of the week, the second the
    week of the month, and the third the month of the
    year.

5
Declaring Arrays
  • A one-dimensional array is represented by
    specifying the array name followed by a number in
    square brackets to indicate the array size
    (or number of elements).
  • The statement
  • int day 7
  • would declare a seven element array of integers.
  • Declaring multi-dimensional arrays is similar
    int month 4 7 would declare an array of
    integers with 4 rows and 7 columns.

6
Assigning Values to Arrays
  • Once an array is declared, the elements are
    accessed by specifying the array name and the
    number of the element. In C, the first element
    is number 0, so a seven element array would have
    elements numbered 0 through 6.
  • For the week of February 3 through 9 the array
    could be assigned as follows
  • day03 day14 day25
    day36 day47 day58 day69
  • The number in the brackets is called an index (or
    a subscript).

7
Assigning Values to Arrays
  • Optionally, the array can have values assigned to
    its elements when the array is declared.
  • For example, the statement
  • int day 7 2, 3, 4, 5, 6, 7, 8
  • declares a seven element array day and
    initializes (assigns values to) all seven of the
    elements.
  • The program on the next slide illustrates how
    that works.

8
Program Example for Arrays
  • /The following program will declare the array
    day, assign values to the elements, and print
    the data to the screen. /
  • include ltstdio.hgt
  • void main ( )
  • int n, day 7 2, 3, 4, 5, 6, 7, 8
  • for (n 0 n lt 7 n )
  • printf ("day d is February d, 2004
    \n",
  • n, dayn )

9
Program Output of an Array
  • When this program is run, it will produce the
    following output
  • day0 is February 2, 2004
  • day1 is February 3, 2004
  • day2 is February 4, 2004
  • day3 is February 5, 2004
  • day4 is February 6, 2004
  • day5 is February 7, 2004
  • day6 is February 8, 2004

10
Another Program Example
  • / Program to generate an array of 10 random
    numbers /
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • define SIZE 10 / SIZE defined as symbolic
    constant /
  • void main ( )
  • int i, numbers SIZE
  • for (i 0 i lt SIZE i )
  • numbers i rand ( )
  • printf ( "The number d is d \n", i,
    numbers i )
Write a Comment
User Comments (0)
About PowerShow.com