CPS 125: Digital Computation and Programming - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CPS 125: Digital Computation and Programming

Description:

Modeling with Two-Dimensional Arrays. Vectors and Matrices. Solving Systems of Linear Equations ... than one pixel has this highest value, information for all ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 19
Provided by: Che6158
Category:

less

Transcript and Presenter's Notes

Title: CPS 125: Digital Computation and Programming


1
CPS 125 Digital Computation and Programming
  • Multidimensional Arrays

2
Outline
  • Declaring and Referencing Multidimensional Arrays
  • Modeling with Two-Dimensional Arrays
  • Vectors and Matrices
  • Solving Systems of Linear Equations
  • Common Programming Errors

3
Dimensions
  • int num6

int num26
num00
num0
num1
num15
4
Multidimensional Arrays
  • Multiple dimensional arrays
  • int room236
  • Can think of as 2 sets of 3 rows and 6
    columns

room105
room000
room115
room125
room020
room025
5
Multidimensional Array
  • Really stored in contiguous fashion
  • int room236

000
010
020
125
100
110
120
Just convenient to think of as 2 sets of 3
rows and 6 columns!
6
Multidimensional Arrays
  • Declaration initialization
  • Similar to one-dimensional
  • Nested braces, commas
  • char name38 S, a, m, \0,
  • C, h, u, c, k,
    \0,
  • M, a, r, k, \0
  • --- or ---
  • char name38 Sam, Chuck, Mark
  • int num26 1, 2, 3, 4, 5, 6, 2, 3

7
Passing Multidimensional Arrays
  • All but 1st subscript must be present
  • function call init_array(rooms)
  • function header void init_array(int array
    36)

Highest dimension always unspecified because we
may pass arrays of different sizes. Other
dimensions just say how array will be manipulated.
8
  • / 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)

9
  • Which of the arrays shown would be valid actual
    arguments to pass to a function as below,
  • int fun(double arr310)
  • double m6310
  • int n4310
  • double z8211
  • double q20310
  • How many elements are in each of arrays above?

10
  • int soil_type47MAXDEPTH
  • /Finds and displays how much silt is contained
    in each north-south strip /
  • for (north_south0 north_southlt4 north_south)
  • silt_amount 0
  • for (east_west0 east_westlt7 east_west)
  • for (depth0 depthltMAXDEPTH depth)
  • silt_amount soil_typenorth_southeast_west
  • depth
  • printf(There are d soil cells containing silt
    in north_south segment d.\n, silt_amount,
    north_south)

11
Programming Project
  • You have been asked to write one part of a
    detector analysis software package for a
    telescope. Your program takes as input the
    brightness of each point in a two-dimensional
    array representing an image of the sky. Use a
    1010 integer array for this image. Find and
    display the x and y coordinates and the value of
    the brightest pixel. If more than one pixel has
    this highest value, information for all
    highest-valued pixels should be displayed.

12
Vectors
  • Vector a mathematical object consisting of a
    sequence of numbers.
  • / a vector lt4, 12, 19gt /
  • int vect3 4, 12, 19
  • Calculating scalar product
  • sum_prod 0
  • for (k0 kltn k)
  • sum_prod xk wk

13
Matrices
  • Matrix a mathematical object consisting of a
    rectangular arrangement of numbers.
  • / a matrix 4.1 8.3 /
  • 7.9 6.2
  • double m22 4.1, 8.3, 7.9, 6.2
  • Multiplying a matrix by a vector
  • vi 0
  • for (k0 kltn k)
  • vk aik xk

14
  • / 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

15
  • If m is a 5x5 integer matrix, what is displayed
    by this loop
  • for (i0 i lt5 i)
  • printf(8d, m2i)
  • what is displayed by this loop?
  • for (i0 i lt5 i)
  • printf(8d, mi4)
  • If sq is a C array representing the matrix
  • 4 5 18
  • 2 4 9
  • 8 4 12
  • what is displayed by this loop?
  • for (i0 i lt3 i)
  • printf(8d, sqii)

16
Solving Systems of Linear Equations
  • Algorithm for Gaussian Elimination
  • Transform the original system into scaled
    triangular form
  • Solve for the xi by back substitution
  • Data structure augmented matrix
  • Triangularizing and scaling rules
  • Multiply any row of Aug by a nonzero number
  • Add to any row of Aug a multiple of any other row
  • Swap any two rows

17
Solving Systems of Linear Equations
  • Implementation
  • Function Gauss
  • Function pivot
  • Function back substitution
  • It works when there is unique solution
  • It does not work for systems with no solution,
    multiple solutions, or ill-conditioned
  • Computational round-off errors may be too large

18
Common Programming Errors
  • Must supply all except first dimensions for array
    parameters in function prototype
  • Subscript range errors
  • No enough space for large multidimensional arrays?
Write a Comment
User Comments (0)
About PowerShow.com