22
Prakash Khaire Prakash Khaire The Mandvi Education Society Institute of The Mandvi Education Society Institute of Computer Science Computer Science A A R R R R A A Y Y S S

Mesics lecture 8 arrays in 'c

Embed Size (px)

DESCRIPTION

Array in 'C'

Citation preview

  • 1. ARRAYPrakash KhairePrakash KhaireThe Mandvi Education Society Institute of Computer Science The Mandvi Education Society Institute of Computer ScienceS

2. It is derived data type. An array is a very popular and useful datastructure used to store data elements insuccessive memory locations. It is known as Composite data structure asmore than one element is stored in a sequence. It permits only homogenous data. An array can be declared of any standard orcustom data type. 3. The array of character(strings) type workssomewhat differently from an array ofintegers, floating numbers. Array elements can be accessed by its positionin the array called as index. Values in an array are identified using arrayname with subscripts. It is also known as subscripted variable. 4. The array can be done as underint a[5] = {1, 2, 3, 4, 5}; Calling array elements a[0] Refers to 1st element i.e. 1 a[1] Refers to 2nd element i.e. 2 a[2] Refers to 3rd element i.e. 3 a[3] Refers to 4th element i.e. 4 a[4] Refers to 5th element i.e. 5 5. Size: Number of elements or capacity to store elements in an array is called its size. It is always mentionedin brackets [ ].Type : Type refers to data type. It decides which type of element is stored in the array. It also instructs thecompiler to reserve memory according to data type.Base : The address of the first element(0th) is a base address. The array name itself stores address of thefirst element.Index : The array name is used to refer to the array element. For example, num[x], num is array name and xis index. The value of x begins from 0 to onwards depending on the size of the array. The index value isalways an integer value.Range : Index of an array i.e. value of x varies from lower bound to upper bound while writing or readingelements from an array. For example in num[100] the range of index is 0 to 99.Word : It indicates the space required for an element. In each memory location, computer can store a datapiece. The space occupation varies from machine from machine to machine. If the size of element is morethan word (one byte) then it occupies two successive memory locations. The variable of data type int, float,long need more than one byte in memory. 6. 1. The Declaration int a[5] is the creation of five variables of integer types in memory. Instead of declaring five variables for five values, the programmer.2. All the elements of an array share the same name, and they are distinguished from one another with the help of the element number.3. The element number in an array plays a major role for calling each element.4. Any particular element of an array can be modified separately without disturbing the otherelements. int a[5] = {1, 2, 3, 4, 5}5. Any element of an array a[] can be assigned/equated to another ordinary variable or array variable of its type. 7. 6.An array elements are stored in contiguous memory0 1 23 4102030 4050100010021004 100610087.Once the array is declared, its lowest boundary cannot be changed but upperboundary can be expanded.8.We know that an array name itself is a pointer. Though it is a pointer, it doesnot need * operator. The brackets [] automatically denote that the variableis a pointer.9.All the elements of an array share the same near, and they are distinguishedfrom one another with the help of the element number.10. The amount of memory required for an array depends upon the data type andthe number of elements. The total size in bytes for a single dimensional array iscompared as shown below.Total bytes = sizeof(data type) x size of array 8. 11. The operations such as insertion, deletion of an element can be done with listbut cannot be done with an array. Once an array is created we cannot removeor insert memory location. An element can be deleted, replaced but thememory location remains as it is.12. When an array is declared and not initialization, it contains garbage values. Ifwe declared an array as static, all elements are initialized to zero. 9. A collections of variables are given one variablename using only one subscript and such avariable is called a single-subscripted variable orone dimensional array. Syntaxdata_type ArrayName[size]; data_type : is a valid data type like int, float or charArrayname : is a valid identifiersize : maximum number of elements that can be stored in array 10. Initialization of an array int a[5] Type of array variable is integer. Its variable name is a. 5 is the size of the array. The elements of an array are stored incontiguous memory locations. Element a[0]a[1] a[2]a[3] a[4]Value 5 46 37 Address 20502052 20542056 2058 11. Array index starts with zero The last index in an array is num 1where num is the no of elements in a array int a[9] is an array that stores 9 integersindex 01 2 3 45 6 7 8elementsMemory address 100 102 104 106 108 110 112 114 116 12. An elements of an array must be initialized,otherwise they may contain garbage value. An array can be initialized at either of thefollowing stages At compile time At run time 13. We can initialize the elements of arrays in the same way asthe ordinary variables when they are declared. type array_name[size] = {list of values}; For example, int number[3] = {5,10,15}; float total[5] ={0.0, 15.75, -10.9}; The size may be omitted. In such cases, the compiler allocatesenough space for all initialized elements. int counter[] = {1,1,1,1}; The character array may be initialized in the similar mannerchar name[] = {v,i,k,a,s,0}; orchar name[] = vikas; 14. An array can be explicitly initialized at run time.void main(){int MyArray[5],i;for(i=0;i