Arrays and Matrices - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays and Matrices

Description:

Arrays and Matrices – PowerPoint PPT presentation

Number of Views:140
Avg rating:3.0/5.0
Slides: 25
Provided by: Sonia209
Category:

less

Transcript and Presenter's Notes

Title: Arrays and Matrices


1
Arrays and Matrices
2
One-Dimensional Arrays
  • An array is an indexed data structure
  • All variables stored in an array are of the same
    data type
  • An element of an array is accessed using the
    array name and an index or subscript
  • The name of the array is the address of the first
    element and the subscript is the offset
  • In C, the subscripts always start with 0 and
    increment by 1

3
Definition and Initialization
  • An array is defined using a declaration
    statement.
  • data type array_namesize
  • allocates memory for size elements
  • subscript of first element is 0
  • subscript of last element is size-1
  • size must be a constant

4
Example
  • int list10
  • allocates memory for 10 integer variables
  • subscript of first element is 0
  • subscript of last element is 9
  • C does not perform any bounds checking on arrays

list0
list1
list9
5
Initializing Arrays
  • Arrays can be initialized at the time they are
    declared.
  • Examples
  • double taxrate3 0.15, 0.25, 0.3
  • char list5 h,e,l,l,o
  • double vector100 0.0 /assigns zero to
    all 100 elements/
  • int s 5,0,-5 /the size of a s is 3
    /

6
Assigning values to an array
  • for loops are often used to assign values to an
    array
  • Example
  • int list10, i
  • for(i0 ilt10 i)
  • listi i

7
Input from a data file
  • Arrays are often used to store information from a
    data file
  • Example
  • int k
  • double time10, motion10
  • FILE sensor3
  • sensor3 fopen(sensor3.txt, r)
  • for(k0 klt10 k)
  • fscanf(sensor3, lf lf,timek,
    motionk)

8
Practice!
  • Show the contents of the arrays defined in each
    of the following sets of statements.
  • a) int x10 -5, 4, 3
  • b) char letters 'a', 'b', 'c'
  • double z4
  • z1-5.5
  • z2z3fabs(z1)
  • int k
  • double time9
  • ..
  • for(k0klt8k)
  • timek(k-4)0.1

9
Practice
  • Assume that the variable k and the array s have
    been defined with the following statement
  • int k, s3,8,15,21,30,41
  • Using a hand calculation, determine the output
    for each of the following sets of statements
  • 1. for(k0klt5k2)
  • printf(d d \n,sk,sk1)
  • 2. for(k0klt5k)
  • if(sk20)
  • printf(d ,sk)
  • printf(\n)

10
Function Arguments
  • When the information in an array is passed to a
    function, two parameters are usually used.
  • One parameter specifies a particular array.
  • Other parameter specifies the number of elements
    used in the array.

11
Passing Entire Arrays as Arguments to Functions
  • Arrays are always pass by reference
  • The array name is the address of the first
    element
  • The maximum size of the array must be specified
    at the time the array is declared. The actual
    number of array elements that are used will vary,
    so the actual size of the array is usually passed
    as another argument to the function

12
Example
  • int main(void)
  • / Declare variables and functions /
  • FILE exp1
  • double max (double array, int actual_size)
  • double x100
  • int count0
  • exp1 fopen(exp1.txt, r)
  • while((fscanf(exp1, f, xcount)) 1)
  • count
  • printf(Maximum value f \n, max(x, count))
  • fclose(exp1)
  • return 0
  • //end main

13
Selection Sort
  • Example
  • Ascending algorithm
  • Find the minimum value
  • Exchanging the minimum value with the value in
    the first position in the array
  • Find the next minimum value begin with second
    element and exchange this minimum value with the
    second position in the array
  • Continue until the last element

14
Two-Dimensional Arrays
15
Definition and Initialization
  • int xrowscolumns
  • Example, int x43
  • int x432,3,-1,0,-3,5,2,6,3,-2,10,4
  • int x32,3,-1,0,-3,5,2,6,3,-2,10,4
  • If the initializing sequence is shorter than the
    array, the rest of the values are initialized to
    zero.

16
Nested for loop
  • Arrays can also be initialized with program
    statements.
  • Two nested for loops are usually required to
    initialize an array.
  • / Declare variables /
  • int i, j, t54
  • .
  • / Initialize array. /
  • for(i0ilt4i)
  • for(j0jlt3j)
  • tiji

17
Example
  • include ltstdio.hgt
  • int main()
  • int i,j
  • int data341,2,3,4,5,6,7,8,9,10,11,12
  • for (i0ilt3i)
  • for(j0jlt4j)
  • printf("2d ",dataij)
  • printf("\n")
  • return 0

18
Matrices
  • A matrix is a set of number arranged in a grid
    with rows and columns. A matrix is defined using
    a type declaration statement.
  • data type array_namerow_sizecolumn_size
  • int matrix34

row0 row1 row2
in memory
row0
row1
row2
19
Accessing Array Elements
  • int matrix34
  • matrix has 12 integer elements
  • matrix00 element in first row, first column
  • matrix23 element in last row, last column
  • matrix is the address of the first element
  • matrix1 is the address of the second row

20
Practice!
  • int i, j, g330,0,0,1,1,1,2,2,2
  • Give value of sum after each statements is
    executed
  • 1) sum0
  • for (i0ilt2 i)
  • for(j0jlt2j)
  • sum gij
  • 2) sum1
  • for(i1ilt2i)
  • for(j0jlt1j)
  • sum gij
  • 3) sum0
  • for(j0jlt2j)
  • sum - g2j
  • 4) sum0
  • for(i0ilt2i)
  • sum gi1

21
2-Dimensional Arrays as Arguments to Functions
  • Example
  • void transpose(int bNROWSNCOLS, int
    btNCOLSNROWS)
  • / Declare Variables. /
  • int i, j
  • / Transfer values to the transpose matrix. /
  • for(i0 iltNROWS i)
  • for(j0 jltNCOLS j)
  • btji bij
  • return

22
Character Functions
  • Character I/O
  • Example 1,
  • printf and scanf functions- print and read
    characters using c specifier.
  • Example 2,
  • putchar- takes one integer argument, returns an
    integer value.
  • putchar(a)
  • putchar(b)
  • putchar(\n)
  • putchar(c)
  • same as
  • putchar(97)
  • putchar(98)
  • putchar(10)
  • putchar(99)

23
  • getchar function-reads character from the
    keyboard and returns the integer value of the
    character.
  • Example,
  • int c
  • .
  • putchar(\n)
  • cgetchar()
  • putchar(c)
  • putchar(\n)

24
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com