28
UNIT IV

UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Embed Size (px)

Citation preview

Page 1: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

UNIT IV

Page 2: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Definition of Array:

An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations, sharing a common name but distinguished by subscript(s) values.

Depending on the number of subscripts used, array are classified into 1.one-dimensional arrays(1-d array)

2.two-dimensional arrays(2-d arrays)

One-Dimensional Arrays:

It termed as one-dimensional array or 1-d array. It is used to store a list of values, all of which share a common name & distinguishable by subscript values.

Declaration of One-dimensional Arrays

Syntax:

data-type variable-name[size];Where,

1.Data-type refers to any data type supported by C.

2.variable-name refers to array name & should be valid C identifier.

3.size-indicates number of data items of type data-type grouped together.

Page 3: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Example int a[5];Here, a is declared to be an array of int type and of size five. Five contiguous memory locations are shown below,

Each data item in the array a is identified by the array name a followed by a pair of square brackets enclosing a subscript value.The subscript value ranges from 0 to 4., a[0] denotes first data item,a[1] denotes second data item and a[4] denotes the last data item.

2

a[0]

5

a[1]

6

a[2]

8

a[3]

9

a[4]

Page 4: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Initialization of One-Dimensional Arrays

We can initialize one-dimensional arrays also,i.e., locations of the arrays can be given values they are declared.

Syntax:

data-type variable-name[size]={initializer-list}

Example:

int a[5]={2,5,6,8,9};

Page 5: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Processing One-dimensional Arrays

/* Program for declaration, initialization of a one-dimensional array and display its elements. */

#include<stdio.h>

#include<conio.h>

void main()

{

int i,a[5]={2,3,5,6,7};

clrscr();

printf(“The elements of array a \n”);

for(i=0;i<=4;i++)

printf(“%3d”,a[i]);

getch();

}

Page 6: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Passing Arrays to a function

• An array can be passed to a function as a parameter i.e., the name of the array is used as an argument to a function.

•When an array is passed to a function, the values of the array are not passed to the function,but only the address of the first array element is passed.

Example

Page 7: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

#include<stdio.h>

void main()

{

int i,num[5];

clrscr();

for(i=0;i<5;i++)

{

printf(“Enter the value for number:%d”,i+1);

scanf(“%d”,&num[i]);

}

printf(“\n The original list is:\n”);

for(i=0;i<5;i++)

printf(“%d”,num[i]);

sort(num[]);

printf(“\n The sorted list is:\n”);

for(i=0;i<5;i++)

printf(“\t%d”,num[i]);

getch();

}

sort(int n[])

{

int i,j,temp;

for(i=0;i<4;i++)

for(j=i+1;j<5;j++)

if(n[i]>n[j])

{

temp=n[i];

n[i]=n[j];

n[j]=temp;

}

}

Page 8: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Multidimensional Arrays

An array with more than one subscript is generally called a multidimensional array i.e, arrays of two dimensions(2-d arrays), arrays of three dimensions(3-d arrays), arrays of four dimension(4-d) and so on.

1.Two-dimensional Arrays(2-d arrays)

An array with two subscripts is termed as two-dimensional array.A two dimensional aray can be group of one or more one-dimensional array(s),all of which share a common name(2-d array name) & distinguishable by subscript values.

An two-dimensional arrays are very much associated with matrices & perfect data structures for storing matrices.

Page 9: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Declaration of two-dimensional arrays

Syntax

data-type variable-name[rowsize][col-size]

Where

data-type refers to any valid C data type.

variable-name refers name of array.

rows & colsize indicates the number of elements in each row.

Example

int b[3][3]

0 1 2

0

1

2

b[0][0] b[0][1] b[0][2]

b[1][0] b[1][1] b[1][2]

b[2][0] b[2][1] b[2][2]

Page 10: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Initialization of two-dimensional Arrays

Syntax

data-type variable-name[rowsize][colsize]={initializer-list};

data-type refers to any data type supported by C.

variable-name refers to array name.

rowsize indicates the number of rows

colsize indicates the number of columns.

Example

