Sie sind auf Seite 1von 20

# //C Program to find multiplication of two matrices #include<stdio.

h> # define size 10 void main() { int r1,c1,r2,c2,i,j,k,x[size][size],y[size][size]; int m[size][size]; clrscr(); printf("Enter the order of 1st matrice\n"); scanf("%d%d",&r1,&c1); printf("Enter the order of 2nd matrice\n"); scanf("%d%d",&r2,&c2); if(c1==r2) { printf("enter the %d elements of first matrix\n",r1*c1); for (i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&x[i][j]); } printf("enter the %d elements of second matrix\n",r2*c2 ); for (i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&y[i][j]); } printf("The given first matrix is:\n"); for (i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%d\t",x[i][j]); printf ("\n"); } printf("The given second matrix is:\n"); for (i=0;i<r2;i++) { for(j=0;j<c2;j++) printf("%d\t",y[i][j]); printf ("\n"); } for (i=0;i<r1;i++) { for(j=0;j<c2;j++) { m[i][j]=0; for(k=0;k<r2;k++) m[i][j]=m[i][j]+x[i][k]*y[k][j]; } } printf("The product is:\n"); for (i=0;i<r1;i++) { for (j=0;j<c2;j++) printf(" %d\t",m[i][j]); printf("\n");

## //C PROGRAM TO REVERSE AN INPUT STRING USING STACKS

#include<stdio.h> #include<string.h> #define STACK_SIZE 20 void push(char item,int *top,char s[]) { if (*top==STACK_SIZE-1) { printf("\n stack overflow\n"); return; } s[++(*top)]=item; } char pop(int *top,char s[]) { char item_deleted; if (*top==-1) { return 0; } item_deleted=s[(*top)--]; return item_deleted; } int is_rev(char str[]) { int i; int top=-1; char s[30] ; char stk_item=0; for(i=0;i<strlen(str);i++) { push (str[i],&top,s); } printf("\n The reversed string is:"); for(i=0;i<strlen(str);i++) { stk_item= pop (&top,s); printf("%c",stk_item); } getch(); } void main() { char str[20]; clrscr(); printf("\n Enter the string to be reversed\n"); scanf("%s",str); is_rev(str); }

//C Program to multiply two sparse matrices #include<stdio.h> #include<conio.h> #include<alloc.h> #define MAX1 3 #define MAX2 3 #define MAXSIZE 20 #define TRUE 1 #define FALSE 2 struct sparse { int *sp ; int row ; int *result ; }; void initsparse ( struct sparse * ) ; void create_array ( struct sparse * ) ; int count ( struct sparse ) ; void display ( struct sparse ) ; void create_tuple ( struct sparse*, struct sparse ) ; void display_tuple ( struct sparse ) ; void prodmat ( struct sparse *, struct sparse, struct sparse ) ; void searchina ( int *sp, int ii, int*p, int*flag ) ; Void searchinb ( int *sp, int jj, int colofa, int*p, int*flag ) ; void display_result ( struct sparse ) ; void delsparse ( struct sparse * ) ; void main( ) { struct sparse s[5] ; int i ; clrscr( ) ; for ( i = 0 ; i<= 3 ; i++ ) initsparse ( &s[i] ) ; create_array ( &s[0] ) ; create_tuple ( &s[1], s[0] ) ; display_tuple ( s[1] ) ; create_array ( &s[2] ) ; create_tuple ( &s[3], s[2] ) ; display_tuple ( s[3] ) ; prodmat ( &s[4], s[1], s[3] ) ; printf ( "\nResult of multiplication of two matrices: " ) ; display_result ( s[4] ) ; for ( i = 0 ; i<= 3 ; i++ ) delsparse ( &s[i] ) ; getch( ) ; } /* initialises elements of structure */ void initsparse ( struct sparse *p ) { p -> sp = NULL ; p -> result = NULL ; } /* dynamically creates the matrix */ void create_array ( struct sparse *p ) { int n, i ; /* allocate memory */ p -> sp = ( int * ) malloc ( MAX1 * MAX2 * Sizeof ( int ) ) ; /* add elements to the array */ for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) {

