11-matrices-cs101-2013

Upload: aravindsomu

Post on 01-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 11-matrices-cs101-2013

    1/21

    CS 101: Computer Programming

    and Utilization

    11-MatricesMultidimensional Arrays

    Instructor: Sridhar IyerIIT om!ay

  • 8/9/2019 11-matrices-cs101-2013

    2/21

    IIT Bombay CS 101 - 2012-2 2

    Acti"ity # $oad !alancing

    There are t%o truc&s' A and ' each ha"ing

    pac&ages %ith di((erent %eights) *e %ish to !alancethe load in !oth the truc&s !y s%apping e+actly onepair o( pac&ages !et%een them)

    *rite a program to determine i( such load !alancingis possi!le (or a gi"en set o( input %eights' and i(yes' identi(y %hich pac&ages to s%ap) Assume that

    all %eights are integers ,e)g) . /g' /g' 2 /g'etc)3)

    4iscuss: pseudo-code5 6un: demo11-!alance)cpp

  • 8/9/2019 11-matrices-cs101-2013

    3/21

    IIT Bombay CS 101 - 2012-2 3

    $oad !alancing truc&s # !asic idea

    I( + is element o( A' and y element o( ' %e need to

    (ind + and y such that' i( + and y are e+changed' theresulting sums match)

    sumA 7y -+ 8 sum -y 7 + ,a(ter e+changing + and y3

    or y 8 ,sum-sumA39 7 +

    or some i' i( %e are loo&ing at A;i

  • 8/9/2019 11-matrices-cs101-2013

    4/21IIT Bombay CS 101 - 2012-2 4

    inary Search

    Can %e do (aster than se>uentially searching (or y in

    Array ?● @o% long does it ta&e any%ay?

    ● proportional to num!er o( elements - ,n3

    nce the Array is sorted

    ● *e can use an idea similar to (inding a root !y

    !isection method● @o% much is the time reduced?

    ● ,log n3

  • 8/9/2019 11-matrices-cs101-2013

    5/21IIT Bombay CS 101 - 2012-2 5

    inding a root !y !isection method

    Use this idea to de"elop the code (or inary Search

    ● 4o Thin&-Pair-Share

  • 8/9/2019 11-matrices-cs101-2013

    6/21IIT Bombay CS 101 - 2012-2 6

    B+ecution o( inary Search - e+ample

    ;0< 10012

    ;1< 100.

    ;< 100D1

    ;< 100E

    ;< 100E

    ;.< 100.

    ;E< 100DE.;2< 1011D.

    ;< 1012

    ;D< 101E2D

    or the array gi"en' sho% the "alueso( lo' hi and mid (or each iteration'

    ●  %hen num is gi"en as 100.

    ●  %hen num is gi"en as 101D.

    Indi"idually' %or& it out in %ritingF

  • 8/9/2019 11-matrices-cs101-2013

    7/21

    IIT Bombay CS 101 - 2012-2 7

    inary Search - code

    int n8100' A;n

  • 8/9/2019 11-matrices-cs101-2013

    8/21

    IIT Bombay CS 101 - 2012-2 8

    Multidimensional Arrays

    *e can use arrays %ith more than one dimension

    int A;.0

  • 8/9/2019 11-matrices-cs101-2013

    9/21

    IIT Bombay CS 101 - 2012-2 9

    Storage: ro% ma=or

    ● Internally' no distinction !et%een ;

  • 8/9/2019 11-matrices-cs101-2013

    10/21

    IIT Bombay CS 101 - 2012-2 10

    Matri+ manipulations

    4 arrays are commonly used to represent Matrices

    and are (ound in programs such as:

    ● Matri+ multiplication

    ● Oi"en: m + n matri+ A5 n + p matri+ ' the matri+product C 8 A %ill !e m p matri+)

    ● C[i][j] =k=0

    Qn-1A;i

  • 8/9/2019 11-matrices-cs101-2013

    11/21

    IIT Bombay CS 101 - 2012-2 11

    asic operations

    ● Initialize s>uare matri+ to identity

    for (int rx = 0; rx < rows; ++rx) {  for (int cx = 0; cx < cols; ++cx) {

      dmat[rx][cx] = (rx == cx)? 1 : 0; } }

    ● etter code uses t%o loops ,%hy?3● In (irst r+'c+ loop set all elements to zero

    for (int rx=0; rx

  • 8/9/2019 11-matrices-cs101-2013

    12/21

    IIT Bombay CS 101 - 2012-2 12

    Transpose a s>uare matri+ - e+ample

    1 2 3

    4 5 6

    7 8 9

    1 4 3

    2 5 6

    7 8 9

    1 4 3

    2 5 6

    7 8 9

    1 4 7

    2 5 6

    3 8 9

    1 4 7

    2 5 6

    3 8 9

    1 4 7

    2 5 8

    3 6 9

    4o Thin&-Pair-Share (or coding

  • 8/9/2019 11-matrices-cs101-2013

    13/21

    IIT Bombay CS 101 - 2012-2 13

    Transpose a s>uare matri+ - code

    ● 4onKt dou!le transpose !ac& to s>uare oneF

    for (int rx = 0; rx < rows; ++rx) {

      for (int cx =rx+1

    ; cx < cols; ++cx) {  float tmp = fmat[rx][cx];

      fmat[rx][cx] = fmat[cx][rx];

      fmat[cx][rx] = tmp;

      }

    }

    No need to transpose diagonal or below

    Swap [rx][cx]with [cx][rx]

  • 8/9/2019 11-matrices-cs101-2013

    14/21

    IIT Bombay CS 101 - 2012-2 14

    Matri+ "ector multiplication - e+ample

    a00 a01 a02

    a10 a11 a12

    x0

    x1

    x2

    y0

    y1=

    a00*x0 +a01*x1 +a02*x2

    a10*x0 +a11*x1 +a12*x2

    4o Thin&-Pair-Share (or coding

  • 8/9/2019 11-matrices-cs101-2013

    15/21

    IIT Bombay CS 101 - 2012-2 15

    Matri+-"ector multiplication - code

    float A[rows][cols];

    float x[cols] ![rows];"" fill #p A and x

    for (int rx=0; rx < rows; ++rx) {

      ![rx] = 0;  for (int cx=0; cx < cols; ++cx) {

      ![rx] += A[rx][cx] $ x[cx];

      }}

  • 8/9/2019 11-matrices-cs101-2013

    16/21

    IIT Bombay CS 101 - 2012-2 16

    Matri+-matri+ multiplication

    float amat[lsi%&][msi%&] 'mat[msi%&][nsi%&] cmat[lsi%&][nsi%&];

    for (int crx=0; crx

  • 8/9/2019 11-matrices-cs101-2013

    17/21

    IIT Bombay CS 101 - 2012-2 17

    Oaussian elimination method

    Consider t%o e>uations: + 7 y 8 and + 7 .y 8 1

    They can !e represented as:

     ; < ;+< 8 ;<

     ; .< ;y< 8 ;1<

    Oauss elimination method in"ol"es trans(ormations,!y using multiplication and linear com!inations3 toreduce the coe((icient matri+ to upper triangular (orm:

    ;1 < ;+< 8 ;<

    ;0 1< ;y< 8 ;<

    Then sol"ing !y !ac&-su!stitution to get y 8 ' + 8 -)

  • 8/9/2019 11-matrices-cs101-2013

    18/21

    IIT Bombay CS 101 - 2012-2 18

    Oaussian elimination representation

    The pre"ious coe((icient matri+ can !e stored as:

    A;0

  • 8/9/2019 11-matrices-cs101-2013

    19/21

    IIT Bombay CS 101 - 2012-2 19

    Oaussian elimination pseudo-code

    ● 6ead matrices A;

  • 8/9/2019 11-matrices-cs101-2013

    20/21

    IIT Bombay CS 101 - 2012-2 20

    Oaussian elimination program

    ● *or& %ith your neigh!our to %rite the C77 code

    ● Compare %ith: demo11-gauss)cpp

    dli l i

  • 8/9/2019 11-matrices-cs101-2013

    21/21

    IIT Bombay CS 101 - 2012-2 21

    @andling large inputs

    ● Tedious to type the input "alues one at a timeF

    ● Solution: I9 6edirection R

    ● *hen %e e+ecute our program' S assigns it

    three standard (iles - stdin' stdout' stderr

    ● y de(ault' S JconnectsK these to de"ices:

    ● stdin to &ey!oard' stdout and stderr to monitor

    ● )9a)out input)t+t 6edirects stdin (rom a (ile

    ● )9a)out G output)t+t 6edirects stdout to a (ile