Download presentation
Presentation is loading. Please wait.
Published byMarylou Johnston Modified over 9 years ago
1
Introduction of Arrays
2
Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such or can be used to form complex data structures like stack and queue. An array can be defined as an infinite collection of homogeneous (similar data type) elements. This means that an array can store either all integer, all floating point numbers, all characters or any other complex data type but all of same type.
3
Arrays there are some important points to be pointed out about arrays. Arrays are always stored in consecutive memory locations. An array can store multiple value which can be referenced by a single name unlike a simple variable which store one value at a time. Followed by an index or subscript, specified inside a square bracket.
4
Arrays Array name is actually a pointer to the first location of the memory block allocation to the name of the array. An array either be a integer, character or floating data type can be initialized only during declaring time and not afterwards. There is no bound checking concept for arrays in C. it means you can attempt to enter any no of values irrespective of the integer index specified inside square brackets, during declaration of arrays. It is up to programmer to check upper bound of array.
5
Types of Arrays There are three types of arrays: One-Dimensional Arrays Two-Dimensional Arrays Multi-Dimensional Arrays
6
One-Dimensional Array A one-dimensional array is one in which only one subscript specification is needed to specify a particular element of the array. One dimensional array can be declare as follows: data_type array_name [size]; Data type is the type of elements to be stored in the array. Variable name specifies the name of array, it may be given any name like other simple variable.
7
One-Dimensional Array Size the specifies the no of values to be stored in the array, it must be integer value. The index of the array start from o to onwards. Declaration of one-dimensional array: int num[5];
8
One-Dimensional Array It array will store five integer values, its name is num. it can be visualized as show. index data num[0]10 num[1]20 num[2]30 num[3]40 num[4]50 size of array=(upper bound – lower bound) +1 =4+0+1 =5
9
One-Dimensional Array Initialization of one dimensional arrays: int arr[5]={10,20,30}; Here, initialization must be constant value at compile time. Here, arr[0] to arr[2] has assign a value but other element assign a zero by default.
10
One-Dimensional Array :Example Void main() { int arr[5]={10,20,30,40,50}; int i; for (i=0;i<5;i++) { printf(“\n[%d] value is:%d”,arr[i]); }
11
Two-Dimensional Array Two-dimensional arrays are as a grid. Two dimensional array can be declare as follows: data_type array_name [row size] [column size]; the first number in brackets is the number of rows, and second number in brackets is the numbers of column. So the upper left corner of any grid would be element [0][0].
12
Two-Dimensional Array Declaration of two-dimensional array: int arr[3][3]; Here, this example graphical or pictorial representation of 2-D array. Arr[0][0]Arr[0][1]Arr[0][2] Arr[1][0]Arr[1][1]Arr[1][2] Arr[2][0]Arr[2][1]Arr[2][2] 0 1 2 0 1 2 0 1 201
13
Two-Dimensional Array Initialization of Two dimensional arrays: int arr[2][3]={ {10,20,30}, {40,50,60} }; Bellow show the data in 2-D grid. 102030 405060 0 1 0 1 12 0
14
Two-Dimensional Array : Example Void main() { int arr[2][3]={ {10,20,30}, {40,50,60} }; int i,j; for (i=0;i<2;i++) { printf(“\n”); for(j=0;j<3;j++) { printf(“%d ”, arr[i][j]); }
15
Implementation of Two-Dimensional Array in memory A two-dimensional array can be implemented in a programming language in two ways: Row-major implementation Column-major implementation
16
Two-Dimensional Arrays: Row-major Implementation Row-major implementation is a linearization technique in which elements of array are reader from the keyboard row-wise that means the complete first row is stored then the complete second row is stored and so on. For example an array [3][3] is stored in the memory as show bellow: A00A01A02A10A11A12A20A21A22 Row 1 Row 2Row 3
17
Two-Dimensional Arrays Row-major Implementation The storage can be clearly understood by arranging array as matrix as show bellow: A00A01A02 A10A11A12 A20A21A22 Row 1 Row 2 Row 3 a=
18
Two-Dimensional Arrays: Row-major Implementation Address of elements in row major implementation: the computer does not keep the track of all elements of the array, rather it keeps a base address and calculates the address of required element when needed. It calculates by the following relation: address of element a[i][j]=B+W(n(i-l 1 )+(j-l 2 ))
19
Two-Dimensional Arrays: Row-major Implementation Here, B=Base address W=size of each element array element n=the number of column (u 2 -l 2 +1) l 1 the lower bound of row l 2 is lower bound of column u 1 upper bound of row u 2 upper bound of column
20
Two-Dimensional Arrays: Row-major Implementation Example 1: a two-dimensional array defined as a[4:7,-1:3] requires 2 bytes of storage space for each element. If the array is stored in row-major form, then calculate the address of element at location a[6,2] given base address is 100. Sol: B=100, l 1 =4, l 2 =-1, u 1 =7, u 2 =3,W=2(int size) i=6,j=2 and n(no. of col)= u 2 - l 2 +1=3-(-1)+1=5 Address of a[i][j]= B+W(n(i-l 1 )+(j-l 2 )) Address of a[6][2]=100+2(5(6-4) + (2-(-1)) =100+(2(5*2+3) =100+26 =126
21
Two-Dimensional Arrays: Column-major Implementation In column major implementation memory allocation is done column by column that means first the elements of the complete first column is stored then elements of complete second column is stored and so on. For example an array [3][3] is stored in the memory as show bellow: A00A10A20A01A11A21A02A12A22 Col 1 Col 2Col 3
22
Two-Dimensional Arrays Column-major Implementation The storage can be clearly understood by arranging array as matrix as show bellow: A00A01A02 A10A11A12 A20A21A22 Col 1Col 2Col 3 a=
23
Two-Dimensional Arrays: Column-major Implementation Address of elements in Column major implementation: It calculates by the following relation: address of element a[i][j]=B+W(m(j-l 2 )+(i-l 1 ))
24
Two-Dimensional Arrays: Column-major Implementation Here, B=Base address W=size of each element array element m=the number of row (u 1 -l 1 +1) l 1 the lower bound of row l 2 is lower bound of column u 1 upper bound of row u 2 upper bound of column
25
Two-Dimensional Arrays: Column-major Implementation Example 1: a two-dimensional array defined as a[-20:20,10:35] requires one bytes of storage space for each element. If the array is stored in column-major form, then calculate the address of element at location a[0,30] given base address is 500.
26
Two-Dimensional Arrays: Column-major Implementation Sol: B=500, l 1 =-20, l 2 =10, u 1 =20, u 2 =35 W=1 byte i=0,j=30 and m (no. of col)= u 1 - l 1 +1 =20-(-20)+1=41 Address of a[i][j]= B+W(m(j-l 2 )+(i-l 1 )) Address of a[0][30]=500+1(41(30-10)+(0-(-20)) =500+1(41*20+20) =500+1(820+20) =500+840 =1340
27
Multi-Dimensional Array C allows arrays of three or more dimensions. The exact limit is determined by the compiler. The general form of a multi-dimensional array is: type array_name[s1][s2][s3]…..[sm]; Suppose 3-D array can be group of an array of arrays. For example : int a[3][3][2]; Here 3-D array which is collection of three 2-D arrays each contain 3 rows and 2 column.
28
Multi - Two-Dimensional Array For example: int a[3][3][2]={ { {1,2},{3,4},{5,6}}, { {1,2},{3,4},{5,6}}, { {1,2},{3,4},{5,6}} }; Memory representation of above 3-D array is bellow: 123456123456123456 0 th 2-D array 1 st 2-D array2 nd 2-D array
29
Passing Arrays To Functions: One-Dimensional Arrays:Example void show (int a[],int s) { int i; for (i=0;i<s;i++) { printf(“\n[%d] value is:%d”,a[i]); } void main() { int arr[5]={10,20,30,40,50}; show(arr,5); }
30
Passing Arrays To Functions: Two-Dimensional Arrays:Example void show (int a[][3],int r,int c) { int i,j; for (i=0;i<r;i++) { printf(“\n”); for(j=0;j<c;j++) { printf(“%d ”, arr[i][j]); }
31
Passing Arrays To Functions: Two-Dimensional Arrays:Example void main() { int arr[2][3]={ {10,20,30}, {40,50,60} }; show(arr,2,3); }
32
Pointer & Arrays An array is a collection of homogeneous type of element stored in adjacent memory location. Therefore it is used in the situation to store more than one value at a time in a single variable. Array & pointer have a close link in fact array in itself acts like a pointer, as one element in a array is stored adjacent to the previous. Therefore which displaying the elements of array the next adjacent address is automatically called when its previous element is displayed.
33
Pointer & Arrays But in case of using pointers with arrays, the element can be stored in arrays at different location and can be called from that location when needed. Two point must be remember: Array elements are always stored in contiguous memory location. The increment or decrement of pointers leads to increment or decrement of address based on the type of pointer define.
34
Pointer & One-Dimensional Array Pointer along with single dimensional array can be used either to access a single element or it can also be used to access the whole array. Given pointer only points to one particular type, not to all possible type.
35
Pointer & One-Dimensional Array void main() { int *p,i; int arr[5]={10,20,30,40,50}; p=&arr[0]; for(i=0;i<5;i++) { printf(“\n element =%d”,*p); p++; } getch(); }
36
Pointer & Two-Dimensional Array Pointer is used in two-dimensional arrays in the same way as in single dimension. The address of the first element of the matrix can be passed on the pointer and the rest of elements can be displayed. For example : int a[3][2]={{10,20}, {30,40},{50,60}}; The above initialized arrays will be stored in memory in following arrangement:
37
Pointer & Two-Dimensional Array The above initialized arrays will be stored in memory in following arrangement: The program using pointer to handle 2-D array show in word file. Elements102030405060 Matrix Location A[0][0]A[0][1]A[1][0]A[1][1]A[2][0]A[2][1] Address100110031005100710091011
38
Array of Pointer Arrays of pointers refer to homogeneous collection of pointers that is collection of pointers the same data type. The pointer are declared in array from same as other data types. For example an integer pointer array of size 50 can be declared as: int *ptr[50]; A pointer may also be used with more than one array declaration of same data type for an array pointer of size 50 can be used for first array of size 20 and the third of 10.
39
Array of Pointer Here, the pointer will be declared as: int a[20],b[20],c[10]; int *ptr[5]; In above give expression, we can assign the address element of each array as: ptr[0]=&a[0]; ptr[1]=&b[0]; ptr[2]=&c[0]; The program using array of pointers shown in word file.
40
Array of Structure A structure is simple to define if there are only one or two elements, but in case there are too many objects needed for a structure, for example a structure designed to show the data of each student of the class, then it that case the array will be introduced a structure declaration using arrays to define objects is given in next slide.
41
Array of Structure struct student { char name[15]; int rollno; char result[10]; }; Struct student data[60]; The above declaration sets the memory space for 60 objects. To see the detail example in word file.
42
Array within the Structure We have used arrays outside the structures with the object declaration. Arrays can also be used within the structures along with the member declaration. In other words, a member of a structure can be an array type also.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.