Array: Location of Element
Array: Location of Element
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
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[: );
%:>*?>*?
47;<=;<=
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:
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