Laboratory Manual PROGRAMING LABORATORY Second Year - Information Technology Teaching Scheme Examination Scheme Theory : —— Term Work: 50 Marks Practical : 04 Hrs/Week Practical : 50 Marks Oral : —— Prepared By Prof. S. S. Kare Department of Information Technology Vidya Pratishthan’s College of Engineering Baramati – 413133, Dist- Pune (M.S.) INDIA June 2013 Table of Contents 1 Set 1.1 1.2 1.3 1.4 1.5 1.6 1.7 operations using array Problem Statement . . . . . . . . . . Pre Lab . . . . . . . . . . . . . . . . Hardware and Software Requirement Theory . . . . . . . . . . . . . . . . . Procedure . . . . . . . . . . . . . . . Post Lab . . . . . . . . . . . . . . . . Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 2 2 5 5 2 String operations 2.1 Problem Statement . . . . . . . . . . 2.2 Pre Lab . . . . . . . . . . . . . . . . 2.3 Hardware and Software Requirement 2.4 Theory . . . . . . . . . . . . . . . . . 2.5 Procedure . . . . . . . . . . . . . . . 2.6 Post Lab . . . . . . . . . . . . . . . . 2.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 6 6 6 7 7 15 15 Structure manipulation 3.1 Problem Statement . . . . . . . . . . 3.2 Pre Lab . . . . . . . . . . . . . . . . 3.3 Hardware and Software Requirement 3.4 Theory . . . . . . . . . . . . . . . . . 3.4.1 Introduction to C structure . 3.5 Procedure . . . . . . . . . . . . . . . 3.6 Post Lab . . . . . . . . . . . . . . . . 3.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 16 16 16 17 17 20 24 25 4 Searching and Sorting I 4.1 Problem Statement . . . . . . . . . . 4.2 Pre Lab . . . . . . . . . . . . . . . . 4.3 Hardware and Software Requirement 4.4 Theory . . . . . . . . . . . . . . . . . 4.5 Procedure . . . . . . . . . . . . . . . 4.6 Post Lab . . . . . . . . . . . . . . . . 4.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 26 26 26 27 27 30 30 5 Matrix Operations 5.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Pre Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3 Hardware and Software Requirement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 31 31 31 3 i TABLE OF CONTENTS 5.4 5.5 5.6 5.7 Theory . . . . . Procedure . . . Post Lab . . . . Viva Questions . . . . TABLE OF CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 32 32 32 6 Quick Sort 6.1 Problem Statement . . . . . . . . . . 6.2 Pre Lab . . . . . . . . . . . . . . . . 6.3 Hardware and Software Requirement 6.4 Theory . . . . . . . . . . . . . . . . . 6.5 Procedure . . . . . . . . . . . . . . . 6.6 Post Lab . . . . . . . . . . . . . . . . 6.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 33 33 33 34 34 35 35 7 Sparse Matrix 7.1 Problem Statement . . . . . . . . . . 7.2 Pre Lab . . . . . . . . . . . . . . . . 7.3 Hardware and Software Requirement 7.4 Theory . . . . . . . . . . . . . . . . . 7.5 Procedure . . . . . . . . . . . . . . . 7.6 Post Lab . . . . . . . . . . . . . . . . 7.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 36 36 36 37 37 38 38 8 Singly Linked List 8.1 Problem Statement . . . . . . . . . . 8.2 Pre Lab . . . . . . . . . . . . . . . . 8.3 Hardware and Software Requirement 8.4 Theory . . . . . . . . . . . . . . . . . 8.5 Procedure . . . . . . . . . . . . . . . 8.6 Post Lab . . . . . . . . . . . . . . . . 8.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 39 39 39 40 40 43 43 9 Implementation of DLL 9.1 Problem Statement . . . . . . . . . . 9.2 Pre Lab . . . . . . . . . . . . . . . . 9.3 Hardware and Software Requirement 9.4 Theory . . . . . . . . . . . . . . . . . 9.5 Procedure . . . . . . . . . . . . . . . 9.6 Post Lab . . . . . . . . . . . . . . . . 9.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 44 44 44 45 45 46 46 10 Polynomial Using Circular Linked-List 10.1 Problem statement . . . . . . . . . . . . 10.2 Pre Lab . . . . . . . . . . . . . . . . . . 10.3 Hardware and Software Requirement . . 10.4 Theory . . . . . . . . . . . . . . . . . . . 10.5 Post Lab . . . . . . . . . . . . . . . . . . 10.6 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 48 48 48 49 51 51 References 52 ii List of Tables iii List of Figures 8.1 SLL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 9.1 9.2 DLL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DLL1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 45 10.1 Circular list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 iv Assignment 1 Set operations using array 1.1 Problem Statement Write a program to perform Set operations using array. • Union • Intersection • Difference • Symmetric Difference 1.2 Pre Lab • Concept of Linked-List • Concepts Of structures 1.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) – 900 MB free hard disk space 1 1.4. THEORY Set operations using array • Software Requirement – Turbo c 1.4 Theory 1. Union: The Union of two sets A and B in the set of all those elements that are present in set A or in set B in both. Example: A= {1, 2} B= {1, 2, 5} A∪B = {1, 2, 5} 2. Intersection: The intersection of two sets A and B is the set of all those elements that are in set A and set B. i.e. Common elements of set A and set B. Example: A= {1, 2} B= {1, 2, 3} A∩B = {1, 2} 3. Difference: The elements that are presented in first set but not in second the set is called difference of first set from second set. Example: A={ 1,2,3,4} B={2,3,5} A-B = {1,4} 4. Symmetric Difference: The Symmetric Difference of the two sets A and B denoted by A⊕B is defined as A⊕B = (A∪B) - (A∩B) Example: A= {a, b,e,g} B ={d,e,f,g} A⊕B = {a,b,d,f} 1.5 Procedure I.Pseudo code for union of two sets : int uni(int a[],int b[],int u[],int m,int n) { for(i=0 to m) { c[i]=a[i]; // copy A set as it is to C set } Lab Manual - Programming Laboratory 2 VPCOE, Baramati 1.5. PROCEDURE Set operations using array for(j=0 to n) { f=0; for(k=0 to m) { if(c[k]==b[j]) { f=1; break; } } // end of inner for loop if(f==0) { c[m+z]=b[j]; z++; } } //end of outer for loop Print C Array & return val of m+z . } // end of function. II.Pseudo code for intersection of two sets : int inter(int a[],int b[],int v[],int m,int n) { for(i=0 to m) { for(j=0 to n) { if(a[i]==b[j]) // check for common element { c[k]=a[i]; k++; } } // end of inner for loop Lab Manual - Programming Laboratory 3 VPCOE, Baramati 1.5. PROCEDURE } // end of outer for loop Print } Set operations using array C array & return val. of k. //end of function. III.Pseudo code for difference of two sets : void diff (int a[],int b[],int c[],int m,int n) { k=0,flag=0; for(i=0 to m) { flag=0; for(j=0 to n) { if(a[i]==b[j]) { flag=1; break; } } // end of inner for loop if(flag==0) { c[k]=a[i]; k++; } } // end of outer Print } for loop C array. // end of function. IV.Pseudo code for symmetric difference of two sets : For finding symmetric difference, we have to call difference function once again, but now we have to pass U array, V array & their size as a parameter. i.e. diff(U,V,U1,V1) Lab Manual - Programming Laboratory 4 VPCOE, Baramati 1.6. POST LAB Set operations using array where U1= size of U array V1= size of V array. 1.6 Post Lab Union, intersection , difference and symmetric difference of two sets. 1.7 Viva Questions 1. How set elements can be sotred in computer memory? 2. How symmetric difference is calculated between two sets? Lab Manual - Programming Laboratory 5 VPCOE, Baramati Assignment 2 String operations 2.1 Problem Statement Perform following String operations with and without pointers to arrays (without using the library functions) 1. Substring 2. Palindrome 3. Compare 4. Copy 5. Reverse 2.2 Pre Lab • Concept of Linked-List • Concepts Of structures 2.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) 6 2.4. THEORY String operations – 900 MB free hard disk space • Software Requirement – Turbo c 2.4 Theory In computing, a C string is a character sequence stored as a one-dimensional character array and terminated with a null character Basic String Handling Functions:All the string handling functions are prototyped in : #include <string.h> 2.5 Procedure I.Pseudo code for string length: //String Length// Lab Manual - Programming Laboratory 7 VPCOE, Baramati 2.5. PROCEDURE String operations void length(char a[]) { int i=0; while(a[i]!=’\0’) { i++; // increment Val of i, up to null character. } Print i. } // end of function. II.Pseudo code for string reverse: //String Reverse// void reverse(char a[]) { int i=0; while(a[i]!=’\0’) { i++; } i--; for(j=i to 0 ) { printf("%c",a[j]); } } // end of function. III.Pseudo code for string copy: //String Copy// void copy(char a[],char b[]) { int i=0; while(b[i]!=’\0’) { a[i]=b[i]; // copy b array to a array i++; } a[i]=’\0’; // add null character after copying b array to a array. Lab Manual - Programming Laboratory 8 VPCOE, Baramati 2.5. PROCEDURE String operations for(i=0;a[i]!=’\0’;i++) { printf("%c",a[i]); } } //end of function IV. Pseudo code for string concatenate: //String Concatenate// void concat(char a[],char b[]) { int i=0,j=0; while(a[i]!=’\0’) { c[i]=a[i]; i++; } while(b[j]!=’\0’) { c[i+j]=b[j]; j++; } c[i+j]=’\0’; for(i=0;c[i]!=’\0’;i++) { printf("%c",c[i]); } } // end of function. V. Pseudo code for string palindrome: //Palindrome// void palim(char a[]) { int i=0,j=0,f=0; while(a[j]!=’\0’) { j++; } j--; Lab Manual - Programming Laboratory 9 VPCOE, Baramati 2.5. PROCEDURE String operations while(a[i]!=’\0’) { if(a[i]!=a[j]) { f=1; } i++; j--; } if(f==0) { printf("\n String is palindrome"); } Else { printf("\n String is not palindrome"); } } } // end of function. VI.Pseudo code for Substring : void sub(char a[]) { printf("\n Enter starting number"); scanf("%d",&n1); printf("\n Enter Ending number"); scanf("%d",&n2); for (i=n1;i<=n2;i++) { Print a[i] } } // end of function. VII.Pseudo code for String compare: //Comparision// void comp(char a[],char b[]) Lab Manual - Programming Laboratory 10 VPCOE, Baramati 2.5. PROCEDURE String operations { int i=0,j=0,f=0; while (a[i]!=’\0’ || b[j]!=’\0’) { if(a[i]!=b[j]) { f=1; // if any character is not same, set flag to 1. break; } i++; j++; } if (f==0) { print Strings are same. } Else print Strings are not same. } // end of function. VIII.Pseudo code for String length using pointer: Let input is given as a string, p is a pointer as, Set p=&a[0]; //String Length// void length(char *p) { int i=0; while(*p!=’\0’) { p++; i++; } print Val of i. } // end of function. IX.Pseudo code for String reverse using pointer: Let input is given as a string, p is a pointer as, Set p=&a[0]; Lab Manual - Programming Laboratory 11 VPCOE, Baramati 2.5. PROCEDURE String operations //String Reverse// void reverse(char *p) { int i=0,j; while(*p!=’\0’) { p++; i++; } p--; i--; for(j=i;j>=0;j--) { printf("%c",*p); p--; } } // end of function. X. Pseudo code for String concatenate using pointer: Let input is given as a string & b string, p & q are pointers as, Set p=&a[0] , q=&b[0]; //String Concatenate// void concat(char *p,char *q) { char c[100],*r; r=&c[0]; while(*p!=’\0’) { *r=*p; r++; p++; } while(*q!=’\0’) { *r=*q; r++; Lab Manual - Programming Laboratory 12 VPCOE, Baramati 2.5. PROCEDURE String operations q++; } *r=’\0’; printf("%s",c); } XI. Pseudo code for String palindrome using pointer: Let input is given as a string, p is a pointer as, Set p=&a[0]; //Palindrome// void palim(char *p) { int f=0; char *r; *r=*p; while(*r!=’\0’) { r++; // move pointer r up to last character, & set p to first character. } r--; while(*p!=’\0’) { if(*p!=*r) { f=1; } p++; r--; } if(f==0) { printf("\n String is palimdrome"); } Else Lab Manual - Programming Laboratory 13 VPCOE, Baramati 2.5. PROCEDURE String operations { printf("\n String is not palimdrome"); } } // end of function. XII.Pseudo code for finding sbstring using pointer: Let input is given as a string, p is a pointer as, Set p=&a[0]; //SUBSTRING// void sub(char *a) { int n1,n2,i; printf("\n Enter starting no. of string:"); scanf("%d",&n1); printf("\n Enter ending no.of string:"); scanf("%d",&n2); a=&a[n1]; for(i=n1;i<=n2;i++) { printf("%c",*p); p++; } } // end of function. XIII.Pseudo code for String comparison using pointer: Let input is given as a string & b string, p & q are pointers as, Set p=&a[0] , q=&b[0]; //Comparison// void comp(char *p,char *q) { int i=0,j=0,f=0; while (*p!=’\0’ || *q!=’\0’) { if(*p!=*q) { f=1; break; Lab Manual - Programming Laboratory 14 VPCOE, Baramati 2.6. POST LAB String operations } p++; q++; } if (f==0) { printf("\n String are same"); } Else printf("\n String are not same"); } 2.6 // end of function. Post Lab String operations like length, palindrome of string, comparison,copy ,reverse, substring etc. 2.7 Viva Questions 1. What is a mean by palindrome of a string ? 2. Define string ? 3. Where string comparison operation can be used ? Lab Manual - Programming Laboratory 15 VPCOE, Baramati Assignment 3 Structure manipulation 3.1 Problem Statement Create a database using array of structures and perform following operations on it: 1. Create database 2. Display database 3. Add record 4. Search record 5. Modify a record 6. delete a record 3.2 Pre Lab • Concepts Of structures • Concept of Array 3.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 16 3.4. THEORY Structure manipulation – 512 MB RAM (1 GB+ recommended) – 900 MB free hard disk space • Software Requirement – Ubuntu 10.04 3.4 Theory 3.4.1 Introduction to C structure In some programming contexts, you need to access multiple data types under a single name for easy manipulation; for example you want to refer to address with multiple data like house number, street, zip code, country C supports structure which allows you to wrap one or more variables with different data types. A structure can contain any valid data types like int, char, float even arrays and other structures. Each variable in structure is called a structure member. Defining structure:- To define a structure, you can use struct keyword. Here is the common syntax of structure definition: struct struct_name { structure_member }; The name of structure is followed the rule of variable name. Here is an example of defining address structure: struct address { unsigned int house_number; char street_name[50]; int zip_code; char country[50]; }; It contains house number as an positive integer, street name as a string, zip code as an integer and country as a string. Declaring structure:The above example only defines an address structure without create any instance of it. To create or Lab Manual - Programming Laboratory 17 VPCOE, Baramati 3.4. THEORY Structure manipulation declare a structure you have two ways: The first way is declare a structure follows by structure definition like this : struct struct_name { structure_member; ... } instance_1,instance_2 instance_n; In the second way you can declare your structure instance at a different location in your source code after structure definition. Here is structure declaration syntax : struct struct_name instance_1 instance_2 instance_n; Complex structure:Structure can contains arrays or other structures so it is sometimes called complex structure. For example address structure is a complex structure. We can define a complex structure which contains address structure as follows: struct customer { char name[50]; structure address billing_addr; structure address shipping_addr; }; accessing structure member:- To access structure members we can use dot operator (.) between structure name and structure member name as follows: structure name. structure member For example to access street name of structure address we do as follows: struct address billing_addr; billing_addr.country = "US"; If the structure contains another structure, we can use dot operator to access nested structure and use dot operator again to access variables of nested structure. struct customer jack; jack.billing_addr.country = "US"; Initializing structure:- C programming language treats a structure as a custom data type therefore you can initialize a structure like a variable. Here is an example of initialize product structure: Lab Manual - Programming Laboratory 18 VPCOE, Baramati 3.4. THEORY Structure manipulation struct product { char name[50]; double price; } book = { "C programming language",40.5}; In above example, we define product structure, then we declare and initialize book structure with its name and price. Structure and pointer:- A structure can contain pointers as structure members and we can create a pointer to a structure as follows: struct invoice { char* code; char date[20]; }; struct address billing_addr; struct address *pa = &billing_addr; Shorthand structure with typedef keyword:- To make your source code more concise, you can use typedef keyword to create a synonym for a structure. This is an example of using typedef keyword to define address structure so when you want to create an instance of it you can omit the keyword struct typedef struct { unsigned int house_number; char street_name[50]; int zip_code; char country[50]; } address; address billing_addr; address shipping_addr; In assignment there is Structure manipulation (for any database like Employee or Bank database) with and without pointers to structures. Here we declare structure as, Lab Manual - Programming Laboratory 19 VPCOE, Baramati 3.5. PROCEDURE Structure manipulation struct Employee { int id; char name[100]; int basic; }emp[100]; 3.5 Procedure I. Pseudo code for create: void create() { int i=0; Do { printf("\nEnter Employee id,name,basic:"); scanf("%d%s%d",&emp[i].id,emp[i].name,&emp[i].basic); printf("\n Add more record"); ans=getche(); i++; count++; }while(ans==’y’); } // end of function. II.Pseudo code for display: void display() { for(i=0 to count ) { printf("\nEmployee Id:%d\t\tEmployee Name:%s\t\tEmployee Basic:%d",emp[i].id,emp[i].nam } } // end of function. III.Pseudo code for search: Lab Manual - Programming Laboratory 20 VPCOE, Baramati 3.5. PROCEDURE Structure manipulation void search() { printf("\n Enter the id u want to search"); scanf("%d",&key); for(i=0 to count ) { if(key==emp[i].id) { printf("\n %d Id is present ",key); break; } // end of if. } // end of for. if(i==count) printf("\nId is not present in record"); } // end of function. IV.Pseudo code for modify: Void modify() { printf("\n Enter the id u modify"); scanf("%d",&key); for(i=0 to count ) { if(key==emp[i].id) { printf("\n Enter the Id to b modified "); scanf("%d",&emp[i].id); printf("\n Enter the name to b modified "); scanf("%s",emp[i].name); printf("\n Enter the basic salary to b modified "); scanf("%d",&emp[i].basic); } } // end of if. // end of for. } // end of function. V.Pseudo code for delete: Lab Manual - Programming Laboratory 21 VPCOE, Baramati 3.5. PROCEDURE Structure manipulation void del() { int f=0,key,i,j=0; printf("\n Enter the id u want delete"); scanf("%d",&key); for(i=0 to count ) { if(key==emp[i].id) { for( j=I to count - 1 ) { emp[j].id=emp[j+1].id; emp[j].basic=emp[j+1].basic; strcpy(emp[j].name,emp[j+1].name); } // end of for. Count--; } // end of if. } // end of outer for. if(i==count) { printf("\n record not present"); } give call to display function. } // end of function. VI. Pseudo code for create with pointer: void create() { char ans; p=&emp[0]; Do Lab Manual - Programming Laboratory 22 VPCOE, Baramati 3.5. PROCEDURE Structure manipulation { printf("\nEnter Employee id,name,basic:"); scanf("%d%s%d",&p->id,p->name,&p->basic); p++; count++; printf("\n Add more record"); ans=getche(); }while(ans==’y’); } // end of function. VII.Pseudo code for display with pointer: void display() { int i; p=&emp[0]; for(i=0;i<count;i++) { printf("\nEmployee Id:%d\t\tEmployee Name:%s\t\tEmployee Basic:%d",p->i p++; } } // end of for. // end of function. VIII.Pseudo code for search with pointer: void search() { int key,i; p=&emp[0]; printf("\n Enter the id u want to search"); scanf("%d",&key); for(i=0;i<count;i++) { if(key==p->id) Lab Manual - Programming Laboratory 23 VPCOE, Baramati 3.6. POST LAB Structure manipulation { printf("\n %d Id is present ",key); break; } // end of if. p++; } // end of for. if(i==count) printf("\nId is not present in record"); } // end of function. IX.Pseudo code for modify with pointer: void modify() { int key,i; p=&emp[0]; printf("\n Enter the id u modify"); scanf("%d",&key); for(i=0;i<count;i++) { if(key==p->id) { printf("\n Enter the Id to b modified "); scanf("%d",&p->id); printf("\n Enter the name to b modified "); scanf("%s",p->name); printf("\n Enter the basic salary to b modified "); scanf("%d",&p->basic); } // end of if. p++; } // end of for. } 3.6 // end of function. Post Lab Creation of database using array of structures, addition of record, deletion of record and modification of record Lab Manual - Programming Laboratory 24 VPCOE, Baramati 3.7. VIVA QUESTIONS 3.7 Structure manipulation Viva Questions 1. What is a structure? 2. How structure size is calculated? 3. what is difference between structure and union? Lab Manual - Programming Laboratory 25 VPCOE, Baramati Assignment 4 Searching and Sorting I 4.1 Problem Statement Accept student information (e.g. Roll No, Name, Percentage etc.) 1. Display the data in descending order of Percentage (Bubble Sort) 2. display the data in descending order of percentage (selection sort) 3. Search for particular string using binary search 4.2 Pre Lab • Concept Array • Concepts Of structures 4.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) – 900 MB free hard disk space • Software Requirement – Ubuntu 10.04 26 4.4. THEORY 4.4 Searching and Sorting I Theory 1. Bubble Sort: The basic idea underlying the bubble sort is to compare the adjacent elements and moves the largest element to the bottom of the list. 2. Selection Sort: A selection sort is one in which we successively select the smallest element, second smallest element, third smallest element, and so on, and place them into their proper sorted positions. 3. Binary Search: In binary searching we compare the item with the element in the center of the list and then restrict our attention to only the first or second half of the list, depending on whether the ’item’ comes before or after the central one. If the ’item’ is in the first half of the list, the procedure is repeated on the first of the list. In this way at each step we reduce the length of the list to be searched by half. And this process continues until either the required ’item’ is found or the search intervals become empty. E.g. target item = 123. 4.5 Procedure I. SelectionSort (int a [], int n) { int i,j,temp; Lab Manual - Programming Laboratory 27 VPCOE, Baramati 4.5. PROCEDURE Searching and Sorting I for (i=0; i<n-1;i++) { for (j=i+1;j<n;j++) { if (a[i]> a[j]) { temp =a[i]; a [i]=a[j]; a [j]= temp; } } } } II.BubbleSort (int a [], int n) { int i,j,t,flag; flag=1; for (i=0; i<n && flag= =1;i++) Lab Manual - Programming Laboratory 28 VPCOE, Baramati 4.5. PROCEDURE Searching and Sorting I { flag=0; for (j=0;j<n-i-1;j++) { if (a[j]> a[j+1]) { flag=1; t=a[j]; a [j]=a[j+1]; a[j+1]= t; } } } } III.Binary Search (int a [], int n, int item) int mid, lower, upper; lower= 0; upper= n-1; while (lower<= upper) Lab Manual - Programming Laboratory 29 VPCOE, Baramati 4.6. POST LAB Searching and Sorting I { mid = (lower+ upper)/2; if (a[mid] = = item) return (mid); else if ( a[mid]> item) upper =mid -1; else lower = mid +1; } return (-1); } 4.6 Post Lab Sorting of database using various sorting techniques. 4.7 Viva Questions 1. What is mean by sorting ? 2. What is time complexity ? 3. Write the complexity of bubble sort and selection sort. Lab Manual - Programming Laboratory 30 VPCOE, Baramati Assignment 5 Matrix Operations 5.1 Problem Statement Implement following Matrix operations: 1. Addition with pointers to arrays 2. Multiplication without pointers to arrays 3. Transpose with pointers to arrays 4. Saddle point without pointers 5.2 Pre Lab • Concept of Linked-List • Concepts Of structures 5.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) – 900 MB free hard disk space 31 5.4. THEORY Matrix Operations • Software Requirement – Ubuntu 10.04 5.4 Theory 5.5 Procedure 5.6 Post Lab Addition, Multiplication and Evaluation of polynomial using circular linked list. 5.7 Viva Questions 1. a Lab Manual - Programming Laboratory 32 VPCOE, Baramati Assignment 6 Quick Sort 6.1 Problem Statement Write a program to implement quick sort on a list of numbers. 6.2 Pre Lab • Concept of Array • Concepts Of quick sorting technique 6.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) – 900 MB free hard disk space • Software Requirement – Ubuntu 10.04 33 6.4. THEORY 6.4 Quick Sort Theory Quicksort sorts by employing a divide and conquer strategy to divide a list into two sub-lists. The steps are: 1. Pick an element, called a pivot, from the list. 2. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. 3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements. 6.5 Procedure I.void quicksort(int list[],int m,int n) int key,i,j,k; if( m < n) { k = choose_pivot(m,n); swap(&list[m],&list[k]); key = list[m]; i = m+1; j = n; while(i <= j) { while((i <= n) && (list[i] <= key)) i++; while((j >= m) && (list[j] > key)) j--; if( i < j) swap(&list[i],&list[j]); } // swap two elements swap(&list[m],&list[j]); // recursively sort the lesser list quicksort(list,m,j-1); quicksort(list,j+1,n); } Lab Manual - Programming Laboratory 34 VPCOE, Baramati 6.6. POST LAB Quick Sort } II.void printlist(int list[],int n) int i; for(i=0;i<n;i++) printf("%d\t",list[i]); } 6.6 Post Lab Sorting of numbers in ascending or descending order 6.7 Viva Questions 1. What is a time complexity of quick sort. 2. What is a recursive function. Lab Manual - Programming Laboratory 35 VPCOE, Baramati Assignment 7 Sparse Matrix 7.1 Problem Statement Represent sparse matrix using two dimensional array or structure and perform simple and fast transpose. 7.2 Pre Lab • Concept of two dimensional arrays • Concepts Of Structures 7.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) – 900 MB free hard disk space • Software Requirement – Turbo c 36 7.4. THEORY Sparse Matrix 7.4 Theory 7.5 Procedure I.Simple Transpose Algorithm: Procedure TRANSPOSE (A,B) // A is a matrix represented in sparse form // B is set to be its transpose 1 (m,n,t) (A(0,1),A(0,2),A(0,3)) 2 (B(0,1),B(0,2),B(0,3)) 3 if t 4 q (n,m,t) 0 then return 1 5 for col 6 for p 1 to n do 1 to t do 7 if A(p,2) = col 8 then [(B(q,1),B(q,2),B(q,3)) 9 (A(p,2),A(p,1),A(p,3)) 10 q q+1] 11 end 12 end 13 end of TRANSPOSE Time complexity of the algorithm is O(nt). This time complexity becomes O(n2m) when t is of the order nm. This is worse than the O(nm) time using arrays. II.Fast Transpose Algorithm: Procedure FAST-TRANSPOSE (A,B) //A is an array representing a sparse matrix m n with t nonzero elements. The transpose is stored in B using only O(t+n) operations // declare S(1:n),T(1:n); 1 (m,n,t) (A(0,1),A(0,2), A(0,3)) 2 (B(0,1),B(0,2), B(0,3)) 3 if t (n,m,t) 0 then return 4 for i 1 to n do S(i) 5 for i 1 to t do 6 S(A(i,2)) 0 end S(A(i,2)) +1 7 end 8 T(1) 1 Lab Manual - Programming Laboratory 37 VPCOE, Baramati 7.6. POST LAB 9 for i 1 to n do 10 T(i) T(i-1) +S(i-1) Sparse Matrix 11 end 12 for i 13 j 1 to t do A(i,2) 14 (B(T(j),1), B(T(j),2) ,B(T(j),3)) 15 (A(i,2), A(i,1), A(i,3)) 16 T(j) T(j) + 1 17 end 18 end FAST_TRANSPOSE Time complexity of fast transpose algorithm is O(n+t). This computing time becomes O(nm) when t is of the order of nm. 7.6 Post Lab Simple and fast transpose operations. 7.7 Viva Questions 1. How fast transpose is calculated ? 2. What is a sparse matrix . Lab Manual - Programming Laboratory 38 VPCOE, Baramati Assignment 8 Singly Linked List 8.1 Problem Statement Problem Statement: Create a singly linked list with options: a. insert (at front, at end, in the middle), b. delete (at front, at end, in the middle), c. Display, d. Display Reverse 8.2 Pre Lab • Concept of Linked-List • Concepts Of structures 8.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) – 900 MB free hard disk space • Software Requirement – Ubuntu 10.04 39 8.4. THEORY 8.4 Singly Linked List Theory Declare a structure to represent a node in the SLL typedef struct link int data; struct link next; node; Create the head node head = (node) malloc (sizeof(node)); head→data=-99; head→next=NULL; Declare the menu Read the choice & switch according to that Pass the head node as a parameter to each function call Figure 8.1: SLL 8.5 Procedure I.Pseudo code for insert a node: void insert(node *head) { node *t,*temp; t=(node*)malloc(sizeof(node)); printf("\nEnter the new node data:"); scanf("%d",&t->data); t->next=NULL; temp=head; while(temp->next!=NULL) { temp=temp->next; Lab Manual - Programming Laboratory 40 VPCOE, Baramati 8.5. PROCEDURE Singly Linked List } temp->next=t; } // end of function. II.Pseudo code for display : void display(node *head) { node *temp; temp=head->next; while(temp!=NULL) { printf("\t%d",temp->data); temp=temp->next; } } // end of function. III.Pseudo code for delete a node: void del(node *head) { node *temp,*t; printf("\nEnter the element which is to be delete"); scanf("%d",&c); temp=head; while(temp->next!=NULL) { if(temp->next->data==c) { t=temp->next; temp->next=temp->next->next; free(t); printf("\nData is deleted"); } Else { temp=temp->next; } Lab Manual - Programming Laboratory 41 VPCOE, Baramati 8.5. PROCEDURE Singly Linked List } display(head); } // end of function. IV.Pseudo code for reverse: void reverse(node *head) { node *p,*q,*r; p=head->next; q=NULL; while(p!=NULL) { r=q; q=p; p=p->next; q->next=r; } head->next= q; } // end of function. V.Pseudo code for revert: void revert(node *head) { node *p,*q,*r; p=head->next; q=NULL; while(p!=NULL) { r=q; q=p; p=p->next; q->next=r; } head->next=q; display(head); Lab Manual - Programming Laboratory 42 VPCOE, Baramati 8.6. POST LAB Singly Linked List } // end of function. 8.6 Post Lab Operations on single link list: addition, deletion, reverse and display 8.7 Viva Questions 1. What is the difference between SLL and DLL? 2. What are the applications of SLL. Lab Manual - Programming Laboratory 43 VPCOE, Baramati Assignment 9 Implementation of DLL 9.1 Problem Statement Implement DLL and perform following operations: • Insert • Delete • Modify • Forward • Backward 9.2 Pre Lab • Concept of Linked-List • Concepts Of structures 9.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) 44 9.4. THEORY Implementation of DLL – 900 MB free hard disk space • Software Requirement – Ubuntu 10.04 9.4 Theory In computer science, a doubly-linked list is a linked data structure that consists of a set of data records, each having two special link fields that contain references to the previous and to the next record in the sequence. It can be viewed as two singly-linked lists formed from the same data items, in two opposite orders. Figure 9.1: DLL The two links allow walking along the list in either direction with equal ease. Compared to a singlylinked list, modifying a doubly-linked list usually requires changing more pointers, but is sometimes simpler because there is no need to keep track of the address of the previous node. In computer science, a doubly-linked list is a linked data structure that consists of a set of data records, each having two special link fields that contain references to the previous and to the next record in the sequence. It can be viewed as two singly-linked lists formed from the same data items, in two opposite orders. Figure 9.2: DLL1 The two links allow walking along the list in either direction with equal ease. Compared to a singlylinked list, modifying a doubly-linked list usually requires changing more pointers, but is sometimes simpler because there is no need to keep track of the address of the previous node 9.5 Procedure I.Insert a Node: Lab Manual - Programming Laboratory 45 VPCOE, Baramati 9.6. POST LAB Implementation of DLL function insertAfter(List list, Node node, Node newNode) newNode.prev := node newNode.next := node.next if node.next == null list.lastNode := newNode Else node.next.prev := newNode node.next := newNode function insertBefore(List list, Node node, Node newNode) newNode.prev := node.prev newNode.next := node if node.prev is null list.firstNode := newNode Else node.prev.next := newNode node.prev := newNode II.Remove Node: function remove(List list, Node node) if node.prev == null list.firstNode := node.next Else node.prev.next := node.next if node.next == null list.lastNode := node.prev Else node.next.prev := node.prev destroy node 9.6 Post Lab Insertion ,deletion, modification and display DLL 9.7 Viva Questions 1. What is a difference between DLL and SLL? Lab Manual - Programming Laboratory 46 VPCOE, Baramati 9.7. VIVA QUESTIONS Implementation of DLL 2. What is a node structure for DLL ? Lab Manual - Programming Laboratory 47 VPCOE, Baramati Assignment 10 Polynomial Using Circular Linked-List 10.1 Problem statement Represent single variable polynomial as a circular linked list. Accept the terms in the polynomial in any order, i.e. not necessarily in the decreasing order of exponent. Sort while creating polynomial in the decreasing order of exponent and write a menu driven program to perform display, addition, multiplication and evaluation. 10.2 Pre Lab • concept of Linked-List • Concepts Of structures 10.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) 900 MB free hard disk space – SVGA or higher-resolution monitor (XGA recommended) • Software Requirement 48 10.4. THEORY Polynomial Using Circular Linked-List – Turbo c 10.4 Theory Circular lists: In some situations, a circular list is needed in which nodes form a ring. Here each node has a successor. An example of such a situation is when several processes are using the same computer resource for the same amount of time, and we have to assure that no process accesses the resource before all other processes did. A process is may be a program in execution. The processes are put on a circular list, and control passes to next one when any of the nodes (processes) is serviced. A circular list is accessible through one of its nodes, usually called the tail. Note that unlike standard linked lists, there is no node with NULL pointer in a circularly linked list. Figure 10.1: Circular list Structure used for polynomial representation using CLL is, typedef struct node { int coeff; int exp; struct node *next; }node; Algorithm: Steps for creating polynomial: 1. create new node head give its link to itself. i.e. head -¿next=head. (first node on list) 2. Enter coefficient and exponent for new term .insert this term in a list by using insert function. 3. Repeate step 2 till you want to add terms in polynomial. 4. Stop. Lab Manual - Programming Laboratory 49 VPCOE, Baramati 10.4. THEORY Polynomial Using Circular Linked-List Steps for inserting node in the list: 1. while inserting new term on polynomial check all the nodes previously present on list. If exponent of new term is matching with exponent of any node(temp) on list then just add the coefficient of new term in the coefficient of temp.and don’t create new node. 2. If coefficient of new term is not matching with any node present on list then Then create new node with new coeff and expo . 3. Add new node at the end of list. 4. return starting ponter of list to create function. 5. stop. Steps for addition of two polynomials: 1. head1 is starting pointer for polynomial 1 and head2 is starting pointer for polynomial 2. 2. Create a node head3 as a starting node of third polynomial. 3. consider first term from first polynomials (term1 ) an first term from second polynomial. (term2) If exponents are equal then add coeff and keep expo same and add this insert this node in list 3 (third Poly) by using insert function. If expo of term1 is greater then insert term1 in list3 else copy term 2 in List3. 4. Repeate this for all the terms from poly1 and poly2 . 5. stop. Steps for multiplication of two polynomials: Initially I=0 , j=0 1. Consider ith term from list1 multiply coeff of that term with jth term in list 2 add coeff of these two terms .Now with this coeff and exponent add new term in list three by using insert function. 2. consider next term in list2 (j+1) 3. Repeate step1 and 2 for all terms in list 2. 4. consider next term in list 1.( i+1) 5. Repeat step 2 3 and 4 for all terms in list 1. Steps for evaluation of a polynomial: J=0 ,ans=0; Lab Manual - Programming Laboratory 50 VPCOE, Baramati 10.5. POST LAB Polynomial Using Circular Linked-List 1. Enter value of x; 2. Consider Jth term of list1 and multiply its coefficient with power of list1 consider its coeff is cf and exponent is ex. 3. Now multiply cf with xex add this answer in previous ans. Increment j 4. Repeat step 2 and 3 for all the terms in list1. 5. return final answer to main function 6. Stop. 10.5 Post Lab Addition, Multiplication and Evaluation of polynomial using circular linked list. 10.6 Viva Questions 1. What is Circular Linked-List? 2. What is a Difference between CLL, DLL and SLL? 3. What are the advantages of CLL over SLL? Lab Manual - Programming Laboratory 51 VPCOE, Baramati References [1] B Forouzan, ”Data Structures:A pseudo code approach with C”, Cenage Learning,. [2] Yashwant Kanetkar, ”Let us C” and ”Pointers in C”, BPB Publication. 52