Vous êtes sur la page 1sur 30

http://vmp4cpp.blogspot.in/ http://www.c4cpp.co.

nr/ Program 1
a) For a given array of integers search a given number using linear search #include<iostream.h> #include<conio.h> void main() { int a_search[50],i,n,item,loc,c=0; clrscr(); cout<<"enter the number of elements in the array"; cin>>n; cout<<"\n"<<"enter the elements of the array"; for(i=0;i<n;i++) { cin>>a_search[i]; } cout<<"\n"<<"enter the element you want to search"; cin>>item; for(i=0;i<n;i++) { if(a_search[i]==item) { c=1; loc=i; } } if(c==1) { cout<<"\n"<<"item is at location "<<loc+1; } else { cout<<"\n"<<"item not in the list"; } getch(); }

OUTPUT enter the number of elements in the array5 enter the elements of the array89 67 90 45 62 enter the element you want to search67 item is at location 2

b) For a given array of integers sort the numbers #include <iostream.h> #include <conio.h> void main( ) { int arr[100] ; int i, j, temp,num ; clrscr(); cout<<"how many number you want to enter: "; cin>>num; cout<<"enter the numbers: "; for(i=0;i<num;i++) cin>>arr[i]; for ( i = 0 ; i <= num ; i++ ) { for ( j = 0 ; j <=i ; j++ ) { if ( arr[j] > arr[j + 1] ) { temp = arr[j] ; arr[j] = arr[j + 1] ; arr[j + 1] = temp ; } } } cout<< "\n\nArray after sorting:\n" ; for ( i = 0 ; i <= num ; i++ ) cout<< arr[i] <<"\t" ; getch(); }

OUTPUT how many number you want to enter: 5 enter the numbers: 2 31 21 25 17 Array after sorting: 2 17 21 25 31

c) For a given array of integers search a given number using binary search #include<iostream.h> #include<conio.h> void main() { int a_search[50],i,n,item,loc,high,low=0,mid; clrscr(); cout<<"enter the number of elements in the array: "; cin>>n; cout<<"\n"<<"enter the elements in the array: "; for(i=0;i<n;i++) { cin>>a_search[i]; } cout<<"\n"<<"enter the item you want to search: "; cin>>item; high=n; mid=((low+high)/2); while(low<=high&&a_search[mid]!=item) { if(item<a_search[mid]) { high=mid-1; } else { low=mid+1; } mid=((low+high)/2); } if(a_search[mid]==item) { cout<<"\n"<<"item is present at location: "<<mid+1; } else { cout<<"\n"<<"item not in the list"; } getch(); }

OUTPUT enter the number of elements in the array: 5 enter the elements in the array: 34 76 95 39 56 enter the item you want to search: 76 item is present at location: 2

d) For a given array of integers find the maximum and minimum number in the array #include<iostream.h> #include<conio.h> void main() { int sort[100],max,r,temp,num,min; clrscr(); cout<<"how many numbers you want to enter(do not enter more then 100): "; cin>>num; cout<<"enter th elements: "; for(r=0;r<num;r++) cin>>sort[r]; max=sort[0]; min=sort[0]; for(r=0;r<num;r++) { if(max<sort[r]) { max=sort[r]; } } for(r=0;r<num;r++) { if(min>sort[r]) { min=sort[r]; } } cout<<"\n"<<"largest element is "<<max; cout<<"\n"<<"smallest element is "<<min; getch(); }

OUTPUT how many numbers you want to enter(do not enter more then 100): 5 enter th elements: 2 6 4 8 2 largest element is 8 smallest element is 2

e) For a given array of integers remove the repeated elements #include<iostream.h> #include<conio.h> void main() { int n, first[10], second[10], i = 0, q, y; clrscr(); cout<<"Enter the number of elements in array: "; cin>>n; cout<<"Enter the elements in array: "; for(q=0;q<n;q++) cin>>first[q]; for(q=0;q<n;q++) { for(y=0;y<i;y++) { if(first[q]==second[y]) break; } if(y==i) { second[i] = first[q]; i++; } } cout<<"Array obtained after removing duplicate elements: "<<"\n"; for(q=0;q<i;q++) cout<<second[q]; getch(); }

OUTPUT Enter the number of elements in array: 7 Enter the elements in array: 1 1 2 2 4 4 6 Array obtained after removing duplicate elements: 1246

