data structure with c -part-2 adt,array, strucure and union

Post on 09-Jul-2015

484 Views

Category:

Engineering

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

third semester Engineering CSE/ISE

TRANSCRIPT

1 Prof. A. Syed Mustafa (Ph.D)

2 Prof. A. Syed Mustafa (Ph.D)

Unit -2

Prof. A. Syed Mustafa (Ph.D)

3

ARRAYS AND STRUCTURES

Prof. A. Syed Mustafa (Ph.D)

4

Topics :

Arrays

Dynamically Allocated Arrays

Structures and Unions

Polynomials

Sparse Matrices

Representation of Multidimensional Arrays

ARRAYS AND STRUCTURES

Prof. A. Syed Mustafa (Ph.D)

5

ARRAYS AND STRUCTURES

Arrays

Prof. A. Syed Mustafa (Ph.D)

6

ARRAYS AND STRUCTURES

Arrays

Prof. A. Syed Mustafa (Ph.D)

7

ARRAYS AND STRUCTURES

Arrays

Prof. A. Syed Mustafa (Ph.D)

8

ARRAYS AND STRUCTURES

Arrays

Prof. A. Syed Mustafa (Ph.D)

9

ARRAYS AND STRUCTURES

Arrays

Prof. A. Syed Mustafa (Ph.D)

10

ARRAYS AND STRUCTURES

Arrays

Prof. A. Syed Mustafa (Ph.D)

11

ARRAYS AND STRUCTURES

Structures

Prof. A. Syed Mustafa (Ph.D)

12

ARRAYS AND STRUCTURES

Structures

Prof. A. Syed Mustafa (Ph.D)

13

ARRAYS AND STRUCTURES

Structures

Prof. A. Syed Mustafa (Ph.D)

14

ARRAYS AND STRUCTURES

Structures

Prof. A. Syed Mustafa (Ph.D)

15

ARRAYS AND STRUCTURES

Structures

Prof. A. Syed Mustafa (Ph.D)

16

ARRAYS AND STRUCTURES

Structures

Prof. A. Syed Mustafa (Ph.D)

17

ARRAYS AND STRUCTURES

Structures

Prof. A. Syed Mustafa (Ph.D)

18

ARRAYS AND STRUCTURES

Structures

Prof. A. Syed Mustafa (Ph.D)

19

ARRAYS AND STRUCTURES

Structures

• Structure variables may be initialized:struct {

char name[25];

int id, age;

char sex;

} s1 = { "Smith, John", 2813, 25, 'M'},

s2 = { "Smith, Mary", 4692, 23, 'F'};

Prof. A. Syed Mustafa (Ph.D)

20

ARRAYS AND STRUCTURES

Structures• A structure can be given a name (a structure tag) for later use:struct student {

char name[25];

int id, age;

char sex;

};

struct student s1, s2;

The two declarations can be combined if desired, with the same effect:struct student {

char name[25];

int id, age;

char sex;

} s1, s2;

Prof. A. Syed Mustafa (Ph.D)

21

ARRAYS AND STRUCTURES

Structures

• As an alternative, we can use typedef to name a structure:

typedef struct {

char name[25];

int id, age;

char sex;

} Student;

Student s1, s2;

Prof. A. Syed Mustafa (Ph.D)

22

ARRAYS AND STRUCTURES

Structures

Prof. A. Syed Mustafa (Ph.D)

23

ARRAYS AND STRUCTURES

Structures

• The members of a structure are accessed by writing first

the name of the structure, then a period, then the name of

the member:

struct student {

char name[25];

int id, age;

char sex;

} s;

strcpy(s.name, “Syed");

s.id = 18193;

s.age = 18;

s.sex = 'M';

Prof. A. Syed Mustafa (Ph.D)

24

ARRAYS AND STRUCTURES

Unions

Prof. A. Syed Mustafa (Ph.D)

25

ARRAYS AND STRUCTURES

Unions

Prof. A. Syed Mustafa (Ph.D)

26

ARRAYS AND STRUCTURES

UnionsSimilar to struct, but only one field is active.

Example: Add fields for male and female.

typedef struct sex_type {

enum tag_field {female, male} sex;

union {

int children;

int beard;

} u;

};

typedef struct human_being {

char name[10];

int age;

float salary;

date dob;

sex_type sex_info;

}

Prof. A. Syed Mustafa (Ph.D)

27

ARRAYS AND STRUCTURES

Unions

Prof. A. Syed Mustafa (Ph.D)

28

ARRAYS AND STRUCTURES

Polynomials

Prof. A. Syed Mustafa (Ph.D)

29

ARRAYS AND STRUCTURES

Polynomials

30

Some of the problems require lot of zeros to be stored

as a part of solution.

Hence storing more number of zeros is a waste of

memory.

Matrices with relatively high proportion of zero entries

are called sparse matrices.

Matrix that contains more number of zero elements are

called sparse matrices.

Sparse matrix is used in almost all areas of the natural

sciences Prof. A. Syed Mustafa (Ph.D)

ARRAYS AND STRUCTURES

Sparse Matrix

31

Prof. A. Syed Mustafa (Ph.D)

ARRAYS AND STRUCTURES

Sparse Matrix

32

Prof. A. Syed Mustafa (Ph.D)

ARRAYS AND STRUCTURES

Sparse Matrix

33

Prof. A. Syed Mustafa (Ph.D)

ARRAYS AND STRUCTURES

Sparse Matrix

top related