Vous êtes sur la page 1sur 28

# 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[5 !"i"n"item"loc"c# \$ clrscr()\$ cout<<%enter the number of elements in the array%\$ cin>>n\$ cout<<%&n%<<%enter the elements of the array%\$ for(i# \$i<n\$i'') { cin>>a_search[i!\$ ( cout<<%&n%<<%enter the element you )ant to search%\$ cin>>item\$ for(i# \$i<n\$i'') { if(a_search[i!##item) { c#*\$ loc#i\$ ( ( if(c##*) { cout<<%&n%<<%item is at location %<<loc'*\$ ( else { cout<<%&n%<<%item not in the list%\$ ( getch()\$ (

OUTPUT enter the number of elements in the array5 enter the elements of the array+, -. , /5 -0 enter the element you )ant to search-. item is at location 0

b) For a given array of integers sort the numbers #include <iostream.h> #include <conio.h> void main( ) { int arr[* ! \$ int i" 1" tem2"num \$ clrscr()\$ cout<<%ho) many number you )ant to enter3 %\$ cin>>num\$ cout<<%enter the numbers3 %\$ for(i# \$i<num\$i'') cin>>arr[i!\$ for ( i # \$ i <# num \$ i'' ) { for ( 1 # \$ 1 <#i \$ 1'' ) { if ( arr[1! > arr[1 ' *! ) { tem2 # arr[1! \$ arr[1! # arr[1 ' *! \$ arr[1 ' *! # tem2 \$ ( ( ( cout<< %&n&n4rray after sorting3&n% \$ for ( i # \$ i <# num \$ i'' ) cout<< arr[i! <<%&t% \$ getch()\$ (

OUTPUT ho) many number you )ant to enter3 5 enter the numbers3 0 5* 0* 05 *. 4rray after sorting3 0 *. 0* 05 5*

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[5 !"i"n"item"loc"high"lo)# "mid\$ clrscr()\$ cout<<%enter the number of elements in the array3 %\$ cin>>n\$ cout<<%&n%<<%enter the elements in the array3 %\$ for(i# \$i<n\$i'') { cin>>a_search[i!\$ ( cout<<%&n%<<%enter the item you )ant to search3 %\$ cin>>item\$ high#n\$ mid#((lo)'high)60)\$ )hile(lo)<#high77a_search[mid!8#item) { if(item<a_search[mid!) { high#mid9*\$ ( else { lo)#mid'*\$ ( mid#((lo)'high)60)\$ ( if(a_search[mid!##item) { cout<<%&n%<<%item is 2resent at location3 %<<mid'*\$ ( else { cout<<%&n%<<%item not in the list%\$ ( getch()\$ (

OUTPUT enter the number of elements in the array3 5 enter the elements in the array3 5/ .,5 5, 5enter the item you )ant to search3 .item is 2resent at location3 0

d) For a given array of integers find the ma:imum and minimum number in the array #include<iostream.h> #include<conio.h> void main() { int sort[* !"ma:"r"tem2"num"min\$ clrscr()\$ cout<<%ho) many numbers you )ant to enter(do not enter more then * cin>>num\$ cout<<%enter th elements3 %\$ for(r# \$r<num\$r'') cin>>sort[r!\$ ma:#sort[ !\$ min#sort[ !\$ for(r# \$r<num\$r'') { if(ma:<sort[r!) { ma:#sort[r!\$ ( ( for(r# \$r<num\$r'') { if(min>sort[r!) { min#sort[r!\$ ( ( cout<<%&n%<<%largest element is %<<ma:\$ cout<<%&n%<<%smallest element is %<<min\$ getch()\$ (

)3 %\$

OUTPUT ho) many numbers you )ant to enter(do not enter more then * enter th elements3 0 / + 0 largest element is + smallest element is 0 )3 5

e) For a given array of integers remove the re2eated elements #include<iostream.h> #include<conio.h> void main() { int n" first[* !" second[* !" i # " ;" y\$ clrscr()\$ cout<<%<nter the number of elements in array3 %\$ cin>>n\$ cout<<%<nter the elements in array3 %\$ for(;# \$;<n\$;'') cin>>first[;!\$ for(;# \$;<n\$;'') { for(y# \$y<i\$y'') { if(first[;!##second[y!) brea=\$ ( if(y##i) { second[i! # first[;!\$ i''\$ ( ( cout<<%4rray obtained after removing du2licate elements3 %<<%&n%\$ for(;# \$;<i\$;'') cout<<second[;!\$ getch()\$ (

OUTPUT <nter the number of elements in array3 . <nter the elements in array3 * * 0 0 / / 4rray obtained after removing du2licate elements3 *0/-

Program 2
a) For a t)o9dimensional array of integers find the ma:imum and minimum in each ro)" each column and in the matri:

