35
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";

c++ file main

Embed Size (px)

Citation preview

Page 1: c++ file main

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();}

Page 2: c++ file main

OUTPUT

enter the number of elements in the array5

enter the elements of the array8967904562

enter the element you want to search67

item is at location 2

Page 3: c++ file main

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();}

Page 4: c++ file main

OUTPUT

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

Page 5: c++ file main

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();

}

Page 6: c++ file main

OUTPUT

enter the number of elements in the array: 5

enter the elements in the array: 3476953956

enter the item you want to search: 76

item is present at location: 2

Page 7: c++ file main

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();}

Page 8: c++ file main

OUTPUT

how many numbers you want to enter(do not enter more then 100): 5enter th elements: 26482

largest element is 8smallest element is 2

Page 9: c++ file main

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();}

Page 10: c++ file main

OUTPUT

Enter the number of elements in array: 7Enter the elements in array: 1122446Array obtained after removing duplicate elements:1246

Page 11: c++ file main

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++)

Page 12: c++ file main

{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)1 2 32 3 42 3 4min and max of row 1 1 3min and max of row 2 2 4min and max of row 3 2 4min and max of column1 1 2min and max of column2 2 3min and max of column3 3 4max and min in matrix is:4 1

Page 13: c++ file main

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();}

Page 14: c++ file main

OUTPUT

enter the dimensions:row and column: 2 2enter the elements of matrix:1 3

2 4the transpose of the matrix is:1 23 4

Page 15: c++ file main

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"; }

}

}

Page 16: c++ file main

getch();}OUTPUT

enter the number of rows and column for 1st matrix: 2 2enter the number of rows and column for 2nd matrix: 2 2enter elements of 1st matrix:1 21 2enter elements of 2nd matrix:3 43 4addition of matrices4 64 6

Page 17: c++ file main

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();

Page 18: c++ file main

}

OUTPUT

enter the number of rows and column for 1st matrix: 2 2enter the number of rows and column for 2nd matrix: 2 2enter elements of 1st matrix:3 45 6enter elements of 2nd matrix:2 31 3subtraction of matrices1 14 3

Page 19: c++ file main

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++)

Page 20: c++ file main

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();}

Page 21: c++ file main

OUTPUT

Enter the order of matrix1:Row and Column:2 2Enter the order of matrix2:Row and Column:2 2Enter the elements of first Matrix:1 23 4Enter the elements of second Matrix:1 21 1The product is3 47 10

Page 22: c++ file main

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

Page 23: c++ file main

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

Page 24: c++ file main

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: ramenter second string: ramesh

strings are different

Page 25: c++ file main

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: yogenter second string: kunwarthe concatenated string is yogkunwar

Page 26: c++ file main

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++; }

Page 27: c++ file main

}

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();}

Page 28: c++ file main

OUTPUT

Enter main string:yog kunwar

Enter sub-string:kun

The given sub-string is present in the main string

Page 29: c++ file main

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: 4ramhirrameshnaradaThe sorted strings are:hirnarada

Page 30: c++ file main

ramramesh