1 / 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, sharing a common name but distinguished by subscript(s) values.

chet
Download Presentation

UNIT IV

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIT IV

  2. Definition of Array: Anarray 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 & shouldbe valid C identifier. 3.size-indicates number of data items of type data-type grouped together.

  3. 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.

  4. 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};

  5. 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(); }

  6. 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

  7. #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; } }

  8. Multidimensional Arrays Anarray 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.

  9. 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

  10. 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}

  11. 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

  12. size1 indicatesthe 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.

  13. /* 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

  14. 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”;

  15. 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 Names Devaraj Devaraj Shobana Shobana Jayanthi Jayanthi Shanthi Shanthi Grija Grija

  16. 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;

  17. 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.

  18. #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(); }

  19. 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(); }

  20. 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); }

  21. Self Referential Structures When 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; };

  22. 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.

  23. 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

  24. 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); }

  25. Difference between Structure and Union

  26. User-defined data types: 1.Per-defined are int float char void user defined are: structures and union.2.Auser-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 };

  27. 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 e e has been declared to be a variable of struct emp type.

  28. 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.

More Related