Multidimensional Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Multidimensional Arrays

Description:

A.Abhari CPS125. 1. Multidimensional Arrays. Multidimensional array is the array ... There are only 10 transmitters and there is a need for a program to help ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 28
Provided by: aabh
Category:

less

Transcript and Presenter's Notes

Title: Multidimensional Arrays


1
Multidimensional Arrays
  • Multidimensional array is the array with two or
    more dimensions.
  • For example
  • char box 3 3 defines a two-dimensional array
  • and
  • box21 is an element in row 2 , column 1
  • and
  • char box3 can be used in the function
    prototype
  • note that only the first dimension can be omitted

2
Multidimensional Arrays
  • For example
  • double table NROWSNCOLS
  • Can be used as parameter in the function
    prototype as
  • void
  • process_martrix( int in 4,
  • int out 4,
  • int nrows)

3
Two Dimensional Array
  • Char box 3 3

Column
0
1
2
Row
0
box 1 2
1
2
4
  • /
  • Checks whether a box is completely filled
  • /
  • int
  • filled(char box33) / input - box to
    check /
  • int r,c, / row and column subscripts /
  • ans / whether or not box is
    filled. /
  • / Assumes box is filled until blank is
    found /
  • ans 1
  • / Resets ans to zero if a blank is
    found /
  • for (r 0 r lt 3 r)
  • for (c 0 c lt 3 c)
  • if (boxrc ' ')
  • ans 0
  • return (ans)

5
Arrays with Several Dimensions
  • int soil_type4 7 MAXDEPTH

6
Case Study Cellular Telephone System
  • Problem Finding the best way to build a cellular
    network. There is some marketing data that
    predicts the demand will be at tree time of
    interest. There are only 10 transmitters and
    there is a need for a program to help analyzing
    call demand data.

7
Case Study Cellular Telephone System
  • Analysis There should be three matrices shows
    traffic density for each time of the day
  • Input
  • int commutersGRID_SIZEGRID_SIZE
  • int salesforceGRID_SIZEGRID_SIZE
  • int weekendGRID_SIZEGRID_SIZE
  • Output
  • int summed_dataGRID_SIZEGRID_SIZE
  • int location_i, location_j

8
Case Study Cellular Telephone System
  • Design initial algorithm
  • Get traffic data for three time period
  • Get the weights from user
  • Multiply weight by each matrix entry and store
    the sum in the summed data
  • Find highest valued cells in the summed data and
    display them as the pair of location_i and
    location_j
  • Implementation

9
Filling the multidimensional array
  • / Fills 3 GRID_SIZE x GRID_SIZE arrays with
    traffic data from TRAFFIC_FILE/
  • void
  • get_traffic_data(int commutersGRID_SIZEGRID_SIZ
    E, / output /
  • int salesforceGRID_SIZEGRID_SI
    ZE, / output /
  • int weekendGRID_SIZEGRID_SIZE
    ) / output /
  • int i, j / loop counters /
  • FILE fp / file pointer /
  • fp fopen(TRAFFIC_FILE, "r")
  • for (i 0 i lt GRID_SIZE i)
  • for (j 0 j lt GRID_SIZE j)
  • fscanf(fp, "d", commutersij)
  • for (i 0 i lt GRID_SIZE i)
  • for (j 0 j lt GRID_SIZE j)
  • fscanf(fp, "d", salesforceij)
  • for (i 0 i lt GRID_SIZE i)
  • for (j 0 j lt GRID_SIZE j)
  • fscanf(fp, "d", weekendij)
  • fclose(fp)

10
Modifying the multidimensional array
  • / Computes and displays the weighted,
    summed_data /
  • for (i 0 i lt GRID_SIZE i)
  • for (j 0 j lt GRID_SIZE j)
  • summed_dataij
    commuter_weight

  • commutersij

  • salesforce_weight

  • salesforceij

  • weekend_weight

  • weekendij

