You are on page 1of 43

ROLL NO.

:2309240
PROGRAM NO. -1 : Write a program to search an element in a two-dimensional array using LINEAR SEARCH.
PROGRAM : #include<stdio.h> void main() { int A[10],count=0,i,item; char choice; clrscr(); printf("Enter the Elements of Array=\n"); for(i=0; i<10; i++) { scanf("%d",&A[i]); } do { printf("\nEnter the Item To Be Searched="); scanf("%d",&item); for(i=0; i<10; i++) { if(A[i]==item) { printf("\nItem Found At Location %d",i); count=count+1; } } if(count==0)

Roll No. : 2309240


{ printf("\nItem Not Found"); }; printf("\nWant to continue(press y to yes)"); scanf("%s",&choice); } while(choice=='y'||choice=='Y'); getch(); }

ROLL NO. : 2309240


PROGRAM NO. -2 : Write a program to find an element of an array using BINARY SEARCH method. PROGRAM :
#include<stdio.h> void main() { int A[6],beg=0,mid,end=5,item,i; clrscr(); printf("Enter the sorted array=\n"); for(i=0;i<6;i++) { scanf("%d",&A[i]); } printf("\nEnter the Element to be found="); scanf("%d",&item); while(beg<=end) { mid=(beg+end)/2; if(item==A[mid]) { printf("\nItem Found At Location %d",mid); break; } if(item>A[mid]) { beg=mid+1;

Roll No. : 2309240


} if(item<A[mid]) { end=mid-1; } } if(beg>end) { printf("\nItem Not Found."); } getch(); }

ROLL NO. : 2309240


PROGRAM NO. -3 : Write a program to perform following operations on matrices using functions a)Addition PROGRAM : #include<stdio.h> void add(); void sub(); void mult(); void trans(); void main() { char choice; clrscr(); do { int a; printf("1.Addition of 2 Martices\n2.Subtraction of 2 Matrices\n3.Multiplication of 2 Matrices\n4. Transpose of a matrix\nEnter your choice="); scanf("%d",&a); switch(a) { case 1: { add(); }break; case 2: b)Subtraction c) Multiplication d)Transpose

Roll No. : 2309240


{ sub(); }break; case 3: { mult(); }break; case 4: { trans(); } }; printf("\nWant to continue(press y to yes)"); scanf("%s",&choice); } while(choice=='y'||choice=='Y'); getch(); } void add() { int A[5][5],B[5][5],C[5][5],i,j,r,c; printf("Enter No. of Rows="); scanf("%d",&r); printf("\nEnter No. of Columns="); scanf("%d",&c); printf("\nEnter elements of Matrix A=\n");

Roll No. : 2309240


for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&A[i][j]); }} printf("\nEnter elements of matrix B=\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&B[i][j]); }} for(i=0;i<r;i++) { for(j=0;j<c;j++) { C[i][j]=A[i][j]+B[i][j]; } } printf("\nResulting Matrix is="); for(i=0;i<r;i++) { printf("\n"); for(j=0;j<c;j++) {

Roll No. : 2309240


printf("\t%d",C[i][j]); }}} void sub() { int A[5][5],B[5][5],C[5][5],i,j,r,c; printf("Enter No. of Rows="); scanf("%d",&r); printf("\nEnter No. of Columns="); scanf("%d",&c); printf("\nEnter elements of Matrix A=\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&A[i][j]); }} printf("\nEnter elements of matrix B=\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&B[i][j]); }} for(i=0;i<r;i++) { for(j=0;j<c;j++)

Roll No. : 2309240


{ C[i][j]=A[i][j]-B[i][j]; } } printf("\nResulting Matrix is="); for(i=0;i<r;i++) { printf("\n"); for(j=0;j<c;j++) { printf("\t%d",C[i][j]); }}} void mult() { int A[5][5],B[5][5],C[5][5],D[5][5],i,j,k,r,c; printf("Enter No. of Rows="); scanf("%d",&r); printf("\nEnter No. of Columns="); scanf("%d",&c); printf("\nEnter elements of Matrix A=\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&A[i][j]); }}

Roll No. : 2309240


printf("\nEnter elements of matrix B=\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&B[i][j]); }} for(i=0;i<r;i++) { for(j=0;j<c;j++) { D[i][j]=0; for(k=0;k<r,k<c;k++) { D[i][j]=D[i][j]+((A[i][k])*(B[k][j])); }}} printf("\nResulting Matrix is="); for(i=0;i<r;i++) { printf("\n"); for(j=0;j<c;j++) { printf("\t%d",D[i][j]); }}} void trans() {

Roll No. : 2309240


int A[5][5],B[5][5],i,j,r,c; printf("Enter No. of Rows="); scanf("%d",&r); printf("\nEnter No. of Columns="); scanf("%d",&c); printf("\nEnter Elements Of Matrix=\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&A[i][j]); }} printf("\nThe Matrix Entered Is="); for(i=0;i<r;i++) { printf("\n"); for(j=0;j<c;j++) { printf("\t%d",A[i][j]); }} printf("\nTranspose of Matrix="); for(i=0;i<r;i++) { for(j=0;j<c;j++) { B[i][j]=A[j][i];

Roll No. : 2309240


}} for(i=0;i<r;i++) { printf("\n"); for(j=0;j<c;j++) { printf("\t%d",B[i][j]); }}}

ROLL NO. : 2309240 PROGRAM NO. -4 : Write a program to implement QUEUE.


PROGRAM : #include<stdio.h> void main() { int Q[5],f=0,r=0,n=5,item; char choice; clrscr(); do { int i; printf("1.Addition in a queue\n2.Deletion from a queue\nEnter your choice="); scanf("%d",&i); switch(i) { case 1: { if((f==1&&r==n)||(f==r+1)) { printf("\nOverflow"); break; } if(f==0) { f=1; r=1;

ROLL NO. : 2309240


printf("\nFront=%d\nRear=%d",f,r); } else if(r==n) { r=1; printf("\nFront=%d\nRear=%d",f,r); } else { r=r+1; printf("\nFront=%d\nRear=%d",f,r); } printf("\nEnter the item to be added="); scanf("%d",&item); }break; case 2: { if(f==0) { printf("\nUnderflow"); break; } if(f==r) { f=0; r=0;

ROLL NO. : 2309240


printf("\nFront=%d\nRear=%d",f,r); } else if(f==n) { f=1; printf("\nFront=%d\nRear=%d",f,r); } else { f=f+1; printf("\nFront=%d\nRear=%d",f,r); }}}; printf("\nWant to continue(press y to yes)"); scanf("%s",&choice); } while(choice=='y'||choice=='Y'); getch(); }