OUTPUT

b) ) For a t)o9dimensional array of integers find the trans2ose #include<iostream.h> #include<conio.h> void main() { int m*[* ![* !"m0[* ![* !"i"1"r"c"ma:\$ clrscr()\$ cout<<%enter the dimensions3ro) and column3 %\$ cin>>r>>c\$ cout<<%enter the elements of matri:3 %\$ for(i#*\$i<#r\$i'') { cout<<%&n%\$ for(1#*\$1<#c\$1'') cin>>m*[i![1!\$ ( cout<<%the trans2ose of the matri: is3 %\$ for(i#*\$i<#c\$i'') { cout<<%&n%\$ for(1#*\$1<#r\$1'') cout<<m*[1![i!<<%&t%\$ ( getch()\$ (

OUTPUT enter the dimensions3ro) and column3 0 0 enter the elements of matri:3 *5 0/ the trans2ose of the matri: is3 * 0 5 /

Program 3
a) For given t)o matrices of integers find the sum of the matrices #include<iostream.h> #include<conio.h> void main() { int m*[* ![* !"m0[* ![* !"m[* ![* !"r*"r0"c*"c0"i"1\$ clrscr()\$ cout<<%enter the number of ro)s and column for *st matri:3 %\$ cin>>r*\$ cin>>c*\$ cout<<%enter the number of ro)s and column for 0nd matri:3 %\$ cin>>r0\$ cin>>c0\$ if(r*8#r0 77 c*8#c0) cout<<%addition is not 2ossible%\$ else { cout<<%enter elements of *st matri:3&n%\$ for(i#*\$i<#r*\$i'') { for(1#*\$1<#c*\$1'') cin>>m*[i![1!\$ ( cout<<%enter elements of 0nd matri:3&n%\$ for(i#*\$i<#r0\$i'') { for(1#*\$1<#c0\$1'') cin>>m0[i![1!\$ ( cout<<%addition of matrices %\$ for(i#*\$i<#r*\$i'') { cout<<%&n%\$ for(1#*\$1<#c*\$1'') { m[i![1!#m*[i![1!'m0[i![1!\$ cout<<m[i![1!<<%&t%\$ ( ( ( getch()\$ (

OUTPUT enter the number of ro)s and column for *st matri:3 0 0 enter the number of ro)s and column for 0nd matri:3 0 0 enter elements of *st matri:3 *0 *0 enter elements of 0nd matri:3 5/ 5/ addition of matrices / / -

b) ) For given t)o matrices of integers find the difference of the matrices #include<iostream.h> #include<conio.h> void main() { int m*[* ![* !"m0[* ![* !"m[* ![* !"r*"r0"c*"c0"i"1\$ clrscr()\$ cout<<%enter the number of ro)s and column for *st matri:3 %\$ cin>>r*\$ cin>>c*\$ cout<<%enter the number of ro)s and column for 0nd matri:3 %\$ cin>>r0\$ cin>>c0\$ if(r*8#r0 77 c*8#c0) cout<<%subtraction is not 2ossible%\$ else { cout<<%enter elements of *st matri:3&n%\$ for(i#*\$i<#r*\$i'') { for(1#*\$1<#c*\$1'') cin>>m*[i![1!\$ ( cout<<%enter elements of 0nd matri:3&n%\$ for(i#*\$i<#r0\$i'') { for(1#*\$1<#c0\$1'') cin>>m0[i![1!\$ ( cout<<%subtraction of matrices %\$ for(i#*\$i<#r*\$i'') { cout<<%&n%\$ for(1#*\$1<#c*\$1'') { m[i![1!#m*[i![1!9m0[i![1!\$ cout<<m[i![1!<<%&t%\$ ( ( ( getch()\$ (

OUTPUT enter the number of ro)s and column for *st matri:3 0 0 enter the number of ro)s and column for 0nd matri:3 0 0 enter elements of *st matri:3 5/ 5enter elements of 0nd matri:3 05 *5 subtraction of matrices * * / 5

c) ) For given t)o matrices of integers find the 2roduct of the matrices #include<conio.h> #include<iostream.h> void main() { int m*[* ![* !"m0[* ![* !"2ro[* ![* !"r*"c*"r0"c0"i"1"=\$ clrscr()\$ cout<<%<nter the order of matri:*3>o) and ?olumn3 %<<%&n%\$ cin>>r*\$ cin>>c*\$ cout<<%<nter the order of matri:03>o) and ?olumn3 %<<%&n%\$ cin>>r0\$ cin>>c0\$ if(c*8#r0) { cout<<%@ulti2liction is not ABCCDEF<%<<%&n%\$ ( else { cout<<%<nter the elements of first @atri:3 %<<%&n%\$ for(i# \$i<r*\$i'') { for(1# \$1<c*\$1'') { cin>>m*[i![1!\$ ( ( cout<<%<nter the elements of second @atri:3 %<<%&n%\$ for(i# \$i<r0\$i'') { for(1# \$1<c0\$1'') { cin>>m0[i![1!\$ ( ( for(i# \$i<r*\$i'') { for(1# \$1<c0\$1'') { 2ro[i![1!# \$ for(=# \$=<r0\$='') 2ro[i![1!#2ro[i![1!'(m*[i![=!Gm0[=![1!)\$ (

