Beruflich Dokumente
Kultur Dokumente
4/25/12
Topics
Data Structure Arrays Records & Pointers Multidimensional Arrays Pointer Arrays Record Structure
4/25/12
Data
Data simple values or sets of values Data Item refers to a single unit of values Data item divided into sub items are called group items Data item not divided into sub items are called elementary items For example employees name divided into first
4/25/12
Data
Entity that has certain attributes or properties which may be assigned values. Values may be numeric or non numeric Employee
Age 20
Sex M
SSN 134-24-5533
Entity set Entities with similar attributes The way that data are organized into the hierarchy of fields, records and files reflects the relationship between attributes, entities and entity sets.
4/25/12
Data
Data organized as Fields Records File
Fixed Length Records Variable Length Records
The data may be organized into many such different ways the logical and mathematical model of a particular organization of data is called data structure The way information is organized in the memory of a computer is called a data structure. A data structure helps you to understand relationship of one data element with the other and organize it within the memory. 4/25/12
way of organizing all data items considers not only elements stored but also their relationship to each other. Specifies fallowing
Processing 4/25/12
Preeti S. Patil
4/25/12
Derived from primitive data structure. Emphasize on structuring of a group of homogeneous or heterogeneous data items Eg:
Arrays Lists
Files 4/25/12
The data in data structures are processed by means of certain operations The four main operations:
Traversing Processing each element in the list. Searching Inserting Deleting Sorting Merging
4/25/12
4/25/12
Data Structure
Data Structure : classified as either linear or non linear Linear data structure: if its elements form a sequence or linear list There are two basic ways of representing such linear structures in memory:
One way is to have the linear relationship between the elements represented by means of sequential memory locations e.g. Arrays
The other way linear relationship between the elements represented by means of pointers or links e.g. Linked Lists
4/25/12
Linear Arrays
A linear array list of finite number n of homogeneous data elements, finite collection of similar elements stored in adjacent memory locations. The elements of the array are referenced respectively by an index set consisting of n consecutive numbers The elements of the array are stored respectively in successive memory locations. n indicates total number of elements in the array size or length of the array. Length = Total number of elements = UB LB +1
4/25/12
Linear Arrays
Dynamically: Run time read value of n at run time and then allocate memory while program execution.
4/25/12
Base(Arr)
No need to keep track of the address of every element of Arr. Track only the address of the first element of Arr, denoted by Base (Arr) Using base address, computer calculates address of any element of Arr by using below formula: Loc(Arr[k]) = Base(Arr) + w(K-Lower bound) 4/25/12
Arrays in C
Declaration of an Array in C:
By declaring an array, the specified number of memory locations are allocated in the memory
For example,
104 arr[2]
106 arr[3]
108 arr[4]
arr[0]
arr[1]
4/25/12
Arrays in C
Array initialization:
2000
2000.50 sal[1]
1000 sal[2]
sal[0]
An array of characters is called a string and it is terminated by a null (o) character. char name[3]=abc;
4/25/12
2.
2.
1.
[End of Loop]
4/25/12 Exit
2.
Inserting into a Linear N=4 NAME Arrays Inserting into a Linear Array
LA = Linear Array LB = Lower Bound UB = Upper Bound N = array with N elements K= K is a positive integer such that K<=N. Davis Smith
Brown Johnson
This algorithm Inserts an element ITEM into the Kth position in LA N=4 K=3 INSERT(LA, N, K, Item) NAME Item=Fo [Initialize counter] Set J:=N Brown rd Repeat Steps 3 and $ while J>= K J=N=4 Davis
1. 2. 3.
[Move Jth element downward] Set LA[J+1]:=LA[J] [Decrease counter] Set J:=J-1 [End of step 2 loop]
4.
5.
Linear arrays referenced by one sub scripts Multi Dimensional arrays- referenced by more than one subscript two or three A two dimensional M* N array A is a collection of M*N elements such that each element is specified by pair of integers such as j, k called subscripts The element of A with subscript j and second subscript k is denoted by
Aj,k or A[j][k]
Two dimensional arrays are called matrices with M rows and N columns. 4/25/12
A two dimensional m* n array in memory represented sequential memory locations. Array A can be stored in either of two ways:
as m*n
Column by column- Column major order Row by row Row Subscri major order A
4/25/12
pt (1,1) (2,1) (3,1) (1,2) (2,2) (3,2) (1,3) (2,3) (3,3) (1,4) (2,4) (3,4)
Row Major Order A Subscri pt (1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) (3,2) (3,3) (3,4)
Row 1
Column 1 Column 2
Row 2
Column 3 Column 4
Row3
Search
Linear Search: Compare search element with each element one by one in array Binary Search :
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
3 1
4 2
5 3
3 4
3 5
5 1 6
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14 h i
l o
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3
3 3 4
4 3 5
5 1 6
5 3 7 mi d
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14 h i
l o
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3
3 3 4
4 3 5
5 1 6 h i
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14
l o
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3 mi d
3 3 4
4 3 5
5 1 6 h i
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14
l o
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3
3 3 4 l o
4 3 5
5 1 6 h i
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3
3 3 4
4 3 5
5 1 6
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14
l mi h o d i
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3
3 3 4
4 3 5
5 1 6
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14
l mi h o d i
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3
3 3 4 l o h i
4 3 5
5 1 6
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3
3 3 4 lo hi mi d
4 3 5
5 1 6
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3
3 3 4 lo hi mi d
4 3 5
5 1 6
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14
4/25/12
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].
1 3 1
1 4 2
2 5 3
3 3 4 lo hi mi d
4 3 5
5 1 6
5 3 7
6 4 8
7 2 9
8 4 10
9 3 11
9 5 12
9 6 13
9 7 14
4/25/12
Set BEG:= LB, END:= UB and MID =INT(BEG +END)/2 MID = (1+5)/2 = 3 /*20 < DATA[MID]= /* END = MID-1= 3-1 = 2 */ /* DATA = Repeat steps 3 and 4 while BEG<=END and DATA[MID] ITEM IF ITEM < DATA[MID], then: DATA[3] =30 */ Set END := MID-1. Else: [10,20,30,40,50,60] */ Set BEG:=MID+1.
4/25/12
4.
2.
3.
Pointers
Pointers are special variables which contain the address of another memory location. Pointers are useful in accessing any memory location directly. An address of a memory location is a long integer which is stored in pointer type variable. Adding two pointers or subtracting two pointers gives number of bytes between two memory addresses.
4/25/12
Pointers
& - address operator represents the address of the variable. %u used for obtaining the address = printf(address of X = %u, &X); Declare base variable
int X;
int *P;
Address of variable Establish relation between base Memoryand pointer variable can pointer P one
P = &X;
1011 25 X
1011 P
access the variable X. *P = Value at Pointer P = Value at memory location 1011 = Value of variable X = 25 Pointer Type Variable
Pointers
Another pointer variable can store the address of a pointer variable int a=2; int *b; int **c; b=&a; c=&b; 1011
2
/* c has been declared as a pointer to pointer variable which contains address of pointer variable b */
Address of Memory where value for variable a is stored 1021 1011 b 1021 c Pointer to Pointer Type Variable Pointer Type Variable
Variable name
4/25/12
Pointer Arrays
On array declaration sufficient amount of storage is allocated by the compiler The compiler also defines the name of the array as a pointer to the first element. The name arr acts as a pointer pointing to the first element. arr = &arr[0] =100; int *ip; ip = arr or ip=&arr[0];
arr[0] &arr[0] = 100; arr[1] ip = 2 4/25/12 4 ip+1 =6&arr[1] = 102; 10 ip+2 = &arr[2] = 104; 10 0 2 104 arr[2] 8 106 arr[3]
Call By Value
The process of passing the actual values of variables as arguments to a function is called call by value. Before calling 2 1 program Main()
a (1000)
b (2000)
Printf(Before Calling the function, a and b are %d %d , a, b); Printf(After calling the function, a and b are %d %d, a, b);
After p++ and q++ in } function 3 2 4/25/12 p(5000) Q(8000) After calling function 2 a(1000) 1 b(2000)
Call By Reference
Pass the addresses of the variables as parameters to the function. Before calling program
2 1
Main() { int a =2; int b=1; Printf(Before Calling the function, a and b are %d %d , a, b); 1000 2000 Value(&a,&b); Printf(After calling the function, a and b are %d %d, a, b);
After (*p)++ and (*q)++ in function 1000 2000
a (1000)
b (2000)
In Function
Records
98 99
Record Structures
Collections of data - organized into a hierarchy of fields, records and files. Record collection of related (Not similar) data items, called as field or attribute. File collection of similar records. Record- collection of non homogenous data the data items in a record may have different data types. The data items in a record are indexed by attribute names.
4/25/12
Record Structures
1.
4/25/12
3 Age
Record Structures
1. Student(20) 2. Name 3. Last 3. First 3. MI 2. Test(3) 2. Final 2. Grade To access last name of first student Student.Name.Last[1] To access marks of third test of first student Student.Test[1][3]
4/25/12