int a[2][3]={2,3,4,5,6,7}

2 3 4

5 6 7

Page 11: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Processing of two-dimensional arrays

1.See Matrix Addition

2.See Matrix Subtraction

3.See Matrix Multiplication

Three-dimensional Arrays

An array with three subscript is termed as a three-dimensional array.A three-dimensional array is a collection of one or more two-dimensional array is a collection of one or more two-dimensional arrays,all of which share a common name and are distinguishable by values of the first subscript of the three-dimensional array.

Declaration of three-dimensional arrays

Syntax

data-type variable-anme[size1][size-2][size-3]

Where,

data-type refers to any data type supported by C.

variable-name refers to the array name

Page 12: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

size1 indicates the number of tables being grouped together.

size2 indicates the number of rows of each table.

size3 indicates the number of columns of each table.

Example

int a[2][4][5]

whare a is declared to be a 3-d array.

a[0][0][0] indicates data item in first table,first row,first column.

a[1][0][0] indicates data item in second table,first row,first column.

a[1][1][2] indicates data item in second table,second row,thrid column.

Page 13: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

/* To accept the elements of a 3-d array and display item */

#include<stdio.h>

#include<conio.h>

void main()

{

int a[2][2][2],I,j,k;

clrscr();

printf(“Enter the elements of a of order

2*2*2(Two tables of size 2*2) \n”);

for(i=0;i<2;i++)

{

for(j=0;j<2;j++)

for(k=0;k<2;k++)

scanf(“%d”,a[i][j][k]);

}

printf(“\n The 3-d arrary a \n\n”);

for(i=0;i<2;i++)

{

printf(“a-Table %d \n\n”,i+1);

for(j=0;j<2;j++)

{

for(k=0;k<2;k++)

printf(“4d”,a[i][j][k]);

printf(“\n”);

}

printf(“\n\n”);

}

getch();

}

Input-Output

Enter the Elements

1 2 3 4 5 6 7 8

The 3-d array a

a-Table 1

1 2

3 4

A-Table 2

5 6

7 8

Page 14: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Arrays and String

In C Strings are defined as arrays of characters.

Example:

char names[7][10]

names[0]=“Raj”;

names[1]=“Jayaram”;

.

.

.

names[6]=“Aishu”;

Page 15: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Example#include<stdio.h>

#include<conio.h”>

void main()

{

char names[5][20];

printf(“Enter five names \n”);

for(i=0;i<5;i++)

scanf(“%s”,names[i]);

printf(“List of Names \n”);

for(i=0;i<5;i++)

printf(“%s \n”,names[i]);

getch();

}Input-Output List of NamesDevaraj Devaraj

Shobana Shobana

Jayanthi Jayanthi

Shanthi Shanthi

Grija Grija

Page 16: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Structure: A structure can be defined to be a group of logically data items, which may be of different types,stored in contiguous memory locations, sharing a common name, but distinguished by its members.Syntax: struct tag-name { data-type member1; data-type member2; data-type member3; }variable 1,variable 2,….variable n;Declaration of Structure Variables struct tag-name variable-name;Example struct emp { int empno; char name[20]; float salary; }e1,e2;

Page 17: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Initialization of Structure Variables

Syntax

struct tag-name variable-name={member1-value,member2-value,

membern-value}

Example

struct emp e={121,”Anju”,20000};

Operation of Structure •Accessing the individual members of a structure variable with help of member operator.

Example

struct emp e;

e.empno=10;

10 is assigned to empno member of e.

strcpy(e.name,”Ram”);

The string “Ram” is copied to name member of e.

Page 18: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

#include<stdio.h>

#include<conio.h>

struct emp

{

int empno;

char name[20];

float salary;

};

void main()

{

struct e1;

clrscr();

printf(“Enter empno,name and salary \n”);

scanf(“%d%s%f”,&e1.empno,e1.name,

e1.salary);

printf(“e1.empno=%d\n”,e1.empno);

printf(“e1.name=%s\n”,e1.name);

printf(“e1.salary=%8.2f\n\n”,e1.salary);

getch();

}