ROLL NO. : 2309240 PROGRAM NO. -5 : Write a program to implement STACK.


PROGRAM : #include<stdio.h> void main() { char choice,item,top=0,maxstck=5; int A[5]; clrscr(); do { int i; printf("\nStack\n\t1.Insertion\n\t2.Deletion\nEnter your choice="); scanf("%d",&i); switch(i) { case 1: { if(top==maxstck) { printf("\n\nOverflow"); break; } else { printf("\nEnter the item to be inserted="); scanf("%d",&item);

ROLL NO. : 2309240


item=A[top]; top=top+1; printf("\nTOP=%d",top); }}break; case 2: { if(top<0) { printf("\n\nUnderflow"); break; } else { item=A[top]; top=top-1; printf("\nTop=%d",top); } }}; printf("\nWant to Continue(press y to yes)"); scanf("%s",&choice); } while(choice=='y'||choice=='Y'); getch(); }

ROLL NO. : 2309240 PROGRAM NO. -6 : Write a program to perform following operations on STRINGS :
A) Copying one string to another. D)Concatenation of two strings. B) Comparison of two string. E)Reversing of a string. C)Finding length of a string

PROGRAM :
#include<stdio.h> void main() { int i; char choice; clrscr(); do { printf("1. Copying 1 string to another\n2. Comparison of 2 strings\n3. Finding length of string\n4. Concatenation of 2 strings\n5. Reversing a string\n\nEnter your Choice="); scanf("%d",&i); switch(i) { case 1: { char str1[10],str2[10]; printf("\nEnter 1st string="); scanf("%s",&str1); printf("\nEnter 2nd string="); scanf("%s",&str2); strcpy(str1,str2);

ROLL NO. : 2309240


printf("\n1st string=%s\n2nd string=%s",str1,str2); }break; case 2: { char str1[10],str2[10]; int a; printf("\nEnter 1st string="); scanf("%s",&str1); printf("\nEnter 2nd string="); scanf("%s",&str2); a=strcmp(str1,str2); if(a<0) { printf("\nString 1 is greater"); } else if(a>0) { printf("\nString 2 is greater"); } else { printf("\nBoth strings are equal"); }}break; case 3: {

ROLL NO. : 2309240


char str[10]; int l; printf("\nEnter any string="); scanf("%s",&str); l=strlen(str); printf("\nLength of string=%d",l); }break; case 4: { char str1[10],str2[10]; printf("\nEnter 1st string="); scanf("%s",&str1); printf("\nEnter 2nd string="); scanf("%s",&str2); strcat(str1,str2); printf("\nConcatenated string=%s",str1); }break; case 5: { char str[10]; printf("\nEnter the string to be reversed="); scanf("%s",&str); strrev(str); printf("\nReversed string=%s",str); } };

