arrays,string handling functions and pointers

Upload: rahul-kusuma

Post on 03-Jun-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    1/29

    ARRAYS

    The fundamental data types, namely char, int, float, double are used to store only

    one value at any given time.

    Hence these fundamental data types can handle limited amounts of data.

    In some cases we need to handle large volume of data in terms of reading,

    processing and printing.

    T a process such large amounts of data, we need a powerful data type that would

    facilitate efficient storing, accessing and manipulation of data items .

    c supports a derived data type !nown as array that can be used for such

    applications.

    An array is a fi"ed si#e se$uenced collection of elements of the same data type

    It is simply grouping of li!e type data sl%ch as list of numbers, list of names etc

    Some examples where arrays can be used are

    %& 'ist of temperatures recorded every hour in a day, or a month, or a year.

    (& 'ist of employees in an organi#ation.

    )& 'ist of products and their cost sold by a store.

    *& Test scores of a class of students

    +& 'ist of customers and their telephone numbers.

    ARRAY:

    An array is collection of same data type elements in a single entity.

    r

    An array is collection of homogeneous elements in a single variable.

    It allocates se$uential memory locations.

    Individual values are called as elements

    Types of Arrays:

    - e can use arrays to represent not only simple lists of values but also tables of data in two

    or three or more dimensions.

    ne / dimensional arrays.

    Two / dimensional arrays

    0ultidimensional arrays

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    2/29

    ONE - DIENSIONA! ARRAY:

    A list of items can be given one variable name using only one subscript and such a

    variable is called a single / subscripted variable or a one / dimensional array.

    "Declara#$on of One-D$mens$onal Arrays :

    A 'i!e any other variables, arrays must he declared before they are used.

    The general form of array declaration is Synta"1

    2datatype3 2array4name3 5si#e of array6 7

    The data type specifies the type of element that will be contained in the array such as

    int, float, or char.

    The si#e indicates the ma"imum number of elements that can be stored inside the

    array.

    The si#e of array should be a constant value.

    8"amples1

    float height 5+967

    :eclares the height to be an array containing +9 real elements. Any subscripts 9 to *;

    are valid.

    int group5%967

    :eclares the group as an array to contain a ma"imum of %9 integer constants. charname5%967

    :eclares the name as a character array

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    3/29

    The values to the array elements can be assigned as

    number 596 @ )+7

    number5%6 @ *97number5(6 @ (97

    number5)6 @ +=7

    number5*6 @ %;7

    This would cause the array number to store the values as

    number 596 )+

    number5%6 *9

    number5(6 (9number5)6 +=

    number5*6 %;

    %al$d S#a#emen#s:

    a @ number 596 %97

    number5*6 @ number596 number5(67

    number5(6 @ "5+6 y5I67

    value5B6 @ nul%%ber5i6 -.)1

    Example-l:rite a program to print bytes reserved for various types of data and space

    re$uired for storing them in memory using array.

    C include2stdio.h3

    C include2conio.h3

    main < &

    Dint a5%967

    char c5%967

    float b5%967

    clrscr< &7

    printfthe type int re$uires Ed bytes>, si#eof, si#eof, si#eof, si#eof,si#eof< c&&7

    printf Fn Ed memory locations are reserved for ten float elements>,si#eof

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    4/29

    Ou#pu#:

    The type int re$uires ( bytes

    The type char re$uires % bytes

    The type float re$uires * bytes

    (9 memory locations are reserved for ten int elements

    %9 memory locations are reserved for ten char elements*9 memory locations are reserved for/ten float elements.

    Example-&: rite a program using a single subscripted variable to read and display the

    array elements

    C include2stdio.h3

    C include2conio.h3

    0ain< &

    D

    int i7

    float "5%967

    printf8nter I 9 real numbers1 Fn&7 - reading values into Array -for , J"5i&&7

    G

    - printing of "li6 values -

    printfThe anay elements are1>&7

    for

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    5/29

    ,Mohn>7

    Lompile time initiali#ation may be partial. i.e., the number of initiali#ers may be less

    than the declared si#e. In such cases the remaining elements are initiali#ed to #ero, if

    the array type is numeric and ?O'' if the type is char.

    8"1 int number5+6 @ D%9,(9G7

    ill initiali#e the first two elements to %9 J (9 respectively and the remaining

    elements to #ero.

    Similarlychar city5+& @@ DPG7

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    6/29

    will initiali#e the first element to P and the remaining four to?O''.

    If we have more initiali#ers than the declared si#e, the compiler will produce an error.

    int number5)6 @ DI 9,(9,)9,*9G7

    will not wor!. It is illegal in L.

    )&+ Run T$me In$#$al$(a#$on:

    An array can be e"plicitly initiali#ed at run time .

    This approach is usually applied for initiali#ing long arrays.

    8"1

    /////////

    /////////

    for are initiali#ed to #ero.

    hile remaining +9 elements are initiali#ed to %.9 at run time.

    e can also use a read function such as scanfQ to initiali#e an array.

    8"1int "5)67scanf Ed Ed Ed >, J"596, J"5(&, J"5(6&7

    This will initiali#e array elements with the values entered through the !ey board.

    Lharacter arrays are called strings.

    There is a slight difference between an integer array and a character array.

    In character array?O''

    void main

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    7/29

    while, name5i6, Jname5i6&7

    i 7G

    GOu#pu#:

    Lharacter 0emory 'ocation

    5A6 *9+*

    5R6 *9++

    5R6 *9+B

    5A6 *9+=

    5Y6 *9+K

    Example-&: rogram to find out the largest and smallest element in an array.

    C include2stdio.h3

    Cinclude2conio.h30aine< &

    Dint i,n7

    float a5+96, large, small7 printfsi#e of vectorvalue1>&7

    scanfEd>, Jn&7

    printf Fn vector elements are Fn>&7

    for, Ja5 i 6&7

    large @ a567

    small @ a567

    for

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    8/29

    C include2stdio.h3

    C include2conio.h3

    0ain&7scanfEd>, Jn&1

    printf Fn vector elements are 1 Fn>&7

    for< i@7 i 2 n7 i&

    scanf Ef >, Ja5 i 6&7

    for< i@7 i 2 n/l7 i&

    for< V@i % 7V 2n7 V&

    D

    if< a5 i 6 3 a5 V 6& D

    temp@a5i67

    a5 i 6 @ a5 V 67

    a5V6 @ temp7

    G

    printfFn vector elements in ascending order1 Fn>&7 for< i@7 i2n7 i&

    printf EK.(f>, a5 i6&7

    getch< &7

    G

    Ou#pu#:

    Si#e of ector 1 K ector elements are

    ;%.99 (9.99 %.99 =.99 )*.99 %%.99 /(.99 B.99

    ector elements in ascending order7

    /(.99 %.99 B.99 =.9..W %%.99 (9.99 )*.99 ;%.99

    T>O DIENSIONA! ARRAYS:

    There could be situations where a table at values will lave to be stored.

    Lonsider a student table with mar!s in ) subVects.

    Student 0at hy Lhem

    StudentC% K; == K*

    StudentC( ;K K; K9

    StudentC) =+ =9 K(

    StudentC* B9 =+ K9

    StudentC+ K* K9 =+

    The above table contains a total of %+ values.

    e can thin! this table as a matri" consisting of + rows J ) columns.

    8ach row represents mar!s of student C % in all i >th row and> V >thcolumn.

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    9/29

    E?A@!E:In the above table ()refers to the value K9 .

    L allows us to define such tables of itemX by using two/dimensional arrays.

    Def$n$#$on:

    A list of items can be given one variable name using two subscripts and such avariable is called a two / subscripted variable or a two / dimensional array.

    Two - D$mens$onal arrays can be declared as3

    da#a #ypeB array nameB row s$(e / column s$(e/ 7

    The above table can be defmed in L as

    $n# %)./=/7

    Representation of two :imensional array in memory.

    Lolumn U 9 column /% column/( 596 596 596 5%6 596 5%6

    Row.9

    5%6

    Row.%

    Initiali#in( Two/ :imensional Arravs1

    'i!e the one U dimensional arrayas, two dimensional arrays may be initiali#ed by

    foolowing their declaration with a list of initial values enclosed in braces.

    $n# #able&/ =/ 0 1242424l4l4l67

    This initiali#es the elements of first row to #ero and the second row to one.

    This initiali#ation is done row by row.

    The above statement can be e$uivalently written as$n# #able&/=/ 04 DD9,9,9G, DI,I,IGG7

    we can also initiali#e a two / dimensional array in the form of a matri" as shown.

    $n# #able&/ =/ 0 1

    12424264

    1l4l4l 6

    67

    Lommas are re$uired after each brace that doses of a row, e"cept in case of last row3

    If the values are missing in an illitiali#er, they are automatically set to #ero.

    8"1 $n# #able &/ =/ 0 1

    K; == K*

    ;K K; K9

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    10/29

    1*4*64 * * 2

    1&6 & 2 2

    67

    This will initiali#e the first two elements. f the first row to one.

    The first clement of the second row to two and all other Llements to #ero. hen all the elements are to be initiali#ed to #ero, the following Shli/cut method

    may be used,

    Int m5)65+6 @ D D9G,D9G,D9G G1

    The first element of each row is e"plicitly initiali#ed to #ero while the other elements

    are automatically initiali#ed to #ero.

    The following statement will also achieve the same result

    In ma 5)6 5+6 @ D9,9G

    )*+>r$#e a proCram #o d$splay #he elemen#s of #wo d$mens$onalarray3

    C include2stdio.h3C include2conio.h3

    void main&7

    fore i@7 i2)7 i&

    D

    for , a5 i 6 5V 6&7G - end of inner for loop -

    printfFn>&7

    G - end of outer for loop -

    getch< &7

    G - end of main function -

    Ou#pu#:

    8lements of an Array % ( )

    * + B

    = K ;

    )&+ rite a program to display (/:imensional array elements together with theiraddresses.

    C include2stdio.h3

    Cinclude2conio.h3 void main&7

    printfcol/ co /lcol/( F.n>&7

    prinf///////4 //////// ////// n>&1

    printfRoFF 9 n

    forI V1>7 V///)7 i&

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    11/29

    D

    fore V@@7 V2)7 M&

    printtD>27>od 5E,O6>7 a5 i 65V6, JaO65V 6&7

    printf Fn Row Ed>, iI&7

    Ggetch< &7

    G

    Ou#pu#:

    /Array 8lements and address

    Lol / 9 col/ I col /(

    Row 9 % 5*9+(6 (5*9+*6 )5*9+B6

    Row % * 5*9+K6 +5*9B96 B5*9B(6

    Row ( = 5*9B*6 K 5*9BB6 ;5*9BK6

    )=+ rogram to read the matri" of the order upto l" %9 elements and display the same in

    matri" form.C include2stdio.h3

    C include2conio.h3

    void main&7 scanf Ed Ed >, Jrow, Jcol&7

    printfFn 8nter 8lements of matri" A1 Fn>&7

    fore i@97 i2row 7 i&

    D

    for< V@ 97 V2col7 V&

    Dscanf

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    12/29

    int i,V, row, col, a5l9&5l96&, b5l9&5l9&7 clrscr< &7

    printf Fn 8nter order of matti" upto &7

    fore i@97 i 2 row7 i&

    Dfore V@97 V2col7 V&

    scanf Ed>, Ja5 i &5V &&7

    G

    - transposing logic simply copying one matri" elements to another in reverse order -

    fore i@97 i 2 row7 i&

    D

    for&7

    fore i@7 i 2 rl7 i&

    D

    for, Ja5 i 65 V 6&7G

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    13/29

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    14/29

    C include2math.h3

    0aine < &

    D

    int a5%965%96, b5l65%96, c5%65%96, m, n, i,V ,%, !7

    clrscr< & 7

    printf Fn 8nter rder of A matri" 1>&7scanfEd Ed>, Jm, Jn&7 .

    - loop to read values of A matri" -

    rint f &7

    scanf, Jn, J%&7

    - loop to read values of P matri" -

    printf8nter P matri" X>&7

    fore i@7 i2n7 i&for , Jb5 i 65V 6&7

    - loop to multiply two matrices -

    for i@7 i2m7 i&

    D

    for &7G

    getch < & 7G

    Explana#$on:

    hen this program is e"ecuted, the user has to first enter the order

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    15/29

    8nter A matri"

    ( /(

    o *

    8nter order of P matri" 1 ( (

    8nter P matri"

    B (* /+

    Resultant matri" is

    * %*

    %B /(9

    )G+rite a L program to fmd the trace of a given s$uare matri" of order rn"m.

    Solu#$on

    e !now that the trace of a matri" is defined as the sum of the leading diagonal

    elements. -?ote that trace is possible only for a s$uare matri".

    Trace of A matri" @ A%%A((A))@)%(@B

    Row I and columm V are e$ual for a diagonal element.

    - program to find trace of s$uare matri"-

    Cinclud 2stdio.h3

    Cinclude 2conio.h3

    0ain < &

    D

    int a5l65l6, m,i,V, sum7 clrscr< &7

    printf Fn 8nter order of the s$uare matri" 1>& 7

    scanf Ed>, Jm&7

    - loop to read values of A matri" - printf Fn 8nter the matri" Fn>&7

    fore i@7 i2m7i&

    for , Ja< i 6, sum&7

    getch

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    16/29

    B * (

    Trace of the matri" @ B

    H!TI - DIENSIONA! ARRAY

    A list of items can be given one variable name using more that two susbsripts and

    such a variable is called 0ulti U dimensional array.Three D$mens$onal Array:

    A list of items can be given one variable name using three subscripts and

    such a variable is called Three U dimensional array.

    Declara#$on of Three-D$mens$onal Arras :

    Syn#ax:

    da#a#ypeB array J nameB s$(eofno3of#woD$mArray+ s$(eofrow/

    s$(eofcolom/7

    The datatype specifies the type of elements that will be contained in the array,

    such as int, float, or char.

    In$#$al$($nC Three- D$mens$onal Arras:

    'i!e the one/dimensional arrays, three/dimensional arrays may be initiali#ed by

    following their declaration with a list of initial values enclosed in braces.

    int table5(65(65)6 @ D9,9,9,%,%,%B,B,B,=,=,=G7

    This initiali#es the elements of first two dimensional

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    17/29

    There are four important string Handling functions in L language.

    & 7

    This will assign the string >PA? \A'R8> to the character variable city. - -?ote

    that character value li!e

    city @ >PA? \A'R8>7 cannot be assigned in L language.

    )III+ STR,AT) + MHN,TION1

    strcat< & function is used to Voin character, Strings. hen two character strings are

    Voined, it is refened as concatenation of strings.

    8"1

    This will Voin the two strings and store the result in city as >PA?\A'R8/

    ?ote that the resulting string is always stored in the left side string variable.

    )I%+ STR,l0adras>7

    char town5(9& @ >0angalore>7 strcmp

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    18/29

    ?8:8'HI>& without any white space ?8 :8'HI>&,

    The white space in the string will terminate the reading and only >?8> is assigned

    to city. ] To read a string with white spaces gets< & function or a loop can be used as.

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    19/29

    G

    hen this program is e"ecuted, the user has to enter the string and the character to be

    counted in the given string

    Ou#pu#:

    8nter the string1 0ISSISSII hich char to be counted^ S The character

    S occurs * times.this is a boo!>. Lount the appearance of vowels A8IO

    in capital or

    small letters.

    @roCram

    - program to count vowels -

    C include2stdio.h3

    C include2conio.h3

    C include2string.h3

    0aine< &

    char +t5K96. ch7

    int count @ ,. i1c lrscr< &1

    - loop to read a string - I

    printf F%% 8nter the sentence1 Fn>&7

    getB< st&

    1* loop to L%t the vowels in the string -Ifore i@,7 i2strlen

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    20/29

    + vowels are present in the sentence.

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    21/29

    of the second string.

    Lonsider the two character strings.

    St% = >A'HA> St( = >P88T N'St( is greater than stl because the ASLII value of P in >P8TA> is greater than that of

    A in A'HA.

    ?ote that when the first letters of the two strings are identical, the second letters of the strings are

    compared.

    @ROKRA TO @RINT A!@LAPETI,A!!Y KREATER STRINK- C

    include2stdio.h3

    C include2conio.h3

    C include2stirng.h3

    main&7 scanf< > Es >,stl&7

    printf Fn 8nter string (1>&7 scanf< > Es >, st(&7 if, st(&7

    getch< &7

    G hen this program is e"ecuted, the user has to enter the two strings.

    ?ote that strcmp< & function returns a positive value when string % is greater & a negative value when

    string ( is greater.

    Ou#pu#:

    8nter string % 1 ALHA

    8nter string ( 1 P8T A

    P8T A is alphabetically greater string^

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    22/29

    0aine< &

    D

    char names5+965(96, temp5(967

    . int n,i,V7

    clrscr< &1

    printf Fn How many names^>&7 scanfEd>, Jn&7

    printf Fn 8nter the Ed nXllnes one by one F n>,n&7

    fore i@7 i2n7 i&

    scanfEs>, names5 i 6&7

    - loop to arrange names in alphabetical order - I

    Zor< i@7 i2n/l7 i&

    for

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    23/29

    0ain < &

    D

    char st

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    24/29

    I7

    G

    printfFnFn no of char , chs&7 printfFn ?o. of words @ Ed>,

    wds&7

    printfFn ?o of lines @ Ed>, Ins&7

    getch< &7G

    Ou#pu#:8nter the te"t, type c at endhat is a string^ How do you initiali#e it^ 8"plain with e"ample.

    ith e"ample. c

    ?o of char & @3 >city>.

    , > delhi>&7 X m @ .

    $+ s#rnca#)+: To Voin specific number of letters to another string.

    8".

    char s%5%96 @ >?ew>7char s(5%96 @ >:elhi /*%>7

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    25/29

    stmcat?ew :el>.

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    26/29

    Opera#$ons w$#h ,harac#ers:

    L language also supports operations with characters and these functions areavailable in the header file >ctype.h>.

    - ?ote that the value l or positive integer will be returned when a condition is true,

    and value 9 will be returned when condition is false.

    Some ctype.h functions are listed

    below. Zunction1

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    27/29

    8"1

    is"digit

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    28/29

  • 8/13/2019 Arrays,String Handling Functions and Pointers

    29/29