array one two

Upload: mcomputer

Post on 01-Jun-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Array One Two

    1/78

    Array

  • 8/9/2019 Array One Two

    2/78

    Objective

    Understand what is an array

    Learn about types of array

    Declaration Initialization

    Ways to access individual elements

    Other possible operations

  • 8/9/2019 Array One Two

    3/78

    Need for an Array

    ach variable can only be used to store asin!le value at a time"

    "!" int a#

    $oat b#

    %he avera!e of two inte!er numbers !ivenby the user can easily be computed"

    &ut printin! the numbers that are !reaterthan the avera!e' re(uires) *eadin! the numbers +alculatin! the avera!e

    +hec,in! the each number with avera!e"

  • 8/9/2019 Array One Two

    4/78

    +ont-

    .o there is a need to store lar!enumber of variables of same typeunder a sin!le variable"

    asy understandin! of the pro!ram"

    "!"

    %o store /ar,s of 01 students"

    *ecord of sales of 211 salesman"

  • 8/9/2019 Array One Two

    5/78

    De3nition

    An array is a collection of individualdata elements that is

    Ordered ) one can count o4 the elements

    1'2'5'-

    6i7ed in size

    8omo!enous ) all elements have to be of

    the same data type""!" int' $oat' double' char etc"

    It is considered as derived data type"

  • 8/9/2019 Array One Two

    6/78

    Array Applications

    9iven a list of test scores' determine thema7imum and minimum scores"

    *ead in a list of student names andrearran!e them in alphabetical order:sortin!;"

    9iven the hei!ht measurements ofstudents in a class' output the names ofthose students who are taller than

    avera!e"

  • 8/9/2019 Array One Two

    7/78

    %ypes of Array

    One Dimensional Array

    %wo Dimensional Array

    /ulti

  • 8/9/2019 Array One Two

    8/78

    One Dimensional Array

    Array is One Dimensional, there will be asingle subscript or index whose value

    refers to the individual array elements

    which ranges from 0 to (n!", where n isthe total number of elements in the array#

    Also called as single dimensional array

  • 8/9/2019 Array One Two

    9/78

    Declaration One DimensionalArray

    %hree thin!s need to be speci3ed) %he type of data it can hold' i"e" int' $oat' double etc"

    %he number of values it can hold or size' i"e" ma7imumnumber of elements it can hold

    A name of array" .ynta7)

    []

    E.g. intA[10];

    must be an constant or a constante7pression" "

  • 8/9/2019 Array One Two

    10/78

    +ont-

    // array of 10 uninitialized ints

    intA[10];

    -- -- ----A -- -- ---- -- --

    4 5 630 2 8 971

  • 8/9/2019 Array One Two

    11/78

    +ont-

    Declare an array of 21 inte!ers)intA[10]; // array of 10 ints

    %o access an individual element we must apply a

    subscript to array namedA. A subscript is a brac,eted e7pression"

    %he e7pression in the brac,ets is ,nown as the inde7"

    6irst element of array has inde7 1"

    A[0]

    .econd element of array has inde7 2' and so on"A[1] A[!] A["]#

    Last element has an inde7 one less than the size of thearray"

    A[$]

    Incorrect inde7in! is a common error"

  • 8/9/2019 Array One Two

    12/78

    +ont-

    Invalid Declaration

    double 7=>

    no value assi!ned to the size"

    int N#

    double 7=N>

    ?ariable of inte!er data type speci3ed assize of the array

  • 8/9/2019 Array One Two

    13/78

    +ont-

    ?alid Declaration @de3ne N 211

    void main:;

    double 7=N>#

    B 8ere array size is de3ned usin! symbolic

    constant' rather than 37ed inte!er"

    Number of array elements can also be!iven by an e7pression"

    @de3ne N 21 int A=NC2>

  • 8/9/2019 Array One Two

    14/78

    Initializin! One DimensionalArray

    // array of 10 uninitialized ints

    intA[10];

    A["] % 1;

    int& % A["];

    -- -- 1--A -- -- ---- -- --

    4 5 630 2 8 971

    A[4] A[5] A[6]A[3]A[0] A[2] A[8] A[9]A[7]A[1]

  • 8/9/2019 Array One Two

    15/78

    +ont-

    Array has multiple elements' bracesare used to denote the entire array ofvalues and commas are used to

    separate the individual valuesassi!ned to the elements in the arrayinitialization statement as)intA[10] % '111!()"!1*$+,;

    12 5 611A 2 1 83 9 7

    4 5 630 2 8 971

    A[4] A[5] A[6]A[3]A[0] A[2] A[8] A[9]A[7]A[1]

  • 8/9/2019 Array One Two

    16/78

    +ont-

    While initializin!' the size of a one' i#

    GGInput .e!mentprintf:Jnter the elements of the arrayJ;#for:i1# iH21# iCC;

    scanf:JKdJ'Ea=i>;#

    GGOutput .e!mentprintf:Jn %he elements of the array arenJ;#for:i1# iH21# iCC;

    printf:JtKdJ'a=i>;#B

  • 8/9/2019 Array One Two

    21/78

  • 8/9/2019 Array One Two

    22/78

    Other Way

  • 8/9/2019 Array One Two

    23/78

    @includeHstdio"hvoid main:;

    int a=50>' i' n#printf:Jnter the value of n) J;#scanf:JKdJ'En;#

    GGInput .e!mentprintf:Jnter the elements of the arrayJ;#for:i1# iHn# iCC;

    scanf:JKdJ'Ea=i>;#

    GGOutput .e!mentprintf:Jn %he elements of the array arenJ;#for:i1# iHn# iCC;

    printf:JtKdJ'a=i>;#B

  • 8/9/2019 Array One Two

    24/78

    7ample

    &i!!est amon! !iven Mn inte!ernumber" As Mn numbers are to be considered' all these elements are

    read into an array :say a; and thenthe biggest/maximumamon! all these is found"

    %o start with the very 3rst element a= 1 > is assumed to be the

    ma7imum :max;"

    %hen this maxis comparedwith the elements startin! from

    the 5ndlocation i"e" startin! from the elements at thelocations a=2>' a= 5 >' a= P >'-' a=nQ 2>' one by one"

    If at any comparison sta!e the max is found to be smallerthan a=i> then the contents of a=i> is copied into maxand thecomparison process is continued till the end"

    At the end the value of the biggestelement will be availablein max"

  • 8/9/2019 Array One Two

    25/78

    Program:

    @includeHstdio"h@includeHconio"h

    void main:;

    int a=50>'i'n'ma7#

    clrscr:;#

    printf:JInput the value of n) J;#

    scanf:JKdJ'En;#

    GGInput .e!mentprintf:JInput the array elementsnJ;#

    for:i1#iHn#iCC;

    scanf:JKdJ'Ea=i>;#

  • 8/9/2019 Array One Two

    26/78

    GGinitially a=1> is assumed as ma7imum

    ma7 a=1>#

    GG+omparison starts with 5nd element of the array

    for:i2#iHn#iCC;

    if:ma7Ha=i>;

    ma7 a=i>#

    B

    printf:Jn%he ma7imum is Kd amon! the !iven elements

    of the arrayJ' ma7;#!etch:;#

    B

  • 8/9/2019 Array One Two

    27/78

    Output

    Input the value of n) 0

    Input the array elements25 R 2S T P

    %he ma7imum is 2S amon! the !iven elements of the array

  • 8/9/2019 Array One Two

    28/78

    7ample

    %o 3nd the avera!e of a !iven set ofn numbers" All the !iven numbers are stored in an array'

    say a"Averageis found out by 3rst summingall the

    elements and then dividin! the sumby totalnumber of elements Mn"

    %his process can be mathematicallyrepresented as

  • 8/9/2019 Array One Two

    29/78

    Program:

    @includeHstdio"h@includeHconio"h

    void main:;

    int a=50>'i'n'sum1#

    $oat avera!e#

    clrscr:;#

    printf:JInput the value of n) J;#

    scanf:JKdJ'En;#

    GGInput .e!ment

    printf:JInput the array elementsnJ;#

    for:i1#iHn#iCC;

    scanf:JKdJ'Ea=i>;#

  • 8/9/2019 Array One Two

    30/78

    GGG3ndin! the sum of all elements

    for:i1#iHn#iCC;sum sum C a=i>#

    avera!e :$oat;sumGn#

    printf:JnAvera!e K"5fJ' avera!e;#

    !etch:;#

    B

    Output

    Input the value of n) 0

    Input the array elements25 R 2S T P

    Avera!e R"T

  • 8/9/2019 Array One Two

    31/78

    7ample

    .earchin! As a set of inte!er elements are to be handled' it is

    ta,en as an array' say a"

    8ere the problem is to searchfor a re(uired

    element in the !iven array" %he re(uired element is!enerally ,nown as the search key" Let this search,ey be represented by a variable called key"

    %he searchin! process for the ,eys presence in thearray starts from the be!innin! of the array andcontinue till either the key is availableor tillthe end of the arrayis reached" %herefore' thismethod is ,nown as linear searchor sequentialsearch method"

  • 8/9/2019 Array One Two

    32/78

    +ont-

    Search key = 36

    Search key = 30

  • 8/9/2019 Array One Two

    33/78

    Program:

    @includeHstdio"h

    @includeHconio"h

    void main:;

    int a=51>',ey'i'n'$a! 1' pos 1#

    clrscr:;#

    printf:JInput the value of n) J;#

    scanf:JKdJ'En;#

    GGInput .e!ment

    printf:JInput the array elementsnJ;#

    for:i1#iHn#iCC;

    scanf:JKdJ'Ea=i>;#

    printf:Jnnter ,ey number) J;# GG*eadin! the ,eyelement

    scanf:JKdJ'E,ey;#

  • 8/9/2019 Array One Two

    34/78

    for:i1#iHn#iCC;

    GGWhether element is e(ual to ,ey number or not

    if:a=i> ,ey;

    $a! 2#

    pos i#

    brea,#

    B

    B

    if:$a! 2;

    printf:JKd is found in position number KdJ',ey'posC2;#else

    printf:JKd is not found in the arrayJ',ey;#

    !etch:;#

    B

  • 8/9/2019 Array One Two

    35/78

  • 8/9/2019 Array One Two

    36/78

    7ercise

    %o 3nd the ma7imum and minimumamon! the !iven elements in anarray"

    %o read n inte!ers:zero' Cve'

  • 8/9/2019 Array One Two

    37/78

    7ample

    Write an al!orithm to sort set ofnumbers in ascendin! order"

    Sortingis any process of arran!in!

    elements accordin! to a certain se(uence" &ubble .ort

    lements in consecutive locations are comparedfrom the be!innin! to the end of the array"

    If they are in the re(uired order' nothin! is doneand the comparison of ne7t set of consecutiveelements is made"

  • 8/9/2019 Array One Two

    38/78

    7ample

    One pass has put the lar!est elementin its place"

  • 8/9/2019 Array One Two

    39/78

    Program:

    @includeHstdio"h@includeHconio"h

    void main:;

    int a=50>' i' n' j' temp#clrscr:;#

    printf:Jnter the value of n) J;#scanf:JKdJ'En;#

    GGInput .e!mentprintf:Jnter the elements of the arraynJ;#for:i1# iHn# iCC;

    scanf:JKdJ'Ea=i>;#

  • 8/9/2019 Array One Two

    40/78

    Program:

    GG%his loop performs re(uired no" of passesfor:i1#iHn#a=j> a=jC2>#a=jC2> temp#

    BB

    B

  • 8/9/2019 Array One Two

    41/78

    Program:

    GGOutput .e!mentprintf:Jnn .orted elements arenJ;#

    for:i1# iHn# iCC;printf:JKdtJ'a=i>;#

    !etch:;#B

    Output:nter the value of n) 0

    nter the elements of the array

    5 X T 0

    .orted elements are

    5 0 T X

  • 8/9/2019 Array One Two

    42/78

    7ample

    Write a pro!ram in c to cyclicallyrotate the elements in array" Fro!ramshould accept a choice in which

    direction to rotate i"e" left to ri!ht"Dependin! on choice it shouldperform cyclic rotation"

    .uppose array A contains elements

    A[4]

    430 21

    2 41A 5

    A[3]A[0] A[2]A[1]

    $

  • 8/9/2019 Array One Two

    43/78

    +ont-

    If choice is rotate left then outputshould be

    If choice is rotate ri!ht then outputshould be

    A[4]

    430 21

    3 52A 1

    A[3]A[0] A[2]A[1]

    %

    A[4]

    430 211 35A 4

    A[3]A[0] A[2]A[1]

    &

    Program:

  • 8/9/2019 Array One Two

    44/78

    Program:@includeHstdio"h@includeHconio"hvoid main:;

    int a=50>' i' n' r' temp#printf:Jnter the value of n) J;#scanf:JKdJ'En;#

    GGInput .e!ment

    printf:Jnter the elements of the arraynJ;#for:i1# iHn# iCC;scanf:JKdJ'Ea=i>;#

    GG+hoice inputprintf:Jn2" Left J;#

    printf:Jn5" *i!ht J;#printf:Jnnter rotation choice from above) J;#scanf:JKdJ'Er;#

    Program:

  • 8/9/2019 Array One Two

    45/78

    gswitch:r;

    case 2)

    temp a=1># GG+opyin! 2stelement of array to variable"

    GGLoop for copyin! other element to proper position

    for:i2#iHn#iCC;a=i a=i>#

    a=n temp

    brea,#case 5)temp a=n# GG+opyin! :n

  • 8/9/2019 Array One Two

    46/78

    Program:

    GGOutput .e!ment

    printf:Jnn .orted elements arenJ;#

    for:i1# iHn# iCC;

    printf:JKdtJ'a=i>;#

    !etch:;#

    B

    Output:

    nter the value of n) 0nter the elements of the array) 2 5 P S 0

    2" Left to *i!ht

    5" *i!ht to Left

    nter rotation choice from above) 2

    %he elements of after *otations are)

    5 P S 0 2

  • 8/9/2019 Array One Two

    47/78

    7ercise

    Write a pro!ram to sort list elementsin descendin! order"

  • 8/9/2019 Array One Two

    48/78

    %wo Dimensional Array

    Array with more than one dimensionsare called multidimensional arrays"

    An array of two dimensions calledtwo dimensional array"

    "!" +hess board"

  • 8/9/2019 Array One Two

    49/78

    %wo Dimensional Array

    It is a collection of data elements ofsame data type arran!ed in rows andcolumns :that is' in two dimensions;"

    "!"

    int sales=P>=0>#

  • 8/9/2019 Array One Two

    50/78

    Declaration of 5D Array

    .ynta7) [][]

    E.g. intA[*][*];

    -must bean constant"

    - are

    the sizes of the arrays first andsecond dimension.

  • 8/9/2019 Array One Two

    51/78

    Initializin! 5D Array

    %he no" of subscript determines thedimensionality of an array"

    %wo dimension can be named asrow:2stdimension; and column:5nddimension;"

    "!"int a=P>=5> 2'2' 5'S'

    P'XB#

    int a=P>=5> 2'2B' 5'SB' P'XBB#

    int a=P>=5> 2'2' 5'S' P'XB#

    int a= >=5> 2'2' 5'S' P'X'0'TB#

  • 8/9/2019 Array One Two

    52/78

    Accessin! 5D Array

    %wo dimensional array is nothin! but matri7"

    Let Abe a !iven matri7' havin! mrows andncolumns i"e" a m7 nmatri7

    2stelement of matri7 is accessed as A=1>=1>"

    Last element of matri7 is accessed as A=m=n"

    *eadin! and Frintin! 5D

  • 8/9/2019 Array One Two

    53/78

    *eadin! and Frintin! 5DArray

    The general element of this matrix is aij"

    8ere the 3rst subscript irepresents a row and

    the second subscriptjrepresents a column" %his matri7 could be read in row-wise' i"e" in

    the order a11' a12' a15' a21' a22and so on' or

    could be read in column-wise' i"e" in the

    order a11' a21' a12' a22and so on" &ut a matri7 is always printed row

  • 8/9/2019 Array One Two

    54/78

    @includeHstdio"hvoid main:;

    int a=21>=21>' i' j' m' n#printf:Jnter the value of m E n) J;#scanf:JKdKdJ'Em'En;#

    GGInput .e!mentprintf:Jnter the elements of the matri7J;#for:i1# iHm# iCC;for:j1# jHn#jCC;scanf:JKdJ'Ea=i>=j>;#

    GGOutput .e!mentprintf:Jn %he elements of the array arenJ;#for:i1# iHm# iCC;for:j1#jHn#jCC;printf:JKdtJ'a=i>=j>;#printf:YnZ;#B

    B

  • 8/9/2019 Array One Two

    55/78

    Output)

    nter the value of m E n) 5 5nter the elements of the array 2 5 P S

    %he elements of the array are

    2 5

    P S

  • 8/9/2019 Array One Two

    56/78

    %ranspose

    %ranspose of !iven matri7

    LetAbe the !iven matri7 ofsize mby n"

    Also let Bis the transpose of the !ivenmatri7" %he transpose of a matri7 isobtained by interchanging its rowelements and column elements"

    "!"

    ][1A =

    0 5 P 0 S RA S 2 & 5 X

    R X T P 2 T

  • 8/9/2019 Array One Two

    57/78

    &y comparin! the two representations

    of B!iven above we see that any elementof Bi"e" bijis e(ual to aji"

    %herefore' the relation bij= ajiis

    used repeatedlyto !enerate all the elements

    of the transposed matrix" Note if the size of matri7 is :m[ n; then the

    size of its transpose will be :n[ m;"

  • 8/9/2019 Array One Two

    58/78

    Fro!ram)

    @includeHstdio"hvoid main:;

    int a=21>=21>' b=21>=21>'i'j'm' n#

    printf:Jnter the value of m E n) J;#scanf:JKdKdJ'Em'En;#

    GGInput .e!mentprintf:Jnter the elements of the arrayJ;#

    for:i1# iHm# iCC;for:j1#jHn#jCC;scanf:JKdJ'Ea=i>=j>;#

  • 8/9/2019 Array One Two

    59/78

    GG%ransposin!for:i1#iHn#iCC;

    for:j1#jHm#jCC;b=i>=j> a=j>=i>#GGOutput .e!mentprintf:Jn %ranspose of /atri7 AnJ;#

    for:i1# iHn# iCC;for:j1#jHm#jCC;

    printf:JtKdJ'b=i>=j>;#

    printf:JnJ;#B

    B

  • 8/9/2019 Array One Two

    60/78

    Output)

    nter the value of m E n) 5 P

    nter the elements of the array 2 5 P S 0 T

    %ranspose of /atri7 A

    2 S

    5 0

    P T

  • 8/9/2019 Array One Two

    61/78

    Norm

    Norm

    &y de3nition normof a matri7 isthe square root of the sum of the

    squares of all its individual elements"

  • 8/9/2019 Array One Two

    62/78

    +ont-

    .ince all the elementsare to be considered' onehas to consider the !eneral element aij' where ais

    a !iven matri7 of m rowsand n columns"%he square of every elementis obtained by simply

    multiplying it by itself once' i"e" by usin! :aij\ aij;"%he sum of all :aij;5is obtained by the relation)

    After 3ndin! the sumthe norm is obtained by3ndin! the s(uare root of the sum" usin! therelation

  • 8/9/2019 Array One Two

    63/78

    Fro!ram)@includeHstdio"hvoid main:;

    int a=21>=21>' i'j'm' n'sum#$oat norm#

    printf:Jnter the value of m E n) J;#scanf:JKdKdJ'Em'En;#

    GGInput .e!mentprintf:Jnter the elements of the arrayJ;#

    for:i1# iHm# iCC;for:j1#jHn#jCC;scanf:JKdJ'Ea=i>=j>;#

  • 8/9/2019 Array One Two

    64/78

    sum 1#for:i1#iHm#iCC;

    for:j1#jHn#jCC;

    sum sum C a=i>=j> \a=i>=j>#

    norm s(rt:sum;#

    GGOutput .e!mentprintf:Jn Norm of the !iven matri7 is

    K"5fJ'norm;#

    B

  • 8/9/2019 Array One Two

    65/78

    Output)

    nter the value of m E n) 5 5

    nter the elements of the array 2 5 P S

    Norm of the !iven matri7 is 5"11

    /atri7 addition and

  • 8/9/2019 Array One Two

    66/78

    /atri7 addition andsubtraction

    In order to obtain the sum or di4erence oftwo !iven matrices' the sie of the givenmatrices must be same"

    %he sum of two matrices is obtained bysummin! the correspondin! elements of the!iven matrices"

    %he di4erence of two matrices is obtained

    by 3ndin! out the di4erence between thecorrespondin! elements of the !ivenmatrices"

    If

  • 8/9/2019 Array One Two

    67/78

    If

    sum

    %hus every element of the sum matri7 + isobtained usin! the relation cij aijC bijfor all

    values of iandj!

    "i#erence

    %hus every element of the di4erencematri7 "is obtained usin! therelation dij aij< bijfor all values of iandj"

  • 8/9/2019 Array One Two

    68/78

    Fro!ram)@includeHstdio"h

    void main:;

    int a=21>=21>' b=21>=21>'c=21>=21>' i'j'm' n#

    printf:Jnter the value of m E n) J;#

    scanf:JKdKdJ'Em'En;#

    GG*eadin! the matri7 A

    printf:Jnter the elements of the matri7 AnJ;#

    for:i1# iHm# iCC;

    for:j1#jHn#jCC;

    scanf:JKdJ'Ea=i>=j>;#

    GG*eadin! the matri7 &printf:Jnter the elements of the matri7 &nJ;#

  • 8/9/2019 Array One Two

    69/78

    printf: nter the elements of the matri7 &n ;#for:i1# iHm# iCC;for:j1#jHn#jCC;scanf:JKdJ'Eb=i>=j>;#

    GG6indin! the sumfor:i1#iHm#iCC;for:j1#jHn#jCC;c=i>=j> a=i>=j> C b=i>=j>#

    GGOutput .e!mentprintf:Jn *esultant /atri7 +nJ;#for:i1# iHm# iCC;for:j1#jHn#jCC;

    printf:JtKdJ'c=i>=j>;#printf:JnJ;#B

    B

    O t t

  • 8/9/2019 Array One Two

    70/78

    Output)

    nter the value of m E n) 5 5

    nter the elements of the matri7 A

    2 5 P S

    nter the elements of the matri7 &

    0 T R

    *esultant /atri7 +

    T R

    21 25

    /atri7 /ultiplicationGFroduct

  • 8/9/2019 Array One Two

    71/78

    /atri7 /ultiplicationGFroductof 9iven %wo /atri7

    LetAand Bare two !iven matrices ofdimensions :m[ n; and :p[ q;respectively"

    Also let $be theproduct matrix" 6or the multiplication of !iven two

    matrices' number of columns of the %rstmatrix must bee(ual to number of rows of

    the second matrix' i"e" nmust be e(ualtop" %he product matri7 $will be of size:m[ q;"

  • 8/9/2019 Array One Two

    72/78

    Let

    Obviously' Ais a :5 \ P; matri7' i"e" m 5

    and n P Bis a :P [ P; matri7' i"e"p P and q P

    .ince n & p' multiplication is possible" %he

    resultantproduct matrix $will be a m[ q'i"e" 5 [ P matri7"

    %he row elements of + are obtained asfollows)

    +11) A11\ &11C A12\&21C A15\ &51

    +12) A11\ &12C A12\&22C A15\ &52

    + ) A \ & C A \& C A \ &

  • 8/9/2019 Array One Two

    73/78

    %he 3rst subscript of Awill be the 3rstsubscript of Cand the second subscript of B

    will be the second subscript of C" Observe that second subscript of Aand the

    3rst subscript of Bare same and vary from 1upto n

  • 8/9/2019 Array One Two

    74/78

    Fro!ram)@includeHstdio"hvoid main:;

    int a=21>=21>' b=21>=21>'c=21>=21>' i'j','m' n'p'(#printf:Jnter the order of matri7 A : m E n;) J;#scanf:JKdKdJ'Em'En;#

    printf:Jnter the order of matri7 & : p E (;) J;#scanf:JKdKdJ'Ep'E(;#

    if:n ] p;printf:Jn /atrices are not multipliableJ;#e7it:2;#B

    GG*eadin! the matri7 A

  • 8/9/2019 Array One Two

    75/78

    GG*eadin! the matri7 Aprintf:Jnter the elements of the matri7 AnJ;#for:i1# iHm# iCC;

    for:j1#jHn#jCC;scanf:JKdJ'Ea=i>=j>;#

    GG*eadin! the matri7 &printf:Jnter the elements of the matri7 &nJ;#for:i1# iHp# iCC;

    for:j1#jH(#jCC;scanf:JKdJ'Eb=i>=j>;#

    GG6indin! the productfor:i1#iHm#iCC; GG6or each row

  • 8/9/2019 Array One Two

    76/78

    for:j1#jHn#jCC; GG6or each column

    c=i>=j> 1#for:,1#,Hn#,CC; GGfor each matri7 elementc=i>=j> c=i>=j> C a=i>=,> \ b=,>=j>#B

    BGGOutput .e!mentprintf:Jn *esultant /atri7 +nJ;#

    for:i1# iHm# iCC;for:j1#jH(#jCC;printf:JtKdJ'c=i>=j>;#

    printf:JnJ;#B

    B

    Output)

  • 8/9/2019 Array One Two

    77/78

    Output)

    nter the order of matri7 A : m E n;) 5 5

    nter the order of matri7 & : p E (;) 5 5

    nter the elements of the matri7 A

    2 5 P S

    nter the elements of the matri7 &

    0 T R

    *esultant /atri7 +

    2X 55

    SP 01

    i

  • 8/9/2019 Array One Two

    78/78

    7ercise

    Write a pro!ram which will accept 5dimensional s(uare matri7 and 3nd outtranspose of it" Fro!ram should not ma,euse of another matri7"

    Write a pro!ram which will accept 5dimensional s(uare matri7 and 3nd outthe trace of it" : traceof a matri7 is !iven

    by the sum of all the elements on itsprinciple diagonal" ;