Page 19: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Passing Structure to function•Similar to,we pass arrays of basic type(char,int,float,etc.,)to functions,passint arrays of structures to

functions as arguments.Example

#include<stdio.h>

#include<conio.h>

struct emp

{

int empno;

char name[20];

float salary;

};

void display(struct emp[],int)

void main()

{

struct emp e[10];

int i,n;

clrscr();

printf(“Enter no. of employees \n”);

scanf(“%d”,&n);

printf(“Enter %d employee details \n”,n);

for(i=0;i<n;i++)

{

scanf(“%d%s%f”,&e[i].empno,

&e[i].name,&e[i].salary);

printf(“The list of employees \n”);

getch();

}

Page 20: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

void display(struct emp e[],int n)

{

int i;

for(i=0;i<n;i++)

printf(“%6d %15s %8.2f \n”,e[i].empno,e[i].nme,e[i].salary);

}

Page 21: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Self Referential StructuresWhen a member of a structure is declared as a pointer that points at

the same structure, then it is called self referential structure.Syntax:

struct tag {

datatype1 member1; datatype2 member2; datatype3 member3; ……………………… struct tag *name; } When tag refers to the structure name and name refers to the name of the pointer variable.Example: struct node { char name[20]; struct node *ptr; };

Page 22: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Where,

name->character array

ptr->pointer to another structure of same type called ptr.Hence it is called self-referential structure.

Self-referential structures are very useful in applications that involve linked data structures, such as linked lists and trees.

Page 23: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Union:

• Like a structure, a union is also a derived data type.• The members of a union share a single storage space.

• Only ONE member of each union can be referenced at a time.

• Amount of space allocated for storage is the amount needed for the largest member of the union.

Syntax:

union tag

{

datatype1 member1;

datatype2 member2;

……………..

datatypen membern

}variable1,variable2,….variablen

Page 24: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Example 1 union id { int i; float f; char c; } Example 2#include<stdio.h>void main(){union data{ int i=10; float f=20.5;}; union data d; printf(“%d”,d.i); printf(“%f”,d.f);}

Page 25: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Difference between Structure and Union

STRUCTURE UNION

Every memory has its own memory sapce. All the members use the same memory space to store the values.

It can handle all the members(or) a few as required at a time.

It can handle only one member at a time,as all the members use the same space.

Keyword struct is used. Keyword union is used.

It may be initialized with all its members. Only its first member may be initialized.

Any member can be accessed at any time without the loss of data.

Only one member can be accessed at any time with the loss of previous data.

Different interpretation for the same memory location are not possible.

Different interpretations for the same

More storage space is required. Minimum storage space is required.

Syntax:

structure strut-name

{

}var1..varn;

Syntax:

union union-name

{

}var1..varn;

Page 26: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

User-defined data types:1.Per-defined are int float char void user defined are: structures and union.2.A user-defined data type is typedef and Enumerated Data type.

3.User defined datatype are nothing but those which are constructed by using structure and union.

1.Enumerated Data Type>Enumerated data type offers us a way of inventing our own data type.

Syntax:

enum tag-name

{

enumerator1,

enumerator2,

enumerator3,

.

enumeratorn

};

Page 27: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Example: enum boolean f; f=false;2.Typedef Typedef a facility provided by C,enables us to rename existing built-in data types and user-defined data types and thereby help in increasing the degree of readability of source code.Syntax typedef old-name new-name;Where old-name is the name of the existing data type and new-name is the new name given to the data type identified by the old-name.Example struct emp {

int empno; char name[20]; float salary; }; typdedef struct emp EMP;Variables of struct emp can now be declared using EMP as the type specifier as: EMP ee has been declared to be a variable of struct emp type.

Page 28: UNIT IV. Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations,

Bitwise Operation•In computer programming, a bitwise operation operates on one or two bit patterns or binary numbers at the level of their individual bits.

•Bitwise operations are slightly faster than addition and subtraction operations and significantly faster than multiplication and division operations.

•The bitwise operators utilize something called Binary Math. If you already know binary Math, it is BASE 2.