# EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]

## Presentation on theme: "EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]"— Presentation transcript:

EC-211 DATA STRUCTURES LECTURE 2

EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100] specifies an array of 100 integers – Two basic operations: Extraction e.g. a[i] Storage e.g. a[i]=x;

1-D Array Element Access Address of A[i]=base(A)+i*esize Here base(A)=100 and esize=4 A[0] A[1] A[2] A[4] A[5] A[6] 100 Addresses of Individual Elements in int A[7] 104 108 112 116 120

Implementing 1-D Array In C, an array variable is implemented as a pointer variable. E.g. in int b[100], the type of b is “pointer to int” or “int *”.

2-D Arrays Rows as well as columns e.g. int a[3][5] The number of rows or columns is called range of that dimension A 2-D array only a logical data structure, because physical hardware does not have such facility (i.e. memory is linear)

2-D Arrays Thinking in 2 dimensions is convenient to programmers in many situations, e.g. – A map – A checkerboard – Any set of values that are dependent on two inputs; a departmental store that has 20 branches each selling 30 items int sales[20][30];

Implementing a 2-D array Mapping from 2-D logical space to 1-D physical space Two approaches – Row major order – Column major order

Row-Major Order The first row of the array occupies the first set of memory locations reserved for the array, the second row occupies the second set, and so on. A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] Row 0 Row 1 Representation of A[2][3] in Row-major Order

Address of an Element in Row-Major Order Suppose int ar[r1][r2] is stored in row-major order with base address base(ar) and element size esize. The address of element ar[i1][i2] can be calculated by calculating the address of the first element of row i1 and adding the quantity i2*esize

Address of an Element in Row-Major Order The address of first element of row i1 is base(ar)+i1*r2*esize Therefore the address of ar[i1][i2]is base(ar)+(i1*r2)*esize+i2*esize or base(ar)+(i1*r2+i2)*esize

Element Access in Row-Major Order Address of A[i1][i2]=base(A)+(i1*r2+i2)*esize Here base(A)=100, r1=2, r2=3, esize=4 A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] Row 0 Row 1 Representation of A[2][3] in Row-major Order 100 104 108 112 116 120

EXISTING DATA STRUCTURES IN C/C++ STRUCTURES – A structure is a group of items in which each item is identified by its own name – Each item is called a member (or field) of the structure

Structures struct nametype { char first[10]; char midinit; char last[20]; }; struct nametype sname, ename; variables Data type

struct Member Access Dot operator (‘.’), e.g. cout< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3265384/slides/slide_14.jpg", "name": "struct Member Access Dot operator (‘.’), e.g.", "description": "cout<

Struct Legal Operations Assignment between struct type variables is allowed (ANSI C) struct nametype { char first[10]; char mid; char last[20]; }; struct nametype n1, n2; // you can omit the keyword struct strcpy (n1.first,"Muhammad"); n1.mid='Y'; strcpy (n1.last,"Javed"); n2=n1; //struct assignment cout< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3265384/slides/slide_15.jpg", "name": "Struct Legal Operations Assignment between struct type variables is allowed (ANSI C) struct nametype { char first[10]; char mid; char last[20]; }; struct nametype n1, n2; // you can omit the keyword struct strcpy (n1.first, Muhammad ); n1.mid= Y ; strcpy (n1.last, Javed ); n2=n1; //struct assignment cout<

Nested Structures struct addrtype { char straddr[40]; char city[10]; char state[3]; char zip[6]; }; struct nmadtype { struct nametype name; struct addrtype address; }; struct nmadtype nmad1, nmad2;

Structure Implementation The amount of storage set aside for a structure is the sum of storage specified by each of its member types. For instance, struct structtype { int field1; float field2; char field3[10]; }; struct structtype r; Suppose size of int is 4 bytes, size of float is 4, and size of char is 1 byte respectively. Then amount of memory allocated for variable r is ?

Structure Implementation Answer: 18 bytes Usually consecutive storage locations are allocated to members of structure Associated with each member of struct is an offset that is added to the base address of the struct to reach that field.

Download ppt "EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]"

Similar presentations