Distance Learning Center - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Distance Learning Center

Description:

Array Definition and Declaration. Array Examples. March 14, ... static char string[5] = 'WXYZ'; static char string[5] = { W', X', Y', Z', '}; March 14, 2005 ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 33
Provided by: melis76
Category:

less

Transcript and Presenter's Notes

Title: Distance Learning Center


1
Distance Learning Center
  • Lecture 12
  • C Programming Language with Applications
  • Melissa Lin, IT Consultant
  • HenEm, Inc. Parkville, Missouri
  • linm_at_ipfw.edu
  • http//www.etcs.ipfw.edu/linm

2
Lecture 12 C Arrays
  • C Arrays
  • Array Characteristics
  • Types of Arrays
  • Array Definition and Declaration
  • Array Examples

3
Array Characteristics
  • Array Characteristics
  • For grouping related data of the same type under
    one name
  • Elements of an array are stored sequentially in
    memory
  • The size of the array must be known at the time
    the array is allocated
  • To refer to particular location or element within
    the array, specifying the name of the array and
    the subscript

4
Array Characteristics (continue)
  • Syntax int cN
  • Arrays name is the base address or pointer to
    the beginning location of the array
  • An array subscript is enclosed in a pair of
    brackets and always counted from 0 to n 1
  • First element of an array - c0
  • Second element of an array - c1
  • The brackets used to enclose the subscript of an
    array.

5
Array Characteristics (continue)
  • Syntax int cN
  • Arrays
  • Character arrays, Integer arrays, Float arrays,
    etc
  • Character array, the last location is reserved
    for \0 (null character)
  • integer and float arrays, all the locations can
    be used for holding data
  • Out of array boundary checking
  • not performed automatically
  • it is the responsibility of C programmers.

6
Array Characteristics (continue)
  • Example int array c10 / contains 10
    elements /
  • c0 -45
  • c1 6
  • c2 0
  • c3 72
  • c4 1543
  • c5 -89
  • c6 0
  • c7 62
  • c8 -3
  • c9 1
  • printf(d, c0 c1 c9) /print
    the sum of the array /

Array name c
Position of the element within array c
7
Types of arrays
  • Array of integers
  • Array of floating point numbers
  • Array of characters String array
  • Array of pointers
  • Multi-dimensional array
  • type array_namesizesize ..size

8
Defining Arrays
  • Array definition
  • data-type array-namesize
  • example int c100
  • Elements of an array can be initialized in a
    definition, assignment, input from keyboard, etc
  • A character array can be initialized using a
    string literal, or character constants
  • All strings in C with a null character \0

9
Array Declarations
  • Arrays declaration
  • It must be declared before use
  • The purpose of defining integer arrays - inform C
    compiler the memory storage needed
  • Dimensions of arrays declaration
  • One-Dimensional Arrays
  • Two-Dimensional Arrays
  • multiple-Dimensional Arrays

10
Array Declarations (continue)
  • Examples of Declaring 1-D Arrays
  • char name30 / 29 characters /
  • char buffer1024 / 1023 characters /
  • int rpm100 / 100 elements /
  • int io_channel10 / IO channel /
  • float pid3
  • / 3 parameters for Personal ID Number /
  • char device_name1010 /10 string arrays/
  • char device10 / a pointer array /

11
Two Dimensional Arrays
  • An image array
  • unsigned char image256256
  • / 8-bit gray level, image array /
  • Define a 2-D character array that can be used for
    displaying 2-by-20 characters
  • define ROW 2
  • define COL 20
  • char display_LCDROWCOL
  • display_LCD00 address of the element at
    row 0 col 0
  • display_LCD019 ... address of the element at
    row 0 col 19
  • display_LCD10 address of the element at
    row 1 col 0
  • display_LCD119 address of the element at
    row 1 col 19

12
Two Dimensional Arrays (continue)
  • define ROW 100
  • define COL 5
  • int data_tableROWCOL
  • data_table00 addresses of the element at
    row 0 col 0
  • data_table01 address of the element at row
    0 col 1
  • data_table10 address of the element at row
    1 col 0
  • data_table11 address of the element at row
    1 col 1

13
Initializing Arrays
  • Examples
  • static int d_array3 1, 2, 3
  • static int parts_bin 100, 120, 200, 300, 400,
    120
  • // parts_bin0, parts_bin1, ..
  • static char string5 WXYZ
  • static char string5 W, X, Y, Z,
    \0

14
Initializing Arrays (continue)
  • Muit-dimensional arrays (more than 1-D)
  • define ROW 2
  • define COL 2
  • static int dataROWCOL 0, 1, 2, 3
  • / Initialize Four 2-by-3 arrays /
  • static int my_array423
  • 1, 2, 3, 4, 5, 6,
  • 7, 8, 9, 10, 11, 12,
  • 13, 14, 15, 16, 17, 18,
  • 19, 20, 21, 22, 23, 24

15
Example 12-1 Initializing 2-by-20 LCD Display
  • Initializing Array at run time using loop
    control
  • / Initialize a 2D character array /
  • define SIZE 2
  • define LEN 20
  • void main()
  • char display_LCDSIZELEN
  • int r, c
  • for(r 0 r lt ROW r )
  • for(c 0 c lt COL c)
  • display_LCDrc \0

16
Array Size Calculation
  • If an array size is omitted from a definition,
    the number of elements in the array will be the
    number of elements in the identifier list.
  • An example int n 1, 2, 3, 4
  • an arrays size can be specified with a symbolic
    constant and initializing array elements
  • example
  • define SIZE 10
  • int nSIZE

17
Example 12-2 Initializing Arrays Using For Loop
  • /Initialize the elements of array s to the even
    integers from 2 to 20 /
  • include ltstdio.hgt
  • define SIZE 10
  • int main() / function main begins program
    execution /
  • / symbolic constant SIZE can be used to
    specify array size /
  • int s SIZE / array s has 10 elements /
  • int j / counter /
  • for ( j 0 j lt SIZE j ) / set the
    values /
  • s j 2 2 j
  • / end for /
  • printf( "s13s\n", "Element", "Value" )
    //array s in tabular format
  • for ( j 0 j lt SIZE j )
  • printf( "7d13d\n", j, s j )
  • / end for /
  • return 0 / indicates successful termination
    /
  • / end main /

18
Example 12-2 Initializing Arrays Using For Loop
(continue)

19
Example 12 - 3
  • / char1.c - Manipulating character arrays /
  • include ltstdio.hgt
  • define SIZE 12
  • static char phoneSIZE 260-481-1234"
  • static char phone1 "260-481-1212"
  • void main()
  • int i
  • char c

20
Example 12 3 (continue)
  • // A loop with counter for reading char array
  • for(i 0 i lt SIZE i)
  • c phonei
  • putchar(c)
  • putchar('\n')
  • for(i 0 (c phone1i) ! '\0) i)
  • putchar(c)

Output 260-481-1234 260-481-1212
21
Example 12-4
  • / for0.c - Working with integer arrays
    Initialize, /
  • / copy and print contents of a and b
    arrays /
  • include ltstdio.hgt
  • define SIZE 10
  • void main()
  • int n, aSIZE, bSIZE
  • /Initialize the a array with the index n 0,
    1, 2,.., 9/
  • for(n 0 n lt SIZE n)
  • an n
  • / Initialize the b array with an 2 /
  • for(n 0 n lt SIZE n)
  • bn an 2
  • printf("ad d, bd
    d\n",n,an,n,bn)

22
Example 12 4 Output (continue)
23
Example 12- 5
  • / for1.c - Working with character arrays /
  • / Print 'a'..'z', 'A'..'Z' /
  • / Initialize lletter, and uletter /
  • / Print the contents of arrays /
  • include ltstdio.hgt
  • define NEWLINE '\n'
  • void main()
  • int n int i 0
  • char uletter27, lletter27

24
Example 12 5 (continue)
  • / Initialize char 'a' through 'z' for a lower
    case letter array /
  • for(n 'a' n lt 'z' n)
  • printf("c", n)
  • lletteri (char) n // Type casting
  • lletteri '\0' // Insert an End-of-string
    delimeter
  • putchar(NEWLINE) i 0
  • / Initialize char 'A' through 'Z' for a upper
    case letter array /
  • for(n 'A' n lt 'Z' n)
  • printf("c", n)
  • uletteri (char) n

25
Example 12 5 (continue)
  • uletteri '\0'
  • putchar(NEWLINE)
  • / Reading two character arrays /
  • for(i 0 uletteri ! '\0' i)
  • putchar(uletteri)
  • putchar(NEWLINE)
  • for(i 0 lletteri ! '\0' i)
  • putchar(lletteri)
  • putchar(NEWLINE)

26
Example 12 - 6
  • / dowhile0.c - A do/while loop example that uses
    rand() function to generate testing data. /
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • define SIZE 10
  • void main()
  • int i
  • unsigned r
  • float sum, savingSIZE
  • i 0 sum 0.0

27
Example 12 6 (continue)
  • do
  • savingi rand() 1.0
  • printf("savingd 8.2f\n",i,savingi)
  • i
  • while(i lt SIZE)
  • i 0
  • // summing saving array
  • do
  • sum savingi
  • while (i lt SIZE)
  • printf("Total saving is 8.2f\n", sum)
  • puts("Hit any key to continue")
  • getchar()

28
Example 12 6 (continue)
  • Output
  • saving0 38.00
  • saving1 7719.00
  • saving2 21238.00
  • saving3 2437.00
  • saving4 8855.00
  • saving5 11797.00
  • saving6 8365.00
  • saving7 32285.00
  • saving8 10450.00
  • saving9 30612.00
  • Total saving is 133796.00
  • Hit any key to continue

29
Example 12 - 7
  • / if1.c - Using if() statement to check if
    swapping is need. Swapping elements of an array
    /
  • include ltstdio.hgt
  • define SIZE 4
  • static int a 2, 4, 9, 0
  • void main()
  • int i, temp
  • // Print array before swapping
  • for(i 0 i lt SIZE i)
  • printf("ad d\t", i, ai)

30
Example 12 - 7 (continue)
  • for(i 0 i lt SIZE -1 i)
  • if(ai lt ai1)
  • temp ai
  • ai ai1
  • ai1 temp
  • // Print array after swapping
  • puts("\n")
  • for(i 0 i lt SIZE i)
  • printf("ad d\t", i, ai)

Output a0 2 a1 4 a2 9 a3
0 a0 4 a1 9 a2 2 a3 0
31
Summary
  • C Arrays
  • Array Characteristics
  • Types of arrays
  • Array definition
  • Array Declaration
  • Array Examples
  • Next
  • Passing array to functions
  • Searching arrays
  • Multiple subscription arrays

32
Question?
  • Answers
  • Email linm_at_ipfw.edu
Write a Comment
User Comments (0)
About PowerShow.com