( cout<<%Hhe 2roduct is %<<%&n%\$ for(i# \$i<r*\$i'') { for(1# \$1<c0\$1'') { cout<<2ro[i![1!<<%&t%\$ ( cout<<%&n%\$ ( ( getch()\$ (

OUTPUT <nter the order of matri:*3>o) and ?olumn3 00 <nter the order of matri:03>o) and ?olumn3 00 <nter the elements of first @atri:3 *0 5/ <nter the elements of second @atri:3 *0 ** Hhe 2roduct is 5 / . *

Program 4
a) For a given string C find the length #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { char name[5 !"name*[5 !\$ int len\$ clrscr()\$ cout<<%enter the string3 %\$ gets(name)\$ len#strlen(name)\$ cout<<%&n%<<%the length of first string is3 %<<len\$ getch()\$ ( OUTPUT enter the string3 ramesh=umar singh the length of first string is3 *.

b) For a given string C find the reverse of the string #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { char name[5 !"name*[5 !\$ clrscr()\$ cout<<%enter the string3 %\$ gets(name)\$ strrev(name)\$ cout<<%&n%<<%the reverse of first string is3 %<<name\$ getch()\$ ( OUTPUT enter the string3 ramesh =umar the reverse of first string is3 ramu= hsemar

c)

## ?om2are the string C )ith another string C*

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { char name[5 !"name*[5 !\$ int len"cm2_rst\$ clrscr()\$ cout<<%enter first string3 %\$ gets(name)\$ cout<<%enter second string3 %\$ gets(name*)\$ cm2_rst#strcm2(name"name*)\$ if(cm2_rst## ) cout<<%&n%<<%both strings are same% \$ else cout<<%&n%<<%strings are different%\$ getch()\$ ( OUTPUT enter first string3 ram enter second string3 ramesh strings are different

d) 422end another string C* to the string C #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { char name[5 !"name*[5 !\$ clrscr()\$ cout<<%enter first string3 %\$ gets(name)\$ cout<<%enter second string3 %\$ gets(name*)\$ strcat(name"name*)\$ cout<<%the concatenated string is %<<name\$ getch()\$ ( OUTPUT enter first string3 yog enter second string3 =un)ar the concatenated string is yog=un)ar

## e) Find a given sub9string C* in the string C

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { int i# "1# "=# "l# "=*# \$ char a[+ !"b[+ !\$ clrscr()\$ cout<<%&n<nter main string3&n%\$ gets(a)\$ cout<<%&n<nter sub9string3&n%\$ gets(b)\$ l#strlen(b)\$ )hile (a[i!8#<BF) { if (a[i!##b[1!) { i''\$ 1''\$ =*#*\$ if (1##l) { 1# \$ =#*\$ ( ( else { if (=*##*) { 1# \$ =*# \$ ( else i''\$ ( ( if (=##*)

{ cout<<%&n&nHhe given sub9string is 2resent in the main string%\$ ( else { if (=## ) cout<<%&n&nHhe given sub9string is not 2resent in the main string%\$ ( getch()\$ (

OUTPUT

<nter main string3 yog =un)ar <nter sub9string3 =un Hhe given sub9string is 2resent in the main string

Program 5

Cort the given array of strings #include<stdio.h> #include<iostream.h> #include<string.h> #include<conio.h> void main() { int i"1"n\$ clrscr()\$ char str[0 ![0 !"tem2[0 !\$ cout<<%<nter the no. of string to be sorted3 %\$ cin>>n\$ for(i# \$i<n\$i'') gets(str[i!)\$ for(i# \$i<n\$i'') for(1#i'*\$1<n\$1''){ if(strcm2(str[i!"str[1!)> ) { strc2y(tem2"str[i!)\$ strc2y(str[i!"str[1!)\$ strc2y(str[1!"tem2)\$ ( ( cout<<%Hhe sorted strings are3 &n%\$ for(i# \$i<n\$i'') 2uts(str[i!)\$ getch()\$ ( OUTPUT <nter the no. of string to be sorted3 / ram hir ramesh narada Hhe sorted strings are3 hir narada ram ramesh