Program 2
a) For a two-dimensional array of integers find the maximum and minimum in each row, each column and in the matrix #include<iostream.h> #include<conio.h> void main() { int array[3][3],i,j; clrscr(); cout<<"enter the elements of matrix (size 3 x 3)\n"; for(i=0; i<3; i++) { for (j=0; j<3; j++) cin>>array[i][j]; } int min=array[0][0], max=array[0][0]; for(i=0;i<3;i++) { int minr=array[i][0]; int maxr=0; for(j=0;j<3;j++) { if(array[i][j]>max) max=array[i][j]; if (array[i][j]<min) min=array[i][j]; if(array[i][j]>maxr) maxr=array[i][j]; if(array[i][j]<minr) minr=array[i][j]; } cout<<"min and max of row "<<i+1<<"\t"<< minr<<"\t"<<maxr<<"\n"; } for(j=0; j<3;j++) { int minc=array[0][j]; int maxc=0; for(i=0;i<3;i++)

{ if(array[i][j]>maxc) maxc=array[i][j]; if(array[i][j]<minc) minc=array[i][j]; } cout<<"min and max of column"<<j+1<<"\t"<<minc<<"\t"<<maxc<<"\n"; } cout<<"max and min in matrix is:"<<max<<"\t"<<min<<"\n\n"; getch(); } OUTPUT enter the elements of matrix (size 3 x 3) 123 234 234 min and max of row 1 1 3 min and max of row 2 2 4 min and max of row 3 2 4 min and max of column1 1 2 min and max of column2 2 3 min and max of column3 3 4 max and min in matrix is:4 1

b) ) For a two-dimensional array of integers find the transpose #include<iostream.h> #include<conio.h> void main() { int m1[100][100],m2[100][100],i,j,r,c,max; clrscr(); cout<<"enter the dimensions:row and column: "; cin>>r>>c; cout<<"enter the elements of matrix: "; for(i=1;i<=r;i++) { cout<<"\n"; for(j=1;j<=c;j++) cin>>m1[i][j]; } cout<<"the transpose of the matrix is: "; for(i=1;i<=c;i++) { cout<<"\n"; for(j=1;j<=r;j++) cout<<m1[j][i]<<"\t"; } getch(); }

OUTPUT enter the dimensions:row and column: 2 2 enter the elements of matrix: 13 24 the transpose of the matrix is: 1 2 3 4

Program 3
a) For given two matrices of integers find the sum of the matrices #include<iostream.h> #include<conio.h> void main() { int m1[100][100],m2[100][100],m[100][100],r1,r2,c1,c2,i,j; clrscr(); cout<<"enter the number of rows and column for 1st matrix: "; cin>>r1; cin>>c1; cout<<"enter the number of rows and column for 2nd matrix: "; cin>>r2; cin>>c2; if(r1!=r2 && c1!=c2) cout<<"addition is not possible"; else { cout<<"enter elements of 1st matrix:\n"; for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) cin>>m1[i][j]; } cout<<"enter elements of 2nd matrix:\n"; for(i=1;i<=r2;i++) { for(j=1;j<=c2;j++) cin>>m2[i][j]; } cout<<"addition of matrices "; for(i=1;i<=r1;i++) { cout<<"\n"; for(j=1;j<=c1;j++) { m[i][j]=m1[i][j]+m2[i][j]; cout<<m[i][j]<<"\t"; } } }

getch(); } OUTPUT enter the number of rows and column for 1st matrix: 2 2 enter the number of rows and column for 2nd matrix: 2 2 enter elements of 1st matrix: 12 12 enter elements of 2nd matrix: 34 34 addition of matrices 4 6 4 6

b) ) For given two matrices of integers find the difference of the matrices #include<iostream.h> #include<conio.h> void main() { int m1[100][100],m2[100][100],m[100][100],r1,r2,c1,c2,i,j; clrscr(); cout<<"enter the number of rows and column for 1st matrix: "; cin>>r1; cin>>c1; cout<<"enter the number of rows and column for 2nd matrix: "; cin>>r2; cin>>c2; if(r1!=r2 && c1!=c2) cout<<"subtraction is not possible"; else { cout<<"enter elements of 1st matrix:\n"; for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) cin>>m1[i][j]; } cout<<"enter elements of 2nd matrix:\n"; for(i=1;i<=r2;i++) { for(j=1;j<=c2;j++) cin>>m2[i][j]; } cout<<"subtraction of matrices "; for(i=1;i<=r1;i++) { cout<<"\n"; for(j=1;j<=c1;j++) { m[i][j]=m1[i][j]-m2[i][j]; cout<<m[i][j]<<"\t"; } } } getch();

} OUTPUT enter the number of rows and column for 1st matrix: 2 2 enter the number of rows and column for 2nd matrix: 2 2 enter elements of 1st matrix: 34 56 enter elements of 2nd matrix: 23 13 subtraction of matrices 1 1 4 3