printf ( "Enter element no. %d: ", i ) ; scanf ( "%d", &n ) ; * ( p -> sp + i ) = n ; } } /* displays the contents of the matrix */ void display ( struct sparse s ) { int i ; /* traverses the entire matrix */ for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { /* positions the cursor to the new line or every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.sp + i ) ) ; } } /* counts the number of non-zero elements */ int count ( struct sparse s ) { int cnt = 0, i ; for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { if ( * ( s.sp + i ) != 0 ) cnt++ ; } return cnt ; } /* creates an array that stores Information about non-zero elements */ void create_tuple ( struct sparse *p, Struct sparse s ) { int r = 0 , c = -1, l = -1, i ; /* get the total number of non-zero Elements */ p -> row = count ( s ) + 1 ; /* allocate memory */ p -> sp = ( int * ) malloc ( p -> row * 3 * sizeof ( int ) ) ; /* store information about total no. of rows, cols, and non-zero values */ * ( p -> sp + 0 ) = MAX1 ; * ( p -> sp + 1 ) = MAX2 ; * ( p -> sp + 2 ) = p -> row - 1 ; l=2; /* scan the array and store info. about Non-zero values in the 3-tuple */ for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { c++ ; /* sets the row and column values */ if ( ( ( i % 3 ) == 0 ) && ( i != 0 ) ) { r++ ; c=0; } /* checks for non-zero element, row, Column and non-zero value is assigned to the matrix */ if ( * ( s.sp + i ) != 0 ) { l++ ; * ( p -> sp + l ) = r ; l++ ; * ( p -> sp + l ) = c ; l++ ; * ( p -> sp + l ) = * ( s.sp + i ) ; } } }

/* displays the contents of the matrix */ void display_tuple ( struct sparse s ) { int i, j ; /* traverses the entire matrix */ printf ( "\nElements in a 3-tuple: " ) ; j = ( * ( s.sp + 2 ) * 3 ) + 3 ; for ( i = 0 ; i< j ; i++ ) { /* positions the cursor to the new line or every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.sp + i ) ) ; } printf ( "\n" ) ; } /* performs multiplication of sparse Matrices */ void prodmat ( struct sparse *p, struct sparse a, struct sparse b ) { int sum, k, position, posi, flaga, flagb, i , j ; k=1; p -> result = ( int * ) malloc ( MAXSIZE * 3 * sizeof ( int ) ) ; for ( i = 0 ; i< * ( a.sp + 0 * 3 + 0 ) ; i++ ) { for ( j = 0 ; j< * ( b.sp + 0 * 3 + 1 ) ; j++ ) { /* search if an element present at ith row */ searchina ( a.sp, i, &position, &flaga ) ; if ( flaga == TRUE ) { sum = 0 ; /* run loop till there are element at ith Row in first 3-tuple */ while ( * ( a.sp + position * 3 + 0 ) == i ) { /* search if an element present at ith col. in second 3-tuple */ searchinb ( b.sp, j, * ( a.sp + position * 3 + 1 ), &posi, &flagb ) ; /* if found then multiply */ if ( flagb == TRUE ) sum = sum + * ( a.sp + position * 3 + 2 ) * * ( b.sp + posi * 3 + 2 ) ; position = position + 1 ; } /* add result */ if ( sum != 0 ) { * ( p -> result + k * 3 + 0 ) = i ; * ( p -> result + k * 3 + 1 ) = j ; * ( p -> result + k * 3 + 2 ) = sum ; k=k+1; } } } } /* add total no. of rows, cols and non-zero values */ * ( p -> result + 0 * 3 + 0 ) = * ( a.sp + 0 * 3 + 0 ) ; * ( p -> result + 0 * 3 + 1 ) = * ( b.sp + 0 * 3 + 1 ) ; * ( p -> result + 0 * 3 + 2 ) = k - 1 ; } /* searches if an element present at iith row */ void searchina ( int *sp, int ii, int *p, int *flag ) { int j ; *flag = FALSE ; for ( j = 1 ; j<= * ( sp + 0 * 3 + 2 ) ; j++ ) {

if ( * ( sp + j * 3 + 0 ) == ii ) { *p = j ; *flag = TRUE ; return ; } } } /* searches if an element where col. of first 3-tuple is equal to row of second 3-tuple */ void searchinb ( int *sp, int jj, int colofa, int *p, int *flag ) { int j ; *flag = FALSE ; for ( j = 1 ; j<= * ( sp + 0 * 3 + 2 ) ; j++ ) { if ( * ( sp + j * 3 + 1 ) == jj && * ( sp + j * 3 + 0 ) == colofa ) { *p = j ; *flag = TRUE ; return ; } } } /* displays the contents of the matrix */ void display_result ( struct sparse s ) { int i ; /* traverses the entire matrix */ for ( i = 0 ; i< ( * ( s.result + 0 + 2 ) + 1 ) * 3 ; i++ ) { /* positions the cursor to the new line for every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.result + i ) ) ; } } /* deallocates memory */ void delsparse ( struct sparse *s ) { if ( s -> sp != NULL ) free ( s -> sp ) ; if ( s -> result != NULL ) free ( s -> result ) ; }

#include <stdio.h> typedef struct pnode { float coef; int exp; struct pnode *next; }p; p *getnode(); void main() { p *p1,*p2,*p3; p *getpoly(),*add(p*,p*); void display(p*); clrscr(); printf(\n enter first polynomial; ) p1=getpoly(); printf(\n enter second polynomial); p2=getpoly(); printf(\nthe first polynomial is); display(p1); printf(\nthe second polynomial is); display(p2); p3=add(p1,p2); printf(\naddition of two polynomial is :\n); display(p3); } p *getpoly() { p *temp,*New,*last; int flag,exp; char ans; float coef; temp=NULL; flag=1; printf(\nenter the polynomial in descending order of exponent); do { printf(\nenter the coef & exponent of a term); scanf(%f%d,&coef,&exp); New=getnode(); if(New==NULL) printf(\nmemory cannot be allocated); New->coef=coef; New->exp=exp; if(flag==1) { temp=New; last=temp; flag=0; } else { last->next=New; last=New; } printf(\ndou want to more terms);

ans=getch(); } while(ans==y'); return(temp); } p *getnode() { p *temp; temp=(p*) malloc (sizeof(p)); temp->next=NULL; return(temp); } void display(p*head) { p*temp; temp=head; if(temp==NULL) printf(\npolynomial empty); while(temp->next!=NULL) { printf(%0.1fx^%d+,temp->coef,temp->exp); temp=temp->next; } printf(\n%0.1fx^%d,temp->coef,temp->exp); getch(); } p*add(p*first,p*second) { p *p1,*p2,*temp,*dummy; char ch; float coef; p *append(int,float,p*); p1=first; p2=second; temp=(p*)malloc(sizeof(p)); if(temp==NULL) printf(\nmemory cannot be allocated); dummy=temp; while(p1!=NULL&&p2!=NULL) { if(p1->exp==p2->exp) { coef=p1->coef+p2->coef; temp=append(p1->exp,coef,temp); p1=p1->next; p2=p2->next; } else if(p1->expexp) { coef=p2->coef; temp=append(p2->exp,coef,temp); p2=p2->next; } else if(p1->exp>p2->exp)

{ coef=p1->coef; temp=append(p1->exp,coef,temp); p1=p1->next; } } while(p1!=NULL) { temp=append(p1->exp,p1->coef,temp); p1=p1->next; } while(p2!=NULL) { temp=append(p2->exp,p2->coef,temp); p2=p2->next; } temp->next=NULL; temp=dummy->next; free(dummy); return(temp); } p*append(int Exp,float Coef,p*temp) { p*New,*dum; New=(p*)malloc(sizeof(p)); if(New==NULL) printf(\ncannot be allocated); New->exp=Exp; New->coef=Coef; New->next=NULL; dum=temp; dum->next=New; dum=New; return(dum); }

//C PROGRAM TO IMPLEMENT STACK USING LINKED LIST #include<stdio.h> struct stack {

int info; struct stack *next; }; typedef struct stack node; class stlink { node *start; public: stlink() { start=NULL; } void display(void); void push(int); int pop(void); }; void stlink::push(int term) { node *p,*s; s=start; if(s==NULL||s!=NULL) { p=(node *)malloc(sizeof(node)); p->info=term; p->next=s; s=p; } start=s; return; } void stlink::display(void) { node *temp; if(start==NULL) { cout << endl<<"UNDERFLOEW"; } temp=start; while(temp!=NULL) { cout << endltemp=temp->next; } return; } int stlink::pop(void) { int term; if(start==NULL) { cout<<"UNDERFLOW"; return(-1); } else { node *p; term=start->info; p=start; free(start); start=p->next; return(term); } } int main() {

stlink s1; int ch,temp; do { clrscr(); cout<<"1->Push\n"; cout<<"2->Display\n"; cout<<"3->Pop\n"; cout<<"4->Exit\n"; cout<<"Enter your choice:"; cin>>ch; switch(ch) { case'1': cout<<"Enter the term to push:"; cin>>temp; s1.push(temp); break; case'2': cout << endl<<"Stack"; s1.display(); getch(); break; case'3': temp=s1.pop(); if(temp!=-1) cout<<"Popped term is " << temp; getch(); break; case'4': cout<<"Exiting"; getch(); break; default: cout<<"Invalid choice"; getch(); break; } }while(ch!=4); return(0); }

## #include < stdio.h> #include < conio.h>

#include < malloc.h> #include < process.h> #include < ctype.h> struct linear_queue { int info; struct linear_queue *next; }*front,*rear,*newnode,*ptr; void menu(); void display(); int underflow(); void enqueue(int); void dequeue(); void main() { clrscr(); menu(); } void menu() { int choice,item; printf("MENU"); printf("\n1. Insert into the queue"); printf("\n2. Delete from queue"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice) { case 1: clrscr(); printf("\nEnter the item tobe inserted: "); scanf("%d",&item); enqueue(item); clrscr(); printf("\nAfter inserting queue is:\n"); display(); getch(); clrscr(); menu(); break; case 2: clrscr(); if(underflow()==1) { dequeue(); if(underflow()==1) { printf("\nAfter deletion queue is:\n"); display(); } } getch(); clrscr(); menu();

break; case 3: clrscr(); if(underflow()==1) { printf("The queue is:\n"); display(); } getch(); clrscr(); menu(); break; case 4: exit(1); default: clrscr(); printf("Your choice is wrong\n\n"); menu(); } } int underflow() { if((front==NULL)&&(rear==NULL)) { printf("\nQueue is empty"); return(0); } else { return(1); } } void enqueue(int item) { newnode=(struct linear_queue*)malloc(sizeof(struct linear_queue)); newnode->info=item; if((front==NULL)&&(rear==NULL)) { front=newnode; rear=newnode; newnode->next=NULL; } else { rear->next=newnode; newnode->next=NULL; rear=newnode; } } void dequeue() { if(front==rear) { front=NULL; rear=NULL; } else { front=front->next;

} } void display() { int i; ptr=front; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; } }

