Sei sulla pagina 1di 5

Array

A linear array is a list of finite number n of homogenous data elements (i.e., data elements of same type). Like ordinary variables arrays too can be initialized during declaration. int num[6 !"#,$,%#,&,$&,&' int n[ !"#,$,%#,&,$&,&' Array elements are stored in contiguous memory locations. (he size of the array should be mentioned )hile declaring it. e.g. n[& Array elements are al)ays counted from * on)ards. Array elements can be accessed using the position of the element in the array. e.g. n[i refers to ith element.

Length of Array
Length ! +, - L, . % +, is the largest inde/, called the upper bound. L, is the smallest inde/, called the lo)er bound.

Location of element
LOC(K) = BASE(LA) + W(K- LB) LA LI EA! A!!A" W - SI#E $AKE B" %A!IABLE LB LOWE! BO& '

O(eration on Array $ra)er*al+ -, -roce**ing each element in the li*t Search+ -, .in/ the location of the element In*ertion -, A//ing a ne0 element to the li*t 'eletion -, !emo)ing an element from the li*t Sorting -, Arranging the element* in or/er 1erging -, Com2ining t0o li*t into a *ingle li*t+

ASSI3 1E $ 4 3!O&- A5 6 A7 8 B5 6 B7

94

0hat is an array 1 2ive an e/ample to sho) the usefulness of an array 1

97 Let the base address of the array is #*** and each element of the array occupies four bytes in the memory, then calculate the address of fifth element in the array1 95 3uppose an array is declared as int arr[%* then ans)er the follo)ing 4uestions5 (i) (ii) (iii) (iv) 9: Length of the array 1 3ize of the array 1 (he address of &th element of the array if the base address is %***. (he value of 56 printf(78d9,arr[: );

</plain the dra) back of array 1


=ate 5 %%>*?>*?

Last date of submission 5 6

%:>*?>*?

$o in*ert an element in an array AL3O!I$>1?


%. [initialize the value of i 3et i! len #. @epeat for i!len do)n to pos [3hift the element do)n by % position 3et a[i.% !a[i [<nd of loop :. [Ansert the element at re4uired position 3et a[pos !num $. [@eset len 3et len!len.% &. =isplay the ne) list of arrays 6. <nd

47;<=;<=

'eleting an element from an array AL3O!I$>1?


%. set item!a[pos #. repeat for B!pos to n6% [shifting elements % position up)ards 3et a[B ! a[B.% [end of loop :. reset n!n6% $. display the ne) list of element of arrays &. end

$ra)er*ing of array AL3O!I$>1?


%. [initialize counter set A at lo)er bound L, #. repeat for i!L, to +, [visit element =isplay a[i [end of loop :. e/it

ASSI3 1E $ 4 3!O&- A4 6 A: 8 B4 6 B:

94 97

0hat is an array 1 2ive an e/ample to sho) the usefulness of an array 1 An automobile company uses an array A+(C to record the number of automobiles sold each year from %D:# through %DE$. @ather than beginning the inde/ set )ith %, it is more useful to begin the inde/ set )ith %D:#. Falculate (i) the length of the array (ii) the address of the fifth element of the array (iii) size of the array 0rite a Hrogram to create an array and display an array using function create() and display().

G:

=ate 5 %6>*?>*? Last date of submission 5 6 %D>*?>*?

Fharacters arrays are often called string

Some *tan/ar/ li2rary f@nction* 3trlen 6 3trcpy 6 3trncpy 6 3trl)r 6 3trcmp 6 3trcat 6 3trrev 6 Iinds length of a string Fopies one string into other Fopies first n character of a string at the end of another converts a string to lo)er case compares t)o strings Appends one string at the end of another reverse a string

Potrebbero piacerti anche