c) For given two matrices of integers find the product of the matrices #include<conio.h> #include<iostream.h> void main() { int m1[100][100],m2[100][100],pro[100][100],r1,c1,r2,c2,i,j,k; clrscr(); cout<<"Enter the order of matrix1:Row and Column: "<<"\n"; cin>>r1; cin>>c1; cout<<"Enter the order of matrix2:Row and Column: "<<"\n"; cin>>r2; cin>>c2; if(c1!=r2) { cout<<"Multipliction is not POSSIBLE"<<"\n"; } else { cout<<"Enter the elements of first Matrix: "<<"\n"; for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { cin>>m1[i][j]; } } cout<<"Enter the elements of second Matrix: "<<"\n"; for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { cin>>m2[i][j]; } } for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { pro[i][j]=0; for(k=0;k<r2;k++)

pro[i][j]=pro[i][j]+(m1[i][k]*m2[k][j]); } } cout<<"The product is "<<"\n"; for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { cout<<pro[i][j]<<"\t"; } cout<<"\n"; } } getch(); }

OUTPUT Enter the order of matrix1:Row and Column: 22 Enter the order of matrix2:Row and Column: 22 Enter the elements of first Matrix: 12 34 Enter the elements of second Matrix: 12 11 The product is 3 4 7 10

Program 4
a) For a given string S find the length #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { char name[50],name1[50]; int len; clrscr(); cout<<"enter the string: "; gets(name); len=strlen(name); cout<<"\n"<<"the length of first string is: "<<len; getch(); } OUTPUT enter the string: rameshkumar singh the length of first string is: 17

b) For a given string S find the reverse of the string #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { char name[50],name1[50]; clrscr(); cout<<"enter the string: "; gets(name); strrev(name); cout<<"\n"<<"the reverse of first string is: "<<name; getch(); } OUTPUT enter the string: ramesh kumar the reverse of first string is: ramuk hsemar

c)

Compare the string S with another string S1

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { char name[50],name1[50]; int len,cmp_rst; clrscr(); cout<<"enter first string: "; gets(name); cout<<"enter second string: "; gets(name1); cmp_rst=strcmp(name,name1); if(cmp_rst==0) cout<<"\n"<<"both strings are same" ; else cout<<"\n"<<"strings are different"; getch(); } OUTPUT enter first string: ram enter second string: ramesh strings are different

d) Append another string S1 to the string S #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { char name[50],name1[50]; clrscr(); cout<<"enter first string: "; gets(name); cout<<"enter second string: "; gets(name1); strcat(name,name1); cout<<"the concatenated string is "<<name; getch(); } OUTPUT enter first string: yog enter second string: kunwar the concatenated string is yogkunwar

e) Find a given sub-string S1 in the string S #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { int i=0,j=0,k=0,l=0,k1=0; char a[80],b[80]; clrscr(); cout<<"\nEnter main string:\n"; gets(a); cout<<"\nEnter sub-string:\n"; gets(b); l=strlen(b); while (a[i]!=EOF) { if (a[i]==b[j]) { i++; j++; k1=1; if (j==l) { j=0; k=1; } } else { if (k1==1) { j=0; k1=0; } else i++; }

} if (k==1) { cout<<"\n\nThe given sub-string is present in the main string"; } else { if (k==0) cout<<"\n\nThe given sub-string is not present in the main string"; } getch(); }

OUTPUT Enter main string: yog kunwar Enter sub-string: kun The given sub-string is present in the main string

Program 5
Sort the given array of strings #include<stdio.h> #include<iostream.h> #include<string.h> #include<conio.h> void main() { int i,j,n; clrscr(); char str[20][20],temp[20]; cout<<"Enter the no. of string to be sorted: "; cin>>n; for(i=0;i<n;i++) gets(str[i]); for(i=0;i<n;i++) for(j=i+1;j<n;j++){ if(strcmp(str[i],str[j])>0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } cout<<"The sorted strings are: \n"; for(i=0;i<n;i++) puts(str[i]); getch(); } OUTPUT Enter the no. of string to be sorted: 4 ram hir ramesh narada The sorted strings are: hir narada

ram ramesh

Vous aimerez peut-être aussi