## //C PROGRAM TO IMPLEMENT BUBBLE SORT PROGRAM USING ARRAYS

#include<stdio.h> #include<conio.h>

void bubble(int a[],int n) { int i,j,t; for(i=n-2;i>=0;i--) { for(j=0;j<=i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } }//end for 1. }//end function. void main() { int a[100],n,i; clrscr(); printf("\n\n Enter integer value for total no.s of elements to be sorted: "); scanf("%d",&n); for( i=0;i<=n-1;i++) { printf("\n\n Enter integer value for element no.%d : ",i+1); scanf("%d",&a[i]); } bubble(a,n); printf("\n\n Finally sorted array is: "); for( i=0;i<=n-1;i++) printf("%3d",a[i]); } //end program

## #include<stdio.h> #define MAX 20 void mergesort(int *,int);

void main() { int x[MAX],n,j,i; char ans; clrscr(); { printf("\nEnter The Length Of The Array\t: "); scanf("%d",&n); for(i=0;i< n;i++) { printf("Enter Element %d\t: ",i+1); scanf("%d",&x[i]); } mergesort(x,n); printf("\n\t Sorted Array :\t\t\t\n\t"); for(i=0;i< n;i++) printf("%d\t",x[i]); } printf(""); getch(); } void mergesort(int x[],int n) { int sub[MAX]; int i,j,k,list1,list2,u1,u2,size=1; while(size< n) { list1=0; k=0; while((list1+size)< n) { list2=list1+size; u1=list2-1; u2=((list2+size-1)< n)?(list2+size-1):(n-1); for(i=list1,j=list2;i< =u1 &&amp;amp;amp; j< =u2;k++) if(x[i]< =x[j]) sub[k]=x[i++]; else sub[k]=x[j++]; for(;i< =u1;k++) sub[k]=x[i++]; for(;j< =u2;k++) sub[k]=x[j++]; list1=u2+1; } for(i=list1;k< n;i++) sub[k++] = x[i]; for(i=0;i< n;i++) x[i] =sub[i]; size *= 2; } }

## //C PROGRAM TO FIND MINIMAL SPANNING TREE USING PRIM'S ALGORITHM

#include<stdio.h> #define INF 1000 int vertex[10]; int wght[10][10]; int new_wght[10][10]; int closed[10];

int n; int inclose(int i,int n1) { /*chk for the ith vertex presence in closed*/ int j; for(j=0;j<=n1;j++) if(closed[j]==i) return 1; return 0; } void buildtree() { int i=0,j,count=0; int min,k,v1=0,v2=0; closed[0]=0; while(count<n-1) { min=INF; for(i=0;i<=count;i++) for(j=0;j<n;j++) if(wght[closed[i]][j]<min && !inclose(j,count)) { min=wght[closed[i]][j]; v1=closed[i]; v2=j; } new_wght[v1][v2]=new_wght[v2][v1]=min; count++; closed[count]=v2; printf("\nScan : %d %d---------%d wght = %d \n",count,v1+1,v2+1,min); getch(); } } void main() { int i,j,ed,sum=0; clrscr(); printf("\n\n\tPRIM'S ALGORITHM TO FIND SPANNING TREE\n\n"); printf("\n\tEnter the No. of Nodes : "); scanf("%d",&n); for(i=0;i<n;i++) { vertex[i]=i+1; for(j=0;j<n;j++) { wght[i][j]=INF; new_wght[i][j]=INF; } } printf("\n\nGetting Weight.\n"); printf("\n\tEnter 0 if path doesn't exist between {v1,v2} else enter the wght\n"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { printf("\n\t%d -------- %d : ",vertex[i],vertex[j]); scanf("%d",&ed); if(ed>=1) wght[i][j]=wght[j][i]=ed; } getch(); clrscr(); printf("\n\n\t\tNODES CURRENTLY ADDED TO SPANNING TREE\n\n"); buildtree(); printf("\n\tNEW GRAPH WEIGHT MATRIX\n\n"); printf("\n\tweight matrix\n\n\t"); for(i=0;i<n;i++,printf("\n\t")) for(j=0;j<n;j++,printf("\t"))

printf("%d",new_wght[i][j]); printf("\n\n\t\tMINIMUM SPANNING TREE\n\n"); printf("\n\t\tLIST OF EDGES\n\n"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(new_wght[i][j]!=INF) { printf("\n\t\t%d ------ %d = %d ",vertex[i],vertex[j],new_wght[i][j]); sum+=new_wght[i][j]; } printf("\n\n\t Total Weight : %d ",sum); getch(); }