Q1. Write a program to search a number from a given list using linear and binary search. #include<stdio.h> #inlcude<conio.h> void main() { int arr[10]; int i,j; int n int snumber; int count=0; printf("Enter the number of element in array"); scanf("%d",&n); printf("\nEnter the element in array"); for(i=0;i<n; i++) { scanf("%d",&arr[i]); } printf("\nenter the search element"); scanf("%d",&snumber); for(i=0;i<n;i++) { if(arr[i]==snumber) { count=1; printf("value is found"); break; } } if(count==0) printf("value is not found"); getch(); } 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Q1. Write a program to search a number from a given list using linear and binary search.
printf("Enter the number of element in array");scanf("%d",&n);printf("\nEnter the element in array");for(i=0;i<n; i++){scanf("%d",&arr[i]);}printf("\nenter the search element");scanf("%d",&snumber);for(i=0;i<n;i++){
if(arr[i]==snumber){
count=1;printf("value is found");break;
}}if(count==0)printf("value is not found");getch();}
1
Output:-Enter the number of element in array 5
Enter the element in array 37895
enter the search element 9value is found
Enter the number of element in array 5
Enter the element in array37894
enter the search element 10value is not found
2
Q2. Write a program to search a number from a given list using binary search recursively.
int high,low=0,i,j,value,pos,n;clrscr();printf("Enter n number of item");scanf("%d",&n);high=n;printf("Enter the value in sorted form\n");for(i=0;i<n;i++){
scanf("%d",&b[i]);}printf("Enter the value which you have to find");scanf("%d",&value);pos=binary(low,high,value);//printf("\n in location %d th",pos);getch();
Enter n number of item5Enter the value in sorted form1 2 3 4 5Enter the value which you have to find3value is found
4
Q3. Write the program to find the factorial of number using both recursive and non-recursive method and also compare that performance.
#include<stdio.h>#include<conio.h>void main(){int number;int i,fact=1;clrscr();printf("enter the number");scanf("%d",&number);for(i=number;i>1;i--){ fact=fact*i;}printf("\nfactorial number of %d=%d",number,fact);getch();}
clrscr();printf("enter the number");scanf("%d",&number);factorial=fact(number);printf("\nfactorial number of %d=%d",number,factorial);getch();}int fact(int number)
Q4. Sort the following list in increasing order of number 9,94,45,47,28,98,65,42,8,4,88,6 using selection sort.
#include<stdio.h>#include<conio.h>void main(){int sort[15];int i,j;int n;int temp;clrscr();printf("enter the number of element in array");scanf("%d",&n);printf("\nenter the value in array");for(i=0;i<n;i++)scanf("%d",&sort[i]);
int na,nb,nc,k,c[30];na=lb;nb=rb;nc=lb;while((na<=le)&&(nb<=re)){
if(a[na]<a[nb])c[nc++]=a[na++];
elsec[nc++]=a[nb++];
9
}if(na>le){
while(nb<=re)c[nc++]=a[nb++];
}else{
while(na<=le)c[nc++]=a[na++];
}for(k=lb;k<=re;k++)a[k]=c[k];
}
Output
enter the num5Enter the array54123
Sorted list is:12345
10
Q6. Write a program to sort a list using quick sort by applying divide and conquer method .
#include "stdio.h"
int split ( int*, int, int ) ;void quicksort ( int *, int, int ) ;void main( ){
int arr[10];int i ;int n;printf("enter the number of element in array");scanf("%d",&n);printf("Enter the value of array");for(i=0;i<n;i++)scanf("%d",&arr[i]);
quicksort ( arr, 0, n ) ;
printf ( "\nArray after sorting:\n") ;
for ( i = 0 ; i < n ; i++ )printf ( "%d\t", arr[i] ) ;
getch();}
void quicksort ( int a[ ], int lower, int upper ){
int i ;if ( upper > lower ){
i = split ( a, lower, upper ) ;quicksort ( a, lower, i - 1 ) ;quicksort ( a, i + 1, upper ) ;
}}
int split ( int a[ ], int lower, int upper ){
int i, p, q, t ;
p = lower + 1 ;q = upper ;
11
i = a[lower] ;
while ( q >= p ){
while ( a[p] < i )p++ ;
while ( a[q] > i )q-- ;
if ( q > p ){
t = a[p] ;a[p] = a[q] ;a[q] = t ;
}}
t = a[lower] ;a[lower] = a[q] ;a[q] = t ;
return q ;}
OUTPUTenter the number of element in array 5Enter the value of array 1 4 3 5 2
Array after sorting:1 2 3 4 5
12
Q7. Write a program to perform product of two matrix of order nxn using strassen’s multiplication method.
#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(){int a[4][4],b[4][4],c[4][4],d[7],e[7],f[7],g[7],i,j,choice;do{printf("\n1. 4*4 matrix.");printf("\n2. 2*2 matrix.");printf("\n3. Exit.");printf("\nEnter your choice : ");scanf("%d",&choice);switch(choice){case 1:{printf("\nEnter value for matrix A : ");for(i=0;i<4;i++)for(j=0;j<4;j++)scanf("%d",&a[i][j]);printf("\nEnter value for matrix B : ");for(i=0;i<4;i++)for(j=0;j<4;j++)scanf("%d",&b[i][j]);d[0]=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);d[1]=(a[1][0]+a[1][1])*b[0][0];d[2]=a[0][0]*(b[0][1]-b[1][1]);d[3]=a[1][1]*(b[1][0]-b[0][0]);d[4]=(a[0][0]+a[0][1])*b[1][1];d[5]=(a[1][0]-a[0][0])*(b[0][0]+b[0][1]);d[6]=(a[0][1]-a[1][1])*(b[1][0]-b[1][1]);e[0]=(a[0][2]+a[1][3])*(b[0][2]+b[1][3]);e[1]=(a[1][2]+a[1][3])*b[0][2];e[2]=a[0][2]*(b[0][3]-b[1][3]);e[3]=a[1][3]*(b[1][2]-b[0][2]);e[4]=(a[0][2]+a[0][3])*b[1][3];e[5]=(a[1][2]-a[0][2])*(b[0][2]+b[0][3]);e[6]=(a[0][3]-a[1][3])*(b[1][2]-b[1][3]);f[0]=(a[2][0]+a[3][1])*(b[2][0]+b[3][1]);f[1]=(a[3][0]+a[3][1])*b[2][0];f[2]=a[2][0]*(b[2][1]-b[3][1]);f[3]=a[3][1]*(b[3][0]-b[2][0]);f[4]=(a[2][0]+a[2][1])*b[3][1];f[5]=(a[3][0]-a[2][0])*(b[2][0]+b[2][1]);
13
f[6]=(a[2][1]-a[3][1])*(b[3][0]-b[3][1]);g[0]=(a[2][2]+a[3][3])*(b[2][2]+b[3][3]);g[1]=(a[3][2]+a[3][3])*b[2][2];g[2]=a[2][2]*(b[2][3]-b[3][3]);g[3]=a[3][3]*(b[3][2]-b[2][2]);g[4]=(a[2][2]+a[2][3])*b[3][3];g[5]=(a[3][2]-a[2][2])*(b[2][2]+b[2][3]);g[6]=(a[2][3]-a[3][3])*(b[3][2]-b[3][3]);c[0][0]=d[0]+d[3]-d[4]+d[6];c[0][1]=d[2]+d[4];c[1][0]=d[1]+d[3];c[1][1]=d[0]+d[2]-d[1]+d[5];c[0][2]=e[0]+e[3]-e[4]+e[6];c[0][3]=e[2]+e[4];c[1][2]=e[1]+e[3];c[1][3]=e[0]+e[2]-e[1]+e[5];c[2][0]=f[0]+f[3]-f[4]+f[6];c[2][1]=f[2]+f[4];c[3][0]=f[1]+f[3];c[3][1]=f[0]+f[2]-f[1]+f[5];c[2][2]=g[0]+g[3]-g[4]+g[6];c[2][3]=g[2]+g[4];c[3][2]=g[1]+g[3];c[3][3]=g[0]+g[2]-g[1]+g[5];printf("\nMatrix multiplication is : \n");for(i=0;i<4;i++){for(j=0;j<4;j++){printf("%d",c[i][j]);}printf("\n");}break;}case 2:{printf("\nEnter value for matrix A : ");for(i=0;i<2;i++)for(j=0;j<2;j++)scanf("%d",&a[i][j]);printf("\nEnter value for matrix B : ");for(i=0;i<2;i++)for(j=0;j<2;j++)scanf("%d",&b[i][j]);d[0]=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);d[1]=(a[1][0]+a[1][1])*b[0][0];d[2]=a[0][0]*(b[0][1]-b[1][1]);d[3]=a[1][1]*(b[1][0]-b[0][0]);d[4]=(a[0][0]+a[0][1])*b[1][1];
14
d[5]=(a[1][0]-a[0][0])*(b[0][0]+b[0][1]);d[6]=(a[0][1]-a[1][1])*(b[1][0]-b[1][1]);c[0][0]=d[0]+d[3]-d[4]+d[6];c[0][1]=d[2]+d[4];c[1][0]=d[1]+d[3];c[1][1]=d[0]+d[2]-d[1]+d[5];printf("\nMatrix multiplication is : \n");for(i=0;i<2;i++){for(j=0;j<2;j++){printf("%d",c[i][j]);}printf("\n");}break;}case 3:{exit(0);}default:{printf("\nInvalid chocie.");printf("\nSelect value from 1-4...");}}}while(choice!=3);getch();}
OUTPUT
15
Q8. Write a program to find minimum spanning tree using Kruskal’s method.
void main(){int i,j,k=0,temp;int sum=0;clrscr();printf("\n\tEnter the No. of Nodes : ");scanf("%d",&n);for(i=0;i<n;i++){printf("\n\tEnter %d value : ",i+1);fflush(stdin);scanf("%c",&vertex[i]);for(j=0;j<n;j++){wght[i][j]=INF;span_wght[i][j]=INF;}}printf("\n\nGetting Weight\n");for(i=0;i<n;i++)for(j=i+1;j<n;j++){printf("\nEnter 0 if path Doesn't exist between %c to %c : ",vertex[i],vertex[j]);scanf("%d",&ed);if(ed>=1){wght[i][j]=wght[j][i]=ed;que[r].v1=i;que[r].v2=j;que[r].weight=wght[i][j];if(r){for(k=0;k<r;k++)if(que[k].weight>que[r].weight){swap(&que[k].weight,&que[r].weight);swap(&que[k].v1,&que[r].v1);swap(&que[k].v2,&que[r].v2);}}r++;}}build_tree();printf("\n\t\tLIST OF EDGES\n\n");for(i=0;i<n;i++)for(j=i+1;j<n;j++)if(span_wght[i][j]!=INF){printf("\n\t\t%c ------ %c = %d ",vertex[i],vertex[j],span_wght[i][j]);sum+=span_wght[i][j];
Q13. Write a program to place the queen on a square chessboard using Backtracking approach.
# include <stdio.h># include <stdlib.h># include <time.h>int N; //For N * N ChessBoardint flag;void printArray(int a[]); /* Just to Print the Final Solution */void getPositions(int a[],int n1,int n2); /* The Recursive Function */int main(){int *a; int ctr=0; printf("\nTHE N QUEENS PROBLEM "); printf("\nNumber Of Rows(N) For NxN Chessboard."); scanf("%d",&N); a=(int *)(malloc(sizeof(int)*N)); printf("\nAll possible Solutions .. \n"); printf("\nIn Each of the solutions the Coordinates of the N-Queens are given (Row,Col) ."); printf("\nNote that the Rows and Colums are numbered between 1 - N :\n"); for(ctr=0;ctr<N;ctr++) getPositions(a,0,ctr); getchar(); getchar();}void printArray(int a[]){ int i,choice; static int counter=0; counter++; printf("\nSOLUTION # %d :",counter); for(i=0;i<N;i++) printf("(%d,%d) ",i+1,a[i]+1); if(counter%10==0) { printf("\nEnter 0 to exit , 1 to continue .");
scanf("%d",&choice); if(choice==0) exit(0); };
}void getPositions(int a1[],int colno,int val){ int ctr1,ctr2; a1[colno]=val; if(colno==N-1)
{ printArray(a1) ; return; };
29
for(ctr1=0;ctr1<N;) { /* This Loop Finds Suitable Column Numbers , in the NEXT ROW */ for(ctr2=0;ctr2<=colno;ctr2++) if(a1[ctr2]==ctr1 || (colno+1-ctr2)*(colno+1-ctr2)==(ctr1-a1[ctr2])*(ctr1-a1[ctr2])) goto miss1; getPositions(a1,colno+1,ctr1); miss1: ctr1++; }}
30
OUTPUT
31
Q14. Write the program to solve Graph Coloring problem using Backtracking approach.
void main(){int i,t,j,h,n,m,k,maxclique=0;int edge[20][20];int colour[20];printf("\n Enter the no. of vertices in the graph : ");scanf("%d",&n);for(i=0;i<n;i++)colour[i]=0;for(i=0;i<n;i++){for(j=0;j<n;j++)edge[i][j]=0;}for(i=0;i<n;i++){t=0;printf("\n Enter number of adjacent nodes from node %d : ",i);scanf("%d",&t);if(t>maxclique)maxclique=t;printf("\n Enter those adjacent nodes : ");