c Unit 6 Array Last Print

Embed Size (px)

Citation preview

  • 8/18/2019 c Unit 6 Array Last Print

    1/8

    Programming Language Array Unit 6

    Array:

    Arrays are a convenient way of grouping a lot of variables under a single variable name. An

    array is defined using square brackets []. They can be one dimensional, two dimensional or

    more dimensional.

    Array is a collection of similar types of data items.

    For eample marks of students, salary of employees of a company. !n such situation,

    it is convenient to place the data item together giving a common name. Therefore an

    array can be defined as a group of related data items that share common name.

    Arrays are also called derived data type because they are derived from fundamental

    data types.

    Why use arrays?

    Arrays are most useful when they have a large number of elements" that is, in cases where

    it would be completely impractical to have a different name for every storage space in the

    memory. !t is then highly beneficial to move over to arrays for storing information for two

    reasons"

    • The storage spaces in arrays have indices. These numbers can often be  related to

    variables in a problem and so there is a logical connection to be made between an

    array and a program.

    !n #, arrays can be initiali$ed very easily indeed. !t is far easier to initiali$e an arraythan it is to initiali$e twenty or so variables.

    Types of Array:

    1). One dimensional array:The elements of the array can be represented either as a single column or as a single row.

    Declaring one-dimensional array: data%type array%name[si$e]&

    'o far, we(ve been declaring simple variables" the declaration int i&

    declares a single variable, named i, of type int. !t is also possible to declare an array ofseveral elements. The declaration int a[)*]&

    declares an array, named a, consisting of ten elements, each of type int. 'imply speaking,

    an array is a variable that can hold more than one value. +ou specify which of the several

    values you(re referring to at any given time by using a numeric subscript .

    Following are some valid array declarations"int age[)]& float weight[*]& int marks[)**]&

    char section[)-]& char name[)*]&

    Following are some invalid array declarations in c:int value[*]& int marks[*.]& int number[]&

    Array Initialiation !1-Dimentional):

    The general format of array initiali$ation is"

    Ashok Pandey Page 1 of 8

  • 8/18/2019 c Unit 6 Array Last Print

    2/8

    Programming Language Array Unit 6

    data%type array%name[si$e]/0element),element-,1111..,element n2&

    for e"ample:int age[]/0--,33,43,-4,2&

    int weight[]/0,5,66,,4,77,85,)),44,3-2& float a[]/0-,3.,6.8,.8,72&

    char section[4]/09A:,:;:,:#:,:

  • 8/18/2019 c Unit 6 Array Last Print

    3/8

    Programming Language Array Unit 6

    printf@Cnter elements of a matriEn=&

    for@i/*&i>r&iBBfor@J/*&J>c&JBB

    scanf@CDd=,Ga[i][J]& printf@Cthe original matri isEn=&

    for@i/*&i>r&iBB0 for@J/*&J>c&JBB

    0 printf@CDdEt=,a[i][J]&t[i][J]/a[J][i]&

    2printf@CEn=&

    2

    printf@Cthe transpose matri isEn=& for@i/*&i>r&iBB0 for@J/*&J>c&JBB

    0 printf@CDdEt=,t[i][J]& 2printf@CEn=&

    2getch@&

    2

    "ample &: A program to find addition of any t(o matrices of sie *+*.%include>stdio.h?void main@

    0int a[)*][)*], b[)*][)*],c[)*][)*],i,J&

    printf@Cnter elements of first matriEn=&

     for@i/*&i>3&iBB0 for@J/*&J>3&JBB

    0 scanf@CDd=,Ga[i][J]&2

    2printf@Cnter elements of second matriEn=&

    for@i/*&i>3&iBB

    0 for@J/*&J>3&JBB0 scanf@CDd=,Gb[i][J]&

    22

    printf@Cthe sum of two matrices isEn=&for@i/*&i>3&iBB

    0 for@J/*&J>3&JBB

    0 c[i][J]/a[i][J]Bb[i][J]&printf@CDdEt=,c[i][J]&

    2printf@CEn=&

    2getch@&

    2

    #"ample: ,rogram to count the num$er of digits (hite spaces and other

    Kinclude >stdio.h? HL count digits, white space, others LH

    void main@0

    int c, i, nwhite, nother, ndigit[)*]&

    nwhite / nother / *& for @i / *& i > )*& BBi

    ndigit[i] / *&

    while @@c / getchar@ M/ NF

    Ashok Pandey Page 3 of 8

  • 8/18/2019 c Unit 6 Array Last Print

    4/8

    Programming Language Array Unit 6

     if @c ?/ (*( GG c >/ (8(

    BBndigit[c(*(]&else if @c // ( ( OO c // (En( OO c // (Et(

    BBnwhite&

    elseBBnother&

    printf@Pdigits /P&for @i / *& i > )*& BBi

    printf@P DdP, ndigit[i]&printf@P, white space / Dd, other / DdEnP, nwhite, nother&

    2

    The output of this program on itself isdigits / 8 3 * * * * * * * ), white space / )-3, other / 34

    String:'trings are used in programming languages for storing and manipulating tet, such aswords, sentences and names etc. !t is represented as an array of characters and the end of 

    the string is marked by the QRSS @9E*: character.

    'trings are enclosed in double quotes. For eample" Cy college= is a

    string. The representation of the string Cy college= is shown below"

    Ie define string variable as follows" char neme[)*]&Uere, name is an array of character @string of si$e )*. 'o, we can have a name of

    maimum )* characters.

    Ie can initiali$e string as

    char name[]/0:A:,:s:,:h:,:o:,:k:,:E*:2&

    Equivalently we can write aschar name[]/0CAshok=2&

    #"ample: program to display follo(ing pattern

    kka

    katkath

    kathmkathma

    kathman

    kathmandVathmandu

    + c code for a$o'e pattern is +Kinclude>stdio.h?

    Kinclude>string.h?Kinclude>conio.h?

    void main@

    0 char st[]/=kathmandu=&int i/*,J&

    do0

     J/*&while@J>/i

    0

    printf@CDc=,st[J]& JBB&

    2printf@9En=&

    iBB&

    Ashok Pandey Page 4 of 8

  • 8/18/2019 c Unit 6 Array Last Print

    5/8

    Programming Language Array Unit 6

    2while@st[i]M/:E*:&

    2

    /ome /tring manipulation functions:

    # has several built in functions for manipulating strings. To use these built in functions the

    header file 9string.h: must be included.

    1. strlen!): This gives the length of given string including blank spaces and null character.

    #"ample 1: Write a program to read any string and then find out its length.

     HL ;y using strlen@ functionLH

    Kinclude>stdio.h?Kinclude>string.h?

    Kinclude>conio.h?

    void main@

    0 char st[-*]&int l&

    printf@Cnter any string=&gets@st&

     HHor scanf@9Ds=,st&

    l/strlen@st&

    printf@CThe length of string is"Dd=,l&getch@&

    2

    #"ample &: Write a program to read any string and then find out its length (ithout

    using strlen!) function.Kinclude>stdio.h?

    Kinclude>string.h?Kinclude>conio.h?

    void main@0 char st[-*]& int l/*&

    printf@Cnter any string=&

    gets@st&

     HHor scanf@9Ds=,st&while@st[l]M/:E*:

    0 lBB& 2printf@CThe length of string is"Dd=,l&

    getch@&2

    &. strcpy!): this is used to copy the content of one string to another string.!t takes two

    arguments the first is for destination string array and the second is for source string array.

    The source string is copied to the destination string.

    #"ample 1: Write a program to read any string and then copy to another string $y

    using strcpy!) function.Kinclude>stdio.h?

    Kinclude>string.h?

    Ashok Pandey Page 5 of 8

  • 8/18/2019 c Unit 6 Array Last Print

    6/8

    Programming Language Array Unit 6

    Kinclude>conio.h?

    void main@0 char st)[]/=Ashok=&

    char st-[5]&

    strcpy@st-,st)&puts@st-&

    getch@&2

    #"ample &: Write a program to read any string and then copy to another string(ithout using strcpy@ function.

    Kinclude>stdio.h?

    Kinclude>string.h?Kinclude>conio.h?

    void main@0 char st)[]/=Ashok=&

     char st-[5]&

    int l, i/*&

    while@st)[i]M/:E*:

    0 st-[i]/st)[i]&iBB&2

    st[i]/:E*:&puts@st-&

    getch@&

    2

    0. strcat!):This is used to concatenate @Join two strings and resulting string is a single

    string. !t takes two arguments the first is for destination string array and the second is for

    source string array. The source string and the destination strings are concatenated and theresulting string is stored in the first destination string.

    #"ample 1: Write a program to concatenate any t(o strings together $y usingstrcat!) function.

    Kinclude>stdio.h?

    Kinclude>string.h?Kinclude>conio.h?

    void main@0 char st)[]/=Ashok=&

    char st-[]/=Wandey&

    strcat@st),st-&puts@st)&

    getch@&2

    . strcmp!):This is used to compares two strings, character by character. !t accepts two

    strings as parameter and returns an integer whose value is"

    >* if the first string is smaller than the

    second //* if both are equal or same

    ?* if the first string is greater than the second.

    #"ample 1: Write a program to compare any t(o strings $y using strcmp!)function.

    Kinclude>stdio.h?Kinclude>string.h?

    Ashok Pandey Page 6 of 8

  • 8/18/2019 c Unit 6 Array Last Print

    7/8

    Programming Language Array Unit 6

    Kinclude>conio.h?

    void main@0

    char st)[-*], st-[-*]&

    printf@Cnter first string=&gets@st)&

    printf@Cnter second string=&gets@st-&

    if@strcmp@st),st-?*0

    printf@CXreater is Ds=,st)&

    2else

    printf@Cgreater is Ds=,st-&getch@&

    2

    Array of string:

    Xenerally, we use lists of character strings, as for eample name of student in a class, lists

    of names of employs in an organi$ation etc. A list if name can be treated as a table of 

    strings and a two dimensional character array can be used to store the entire list. For

    eample, a character array name232&435 may be used to store a list of names, each of 

    length not more than -* characters.

    'uppose we initiali$e the above array as"char name232&43678A/9O; ;D9##AA=; ;/#=DA;@5

    This is stored in the tabular form as shown in the following fig"

    A ' U N V E*< U Y A J E*

    A Q N N W E*

    Y A Z Q E*

    ' u Y Q < Y A E*

    #"ample: /orting of strings in ascending order:

    Kinclude>stdio.h?

    Kinclude>string.h?Kinclude>conio.h?

    void main@0

    char name[][-*], temp[-*]&

    int i,J&clrscr@&

    printf@Cnter names=&

    for@i/*&i>&iBBgets@name[i]&

    for@i/*&i>)&iBB

    0

    for@J/*&J>i)&JBB0

    if@strcmp@name[J], name[JB)]?*0

    Ashok Pandey Page 7 of 8

  • 8/18/2019 c Unit 6 Array Last Print

    8/8

    Programming Language Array Unit 6

    strcpy@temp, cname[J]&

    strcpy@name[J], name[JB)]&strcpy@name[JB)], temp]&

    22

    2

    printf@Csorted name list is=&for@J/*&J>&JBB

    puts@name[J]&

    getch@&2

    Ashok Pandey Page 8 of 8