11
Searching in the multidimensional array
  • / Finds the NUM_TRANSMITTERS highest values in
    the summed_data matrix.Temporarily stores the
    coordinates in location_i and location_j, and
    then displays the resulting locations /
  • printf("\n\nLocations of the d
    transmitters\n\n", NUM_TRANSMITTERS)
  • for (tr 1 tr lt NUM_TRANSMITTERS
    tr)
  • current_max SELECTED / Starts off
    our search with a
  • value
    that is known to be too low. /
  • for (i 0 i lt GRID_SIZE i)
  • for (j 0 j lt GRID_SIZE j)
  • if (current_max lt
    summed_dataij)
  • current_max
    summed_dataij
  • location_i i
  • location_j j

12
Printing the contents of multidimensional array
  • /
  • Displays contents of a GRID_SIZE x GRID_SIZE
    matrix of integers
  • /
  • void
  • print_matrix(int matrixGRID_SIZEGRID_SIZE)
  • int i, j / loop counters /
  • for (i 0 i lt GRID_SIZE i)
  • for (j 0 j lt GRID_SIZE j)
  • printf("3d ", matrixij)
  • printf("\n")

13
Vectors
  • Vector a mathematical object consisting of a
    sequence of numbers.
  • / a vector lt4, 12, 19gt /
  • int vect3 4, 12, 19
  • Differences between vector and array
  • 1- an n_dimensional vector is represented in C
    as a one dimensional array of size n.
  • 2- vect3 is vect2 in C

14
Vectors
  • Calculating scalar product
  • lt1,2,4gt. lt2,3,1gt 12 23 4112
  • In C
  • sum_prod 0
  • for (k0 kltn k)
  • sum_prod xk wk

15
Matrices
  • Matrix a mathematical object consisting of a
    rectangular arrangement of numbers called the
    element of matrix..
  • / a matrix 3 6
  • 4 5
  • int x22 3, 6, 4, 5

3
6
x
4
5
16
Matrices
  • Multiplying a matrix by a vector
  • A X V
  • 1 1 1 5
  • 2 3 1 1 10
    multiplication
  • 1 -1 -1 2 -3
    on
  • 0 1 2 2 6
    the right
  • In C for each member of V
  • vi 0
  • for (k0 kltn k)
  • vk aik xk

17
  • / Computes the product of M-by-N matrix a and
    the N-dimensional vector x. The result is stored
    in the output parameter v, an M-dimensional
    vector./
  • void mat_vec_prod(double v, / M-dimensional
    vector /
  • double aMN, /
    M-by-N matrix /
  • double x) /
    N-dimensional vector /
  • int i, k
  • for (i 0 i lt M i)
  • vi 0
  • for (k 0 k lt N k)
  • vi aik xk

18
Matrix Multiplication
  • 1 1 1 2 0 1 6 0 0
  • 2 3 1 1 -1 0 10 -2 1
  • 1 -1 -1 3 1 -1 -2 0 2
  • for ( i0 ilt m , i)
  • for (j0 jltp j)
  • .. compute cij.

19
  • / Multiplies matrices A and B yielding product
    matrix C /
  • void mat_prod(double cMP, / output - M by
    P matrix /
  • double aMN, / input - M by
    N matrix /
  • double bNP) / input - N by
    P matrix /
  • int i, j, k
  • for (i 0 i lt M i)
  • for (j 0 j lt P j)
  • cij 0
  • for (k 0 k lt N k)
  • cij aik
    bkj

20
Solving System of Linear Equations
  • To solve many problems such as force equation in
    three-dimensional system we need to solve a three
    linear equations
  • A X
    Y
  • 1 1 1 x1
    4
  • 2 3 1 x2
    9
  • 1 -1 -1 x3
    -2
  • It is multiplication of a matrix by a vector on
    the right

21
Gaussian Elimination
  • Gaussian elimination can be used to solve a
    linear equation.
  • The algorithm for Gussian elimination is
  • Transform the original system into scaled
    triangular form.
  • Solve for xi by back substitution

22
Gaussian Elimination
  • triangular form
  • 1 1 1 x1
    4
  • 0 1 -1 x2
    1
  • 0 0 1 x3
    1
  • back substitution
  • x1 x2 x3 4
  • x2 - x3 1
  • x3 1

23
Gaussian Elimination
  • For doing that we need to triangularizing the
    augmented matrix by following operations
  • Multiply any row of aug by nonzero number
  • Add to any row of aug a multiple of other rows
  • Swap any two rows
  • If system has a unique solution, we can get the
    system into desired form by this three
    operations.

24
  • /
  • Performs pivoting with respect to the pth row
    and the pth column
  • If no nonzero pivot can be found, FALSE is
    sent back through piv_foundp
  • /
  • void pivot(double augNN1, / input/output -
    augmented matrix /
  • int p, / input - current
    row /
  • int piv_foundp) / output - whether or
    not nonzero pivot found /
  • double xmax, xtemp
  • int j, k, max_row
  • / Finds maximum pivot /
  • xmax fabs(augpp)
  • max_row p
  • for (j p1 j lt N j)
  • if (fabs(augjp) gt xmax)
  • xmax fabs(augjp)
  • max_row j

25
  • / Swaps rows if nonzero pivot was found /
  • if (xmax 0)
  • piv_foundp FALSE
  • else
  • piv_foundp TRUE
  • if (max_row ! p) / swap rows /
  • for (k p k lt N1 k)
  • xtemp augpk
  • augpk
    augmax_rowk
  • augmax_rowk xtemp

26
  • /
  • Performs back substitution to compute a
    solution vector to a system of
  • linear equations represented by the augmented
    matrix aug. Assumes that
  • the coefficient portion of the augmented
    matrix has been triangularized,
  • and its diagonal values are all 1.
  • /
  • void
  • back_sub(double augNN1, / input - scaled,
    triangularized

  • augmented matrix /
  • double xN) / output -
    solution vector /
  • double sum
  • int i, j
  • xN - 1 augN - 1N
  • for (i N - 2 i gt 0 --i)
  • sum 0
  • for (j i 1 j lt N j)
  • sum augij xj
  • xi augiN - sum

27
Common Programming Errors
  • Use constants for each dimensions size when
    declaring multidimensional array
  • When declaring the array as a parameter of a
    function if you omit the first dimension all
    other dimensions must be supplied
  • Since access to the elements of a
    multidimensional array requires nested counting
    loops it is easy to make out-of-range error.
  • Since using multidimensional arrays as local
    variables requires large memory space, you may
    need to tell to operating system to increase
    stack size when the program is running
Write a Comment
User Comments (0)
About PowerShow.com