0
Upcoming SlideShare
×

# Mesics lecture 8 arrays in 'c'

1,411

Published on

Array in 'C'

Published in: Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
1,411
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
128
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Transcript of "Mesics lecture 8 arrays in 'c'"

1. 1. A R R A Y Prakash Khaire Prakash KhaireThe Mandvi Education Society Institute of Computer Science The Mandvi Education Society Institute of Computer Science S
2. 2. • It is derived data type.• An array is a very popular and useful data structure used to store data elements in successive memory locations.• It is known as “Composite data structure” as more than one element is stored in a sequence.• It permits only homogenous data.• An array can be declared of any standard or custom data type.
3. 3. • The array of character(strings) type works somewhat differently from an array of integers, floating numbers.• Array elements can be accessed by its position in the array called as index.• Values in an array are identified using array name with subscripts.• It is also known as subscripted variable.
4. 4. • The array can be done as under int 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. 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. 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 other elements. 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. 7. 6. An array elements are stored in contiguous memory 0 1 2 3 4 10 20 30 40 50 1000 1002 1004 1006 10087. Once the array is declared, its lowest boundary cannot be changed but upper boundary can be expanded.8. We know that an array name itself is a pointer. Though it is a pointer, it does not need ‘*’ operator. The brackets “[]” automatically denote that the variable is a pointer.9. All the elements of an array share the same near, and they are distinguished from one another with the help of the element number.10. The amount of memory required for an array depends upon the data type and the number of elements. The total size in bytes for a single dimensional array is compared as shown below. Total bytes = sizeof(data type) x size of array
8. 8. 11. The operations such as insertion, deletion of an element can be done with list but cannot be done with an array. Once an array is created we cannot remove or insert memory location. An element can be deleted, replaced but the memory location remains as it is.12. When an array is declared and not initialization, it contains garbage values. If we declared an array as static, all elements are initialized to zero.
9. 9. • A collections of variables are given one variable name using only one subscript and such a variable is called a single-subscripted variable or one dimensional array.• Syntax data_type ArrayName[size]; data_type : is a valid data type like int, float or char Arrayname : is a valid identifier size : maximum number of elements that can be stored in array
10. 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 in contiguous memory locations. Element a[0] a[1] a[2] a[3] a[4] Value 5 4 6 3 7 Address 2050 2052 2054 2056 2058
11. 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 integers index 0 1 2 3 4 5 6 7 8 elementsMemory address 100 102 104 106 108 110 112 114 116
12. 12. • An elements of an array must be initialized, otherwise they may contain “garbage” value.• An array can be initialized at either of the following stages – At compile time – At run time
13. 13. • We can initialize the elements of arrays in the same way as the 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 allocates enough space for all initialized elements. int counter[] = {1,1,1,1};• The character array may be initialized in the similar manner char name[] = {‘v’,’i’,’k’,’a’,’s’,’0’}; or char name[] = “vikas”;
14. 14. • An array can be explicitly initialized at run time.void main(){ int MyArray[5],i; for(i=0;i<5;i++) { MyArray[i] = i * 10; } getch();}
15. 15. • There could be situations where a table of values will have to be stored. Name/Marks Science Maths English Dhrumit 45 48 42 Uzer 48 44 44 Prakash 20 14 2• C allows us to define such tables of items by using two- dimensional arrays.• The two dimensional array are declared as follows type array_name[row_size][column_size];
16. 16. int MyArray[3][3]; 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,20,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2
17. 17. int MyArray[3][3] = {0,0,0,1,1,1,2,2,2};int MyArray[3][3] = {{0,0,0},{1,1,1},{2,2,2}};int MyArray[][] = { {0,0,0}, {1,1,1}, {2,2,2} }
18. 18. int MyArray[2][3] = {{1,1}, {2}}; In above, the remaining elements will be initialized to zero.int MyArray[3][5] = {{0},{0},{0}}; When all the elements are to be initialized to zero, the above statement can be used.int MyArray[][] = {0,0}; The first two elements will be initialized explicitly while the other elements will be initialized implicitly to zero.
19. 19. int MyArray[2][3] = {{1,1}, {2}}; In above, the remaining elements will be initialized to zero.int MyArray[3][5] = {{0},{0},{0}}; When all the elements are to be initialized to zero, the above statement can be used.int MyArray[][] = {0,0}; The first two elements will be initialized explicitly while the other elements will be initialized implicitly to zero.
20. 20. • C allows arrays of three or more dimensions. The exact limit is determined by the compiler.• The general form of a multi-dimensional array type array_name[s1][s2][s3]…[sm]; • Where si is the size of ith dimension. • For exmaple int MyArray[2][3][4];
21. 21. 0,0 0,1 0, 2 0,3 1,0 1,1 1,2 1,30,0 0,1 0,2 0,31,0 1,1 1,2 1,32,0 2,1 2,2 2,3
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.