FCP 4 Arrays and Strings

download FCP 4 Arrays and Strings

of 8

Transcript of FCP 4 Arrays and Strings

  • 8/19/2019 FCP 4 Arrays and Strings

    1/17

     Arrays and Strings

    Introduction

  • 8/19/2019 FCP 4 Arrays and Strings

    2/17

    Many applications require multiple data items that have

    common characteristics. – In mathematics, we often express such groups of data

    items in indexed form:

      • x1 , x , x! , ", xn 

     #rray is a data structure which can represent a collection of

    data items which have the same data type $float%int%char&

  • 8/19/2019 FCP 4 Arrays and Strings

    3/17

     Example: Finding Minimum of Numbers

      3 numbers

    if ((a

  • 8/19/2019 FCP 4 Arrays and Strings

    4/17

    The Problem

    'uppose we have 1( num)ers to handle.

     *r (.

     *r 1((.

    +here do we store the num)ers -se 1(( varia)les

    ow to tac/le this pro)lem

     'olution:

     – -se arrays.

    0onstruct one varia)le $called array or su)scripted varia)le&capa)le of storing or holding all the hundred $any no& values.

  • 8/19/2019 FCP 4 Arrays and Strings

    5/17

     Arrays

     #n array is a collective name given to a group of similar

    quantities2. +hat is important is that the quantities must )e similar2

    percentage mar/s of 1(( students

    or salaries of !(( employees, or 

    ages of 3( employees.

    4hese similar elements could )e all ints, or all floats, or all

    chars, etc.

    -sually, the array of characters is called a string2, whereas

    an array of ints or floats is called simply an array.

  • 8/19/2019 FCP 4 Arrays and Strings

    6/17

    Using Arrays

     #ll the data items constituting the group share the same

    name.int x51(67

     Individual elements are accessed )y specifying the index

    i.e. 8ach mem)er in the group is referred to )y its position in

    the group.  In 0 the counting of elements )egins with ( and not with 1.

  • 8/19/2019 FCP 4 Arrays and Strings

    7/17

    Delaring Arrays

    Like variables, the arrays that are used in a program

    must be declared before they are used. General syntax:

      type arrayname !si"e#$

     – type specifies the type of element that %ill becontained in the array &int, float, char, etc.'

       – si"e is an integer constant %hich indicates the

    maximum number of elements that can be stored

    inside the array

     int marks!(#$

     – marks is an array containing a maximum of (

    integers.

  • 8/19/2019 FCP 4 Arrays and Strings

    8/17

     )xamples:

    int x!*+#$char line!+#$

    float points!*(+#$

    char name!3(#$

    If %e are not sure of the exact si"e of the array, %e can

    define an array of a large si"e.

    int marks!(+#$

    though in a particular run %e may only be using, say, *+elements.

  • 8/19/2019 FCP 4 Arrays and Strings

    9/17

    !o" an array is stored in memory#

    -tarting from a given memory location, the successive

    array elements are allocated space in consecutive memorylocations.

    • x: starting address of the array in memory

    • k: number of bytes allocated per array element

     – a!i# is allocated memory location at address x i/k

    int a!*+#$ starting address is *+0+

    then a!4# is allocated memory location at address *+0+ 4/0

    i.e. 0+0

  • 8/19/2019 FCP 4 Arrays and Strings

    10/17

    Aessing Array Elements

    1 particular element of the array can be accessed by

    specifying t%o things:

     – 2ame of the array.

     – Index &relative position' of the element in the array.

    In , the index of an array starts from "ero.

    )xample:

     – 1n array is defined as int x!*+#$

     – he first element of the array x can be accessed as

    x!+#, fourth element as x!3#, tenth element as x!5#,etc.

  • 8/19/2019 FCP 4 Arrays and Strings

    11/17

    A $arning 

    In , %hile accessing array elements, array bounds arenot checked.

    )xample:

    int marks!(#$:

    :

    marks!# 6 7($

     – he above assignment %ould not necessarily cause anerror.

     – 8ather, it may result in unpredictable program results.

  • 8/19/2019 FCP 4 Arrays and Strings

    12/17

    %nitiali&ation of Arrays

    General form:

    type arrayname!si"e# 6 9 list of values $ )xamples:

    int marks!(# 6 970, 3, ;(, +, 7;$

    char name!4# 6 9

  • 8/19/2019 FCP 4 Arrays and Strings

    13/17

    'ontd(

     – he si"e may be omitted. In such cases the compiler

    automatically allocates enough space for all initiali"edelements.

    int flag! # 6 9*, *, *, +$

    char name! # 6 9

  • 8/19/2019 FCP 4 Arrays and Strings

    14/17

    !o" to read the elements into array#

    ?y reading them one element at a time

    for &@6+$ @A0($ @'

    scanf &BCfD, Ea!@#'$

    he ampersand &E' is necessary.

    he elements can be entered all in one line or in

    different lines.

     Fou cannot directly scanf or printf arrays

    printf &BD, a'$

  • 8/19/2019 FCP 4 Arrays and Strings

    15/17

    !o" to print the elements of an array#

    ?y printing them one element at a time.

    for &@6+$ @A0($ @'

    printf &BHn CfD, a!@#'$

     – he elements are printed one per line.

    printf &BHnD'$

    for &@6+$ @A0($ @'

    printf &B CfD, a!@#'$

     – he elements are printed all in one line &starting

    %ith a ne% line'.

    i fi d * b i d b

  • 8/19/2019 FCP 4 Arrays and Strings

    16/17

    "rite a program to find a)erage mar*s obtained by a

    lass of +, students in a test(

    void main$ &

    9int avg, sum ( 7

    int i 7

    int mar/s5!(6 7 %; array declaration ;%

    for $ i ( 7 i > &

    9

    printf $ ?@n8nter mar/s ? & 7

    scanf $ ?Ad?, Bmar/s5i6 & 7 %; store data in array ;%

    C

    for $ i ( 7 i > &

    sum sum > mar/s5i6 7 %; read data from an array;%

    avg sum % !( 7

    printf $ ?@n#verage mar/s Ad?, avg & 7

    C

  • 8/19/2019 FCP 4 Arrays and Strings

    17/17

    'harater Arrays and Strings

    char !# 6 9 a, b, h, i, @, i, t, H+ $

    !+# gets the value a, !*# the value b, and so on. he

    last &7th' location receives the null character terminated character arrays are also called strings.

    -trings can be initiali"ed in an alternative %ay. he lastdeclaration is eJuivalent to:

      char !# 6 Kabhi@itK$

    he trailing null character is missing here. automaticallyputs it at the end.

    2ote also that for individual characters, uses single

    Juotes, %hereas for strings, it uses double Juotes.