` printf("\nWant to continue(press y to continue) "); scanf("%s",&choice); }while(choice==y||choice==Y); getch(); }

ROLL NO. : 2309240

ROLL NO. : 2309240 PROGRAM NO. -7 : Write a program for swapping of two numbers using CALL BY
VALUE and CALL BY REFRENCE strategies. PROGRAM :
#include<stdio.h> void main() { int A,B,j; char choice; clrscr(); do { int i; printf("Enter 1st No.,A="); scanf("%d",&A); printf("\nEnter 2nd No.,B="); scanf("%d",&B); printf("\nSwapping through :\n\t\t1: Call by Value\n\t\t2: Call by Reference\nEnter your Choice="); scanf("%d",&i); switch(i) { case 1: { swap(A,B); break; }

ROLL NO. : 2309240


case 2: swap1(&A,&B); break; } }; printf("\nWant to continue(press y to yes)"); scanf("%s",&choice); }while(choice=='y'||choice=='Y'); getch(); } swap(int x,int y) { int c; c=x; x=y; y=c; printf("\nNo.s after swapping :\tA=%d\n\t\t\tB=%d",x,y); } swap1(int *x,int *y) { int c; c=*x; *x=*y; *y=c; printf("\nNo.s after swapping :\tA=%d\n\t\t\tB=%d",*x,*y); }

ROLL NO. : 2309240 PROGRAM NO.-8 : Write a program to create a linked list & perform operations such as INSERT, DELETE, UPDATE, REVERSE in the link list.
PROGRAM :
#include<stdio.h> #include<stdlib.h> void create(); void insert(); void insbeg(); void insend(); void delet(); void delbeg(); void delend(); void update(); void reverse(); void display(); struct node { int info; struct node*link; }*start='\0'; typedef struct node node; node *head,*ptr,*prev,*temp; int item,i,item1; void main() { clrscr();

ROLL NO. : 2309240


do { printf("\n1.Create a linked list\n2.Insertion\n3.Deletion\n4.Update\n5.Reverse\n6.Display\nEnter your choice="); scanf("%d",&i); switch(i) { case 1: create(); break; case 2: insert(); break; case 3: delet(); break; case 4: update(); break; case 5: reverse(); break; case 6: display(); break; }}while(i<7); } void create() { head=(node*)malloc(sizeof(node)); printf("\nEnter the item to be inserted="); scanf("%d",&item); head->info=item;

ROLL NO. : 2309240


head->link='\0'; if(start=='\0') start=head; else ptr->link=head; ptr=head; } void insert() { do { printf("\n1.Insertion at beginning\n2.Insertion at end\nEnter your choice="); scanf("%d",&i); switch(i) { case 1: insbeg(); break; case 2: insend(); break; }}while(i<3); } void delet() { do { printf("\n1.Deletion from beginning\n2.Deletion from end\nEnter your choice=");

ROLL NO. : 2309240


scanf("%d",&i); switch(i) { case 1: delbeg(); break; case 2: delend(); break; }}while(i<3); } void insbeg() { head=(node*)malloc(sizeof(node)); printf("\nEnter the item to be inserted="); scanf("%d",&item); head->info=item; head->link='\0'; if(start=='\0') start=head; else ptr=start; head->link=ptr; start=head; } void insend() { ptr=start;

ROLL NO. : 2309240


while(ptr->link!='\0') { ptr=ptr->link; } head=(node*)malloc(sizeof(node)); printf("\nEnter item to be inserted="); scanf("%d",&item); head->info=item; head->link='\0'; if(start=='\0') start=head; else ptr->link=head; } void delbeg() { if(start=='\0') { printf("\nList is empty"); } else { ptr=start; printf("\nItem deleted is %d",ptr->info); start=start->link; free(ptr);

ROLL NO. : 2309240


} } void delend() { prev=(node*)malloc(sizeof(node)); ptr=start; while(ptr->link!='\0') { prev=ptr; ptr=ptr->link; } printf("\nItem deleted is %d",ptr->info); prev->link='\0'; free(ptr); } void update() { head=(node*)malloc(sizeof(node)); printf("\nEnter item to be replaced="); scanf("%d",&item); temp=start; while(temp->info!=item) { prev=temp; temp=temp->link; }

ROLL NO. : 2309240


prev=prev->link; free(temp); printf("\nEnter the new item="); scanf("%d",&item1); head->info=item1; head->link='\0'; if(prev=='\0') prev=head; else ptr=prev; head->link=ptr; prev=head; } void reverse() { ptr=start; head='\0'; while(ptr!='\0') { prev=head; head=ptr; ptr=ptr->link; head->link=prev; } start=head; }

ROLL NO. : 2309240


void display() { ptr=start; while(ptr!='\0') { printf("%d\t",ptr->info); ptr=ptr->link; } }

ROLL NO. : 2309240 PROGRAM NO.-9 : Write a program to implement stack using Linked List.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void push(); void pop(); void display(); struct stack { int info; struct stack *next; }*top=NULL; typedef struct stack stack; int main() { char choice; do{ int s; printf("\n1.Push into the stack"); printf("\n2.Pop from the stack"); printf("\n3.Display the element of the stack"); printf("\nEnter ur choice::"); scanf("%d",&s); switch(s) { case 1: push();

ROLL NO. : 2309240


break; case 2: pop(); break; case 3: display(); break; default: printf("Invalid Selection"); break; }; printf("\n\nWant 2 continue(press y to yes)"); scanf("%s",&choice); } while(choice=='y' || choice=='Y'); getch(); return 0; } void push() { stack *node; node=(stack *)malloc(sizeof(stack)); printf("\nEnter element ::"); scanf("%d",&node->info); if(top!=NULL) { node->next=top; top=node; }

ROLL NO. : 2309240


else { node->next=NULL; top=node; } } void pop() { stack *temp; int item; temp=top; if(temp==NULL) { printf("\nUnderflow"); return; } item=temp->info; top=top->next; free(temp); printf("Deleted element is:%d",item); } void display() { stack *temp; temp=top; if(temp!=NULL)

ROLL NO. : 2309240


printf("\nElement in the stacks are"); else printf("\nUnderflow"); while(temp!=NULL) { printf("\n%d",temp->info); temp=temp->next; } }

ROLL NO. : 2309240 PROGRAM NO.-10 : Write a program for sorting of elements in a Linked List.
#include<stdio.h> #include<malloc.h>

typedef struct link {int value; struct link *next; }link; link *createnode() { link *temp; temp=(link*)malloc(sizeof(link)); printf("\nenter value for node\n"); scanf("%d",&temp->value); temp->next=NULL; return temp; } link *sort(link*st) { link *nxt,*ptr; int temp; ptr=st; while(ptr!=NULL) { nxt=ptr->next; while(nxt!=NULL)

ROLL NO. : 2309240


{if (ptr->value>nxt->value) {temp=ptr->value; ptr->value=nxt->value; nxt->value=temp; } nxt=nxt->next; } ptr=ptr->next; } return st; } void triverse(link*p) { while(p!=NULL) {printf("\n%d",p->value); p=p->next; } getch(); }

main() { link *start=NULL,*newnode,*ptr; char ch; while(1) {newnode=createnode();

ROLL NO. : 2309240


if(start==NULL) { start=ptr=newnode; } else {ptr->next=newnode; ptr=ptr->next; } printf("\nany new node??"); ch=getche(); if(ch=='y'||ch=='Y') continue; break; } printf("\n---------------------"); printf("\n\n\nentered values are"); triverse(start); sort(start); printf("\nSorted list=2"); triverse(start); getch(); }

Roll No. : 2309240

PROGRAM NO. 11 : Write a program for concatenation of two linked lists where the two linked lists are entered by the user.
#include<stdio.h> #include<malloc.h> typedef struct link {int data; struct link *next; } linkx,linky; linkx *createnode() { linkx *temp; temp=(linkx*)malloc(sizeof(linkx)); printf("\n\nenter value for node\n"); scanf("%d",&temp->data); temp->next=NULL; return temp; } linky *createnode2() { linky *temp; temp=(linky*)malloc(sizeof(linky)); printf("\n\nenter value for node\n"); scanf("%d",&temp->data); temp->next=NULL; return temp;

Roll No. : 2309240


} void triverse(linkx*p) { while(p!=NULL) {printf("\n%d",p->data); p=p->next; } } main() { linkx *start=NULL,*newnode,*ptr; char ch; linky *start2=NULL,*newnode2,*ptr2; char ch2; printf("\nenter values for 1st link list\n"); printf("------------------------------"); while(1) {newnode=createnode(); if(start==NULL) { start=ptr=newnode; } else {ptr->next=newnode; ptr=ptr->next; }

Roll No. : 2309240


printf("\nany new node?? [y/n]"); printf("\t"); ch=getche(); if(ch=='y'||ch=='Y') continue; break; } printf("\n\n\nenter values for 2nd link list"); printf("\n----------------------------"); while(1) {newnode2=createnode2(); if(start2==NULL) { start2=ptr2=newnode2; } else {ptr2->next=newnode2; ptr2=ptr2->next; } printf("\nany new node?? [y/n]"); printf("\t"); ch2=getche(); if(ch2=='y'||ch2=='Y') continue; break; }

Roll No. : 2309240


printf("\n---------------------"); printf("\n\n\nentered values for 1st link list are:"); triverse(start); printf("\n"); printf("\n\n\nentered values for 2nd link list are:"); triverse(start2); ptr=start; while(1) { ptr=ptr->next; if(ptr->next==NULL) break; } ptr->next=start2; printf("\n\nconcatenated link list is:"); triverse(start); getch();}

Roll No. : 2309240

OUTPUT:

You might also like