Man

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 80

NATIONALINSTITUTEOFTECHNOLOGY

WARANGAL–506004

DEPARTMENTOFCOMPUTERSCIENCE&ENGINEERING
IB.Tech.,IISemester

PSCPLabAssignment- l2019

RollNo.-841902 Name:ASHUTOSH MEENA

1 Writeafunctionpower(a,b),tocalculatethevalueof araisedtob.
#include<iostream>
usingnamespacestd;
floatpower(floata,floatb);
intmain()
{
floata,c,b;
cout<<"\nentervalueofa:";
cin>>a;
cout<<"\nentervalueofb:";
cin>>b;
c=power(a,b);
cout<<"\nthevalueofaraisedtobis:"<<c;
return0;
}
floatpower(floata,floatb)
{
inti=1;
floatpow=1;
while(i<=b)
{
pow=pow*a;
i=i+1;
}
returnpow;
}
2 Considerthefollowingprocesswhichcanbeappliedtoanypositiveinteger:iftheintegeris
oddmultiplyitbythreeandaddone.Iftheintegerisevendivideitby2.Thisprocessis
repeateduntilintegerremainingis1.Ex.Thefollowingsequenceis34,17,52,26,13,40,20,
10,5,16,8,4,2,1stoptheprocesswhen1occurs.Writeafunctiontodeterminehowmany
stepsarerequiredtocompletethisprocess.Usefunctions.
#include<iostream>
usingnamespacestd;
voidcount(intn);
intmain()
{
intn;
cout<<"enterthenumber:";
cin>>n;
count(n);
return0;
}
voidcount(intn)
{
intc=0;
while(n!=1)
{
if(n%2==0)
{
n=n/2;
c=c+1;
cout<<n<<"";
}
else
{
n=3*n+1;
c=c+1;
cout<<n<<"";
}
}
cout<<"\nnumberofstepsrequired:"<<c;
return0;
}
3 a
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespacestd;
intflip();
intmain()
{
inth=0,t=0,i,a;
srand(time(0));
for(i=0;i<100;i++)
{
a=flip();
if(a==0)
{
t=t+1;
cout<<"\nthe"<<i+1<<"tossistails\n";

}
else
{
h=h+1;
cout<<"\nthe"<<i+1<<"tossisheads\n";

}
}

cout<<"\nheads tails";
cout<<"\n"<<h<<" "<<t;
return0;
}
intflip()
{
inta;
a=rand()%2;
returna;}
4 Writeaprogramthatconvertsfrom24-hournotationto12-hournotation.Forexample,it
shouldconvert14:25to2:25PM.Theinputisgivenastwointegers.Thereshouldbeatleast
threefunctions,oneforinput,onetodothe conversionandoneforoutput.Recordthe
AM/PMinformationasavalueoftypechar,‘A’forAMandchar‘P’forPM.Thusthefunction
fordoingtheconversionswillhaveacall-by-referenceformalparameteroftypecharto
recordwhetheritisAMorPM.
#include<iostream>
usingnamespacestd;
voidinput(char&a,char&p);
voidconv(char&a,char&p,inth,intm);
voidoutput(inth,intm);
intmain()
{
chara='A',p='P';
input(a,p);
return0;
}

voidinput(char&a,char&p)
{
inth,m;
cout<<"\nenterthetime-";
cin>>h;
cin>>m;
conv(a,p,h,m);
}

voidconv(char&a,char&p,inth,intm)
{
if((h>12)&&(h<24)&&(m<=59))
{
h=h-12;
output(h,m);
cout<<""<<p<<'M';
}
elseif((h<=12)&&(h>=0)&&(m<=59))
{
if((h==12)&&(m==0))
{
cout<<h<<":"<<m<<""<<p<<'M';
}
else
{

output(h,m);
cout<<""<<a<<'M';
}

}
else

cout<<"\ninvalidtime\n";
}

voidoutput(inth,intm)
{
cout<<"\ntimein12hrnotation-\n"<<h<<":"<<m;
}
5 Writeafunctiontoperformmultiplicationoftwocomplexnumbers.Thevalueparametersto
thefunctionshouldbereal1,imag1mreak2,imag2andthereferenceparametersshouldbe
realpartandimagpart.Theformulais
(a+ib)X(c+id)=(ac-bd)+i(bc+ad)
#include<iostream>
usingnamespacestd;
voidmul(int&,int&,int&,int&);
intmain()
{
intreal1,imag1,real2,imag2;
cout<<"enterfirstnumber:";
cin>>real1>>imag1;
cout<<"entersecondnumber:";
cin>>real2>>imag2;
mul(real1,imag1,real2,imag2);
return0;
}
voidmul(int&r1,int&i1,int&r2,int&i2)
{
cout<<"multiplicationoftwonumbersis:"<<(r1*r2-i1*i2)<<"+i"<<(i1*r2+r1*i2);
}

6 (i)Writeafunction nextday()totransformagivenmonthdayandayearintothe after. (ii)Writeafunction


prevday()totransformagivenmonthdayandayearintothe before. (i)
#include<iostream>
usingnamespacestd;
voidnextday(intd,intm,inty);
intmain()
{
intd,m,y;
cout<<"enterday,month,year:";
cin>>d>>m>>y;
if((m==2)&&(d<=29))
{
if((d==29)&&(((y%100)==0)&&((y%400)==0))||(((y%100)!=0)&&((y%4)==0)))
nextday(d,m,y);
elseif(d<29)
nextday(d,m,y);
else
cout<<"\ninvaliddate\n";
}

elseif((d<=31)&&(m<=12)&&(m!=2))
nextday(d,m,y);
else

cout<<"\ninvaliddate";
return0;
}
voidnextday(intd,intm,inty)
{
if((m==1)||m==3||m==5||m==7||m==8||m==10)
{
if(d==31)
{
m=m+1;d=1;
}
else

d=d+1;
}

elseif(m==12)
{
if(d!=31)
d=d+1;
else
{

d=1;m=1;y=y+1;
}
}

elseif(m==4||m==6||m==11||m==9)
{
if(d==30)
{
d=1;m=m+1;
}
elseif(d<30)
d=d+1;
e {
l
s
e
}
else
cout
<<"\
ninv
alid
date
\n";
if((((y%100)==0)&&((y%400)==0))||(((y%100)!=0)&&((y%4)==0)))
{
if(d==29)
{
d=1;m=m+1;
}
elseif(d==28)
d=d+1;
else
d=d+1;

}
else
{

if(d!=28)
d=d+1;
else
{

d=1;m=m+1;
}
}

}
cout<<"\n"<<d<<""<<m<<""<<y;
}
(i)
#include<iostream>
usingnamespacestd;
voidprevday();
intmain()
{
prevday();
return0;
}
voidprevday()
{
intd,m,y;
cout<<"Enteravaliddate\n";
cin>>d>>m>>y;
cout<<"Theentereddateis"<<d<<"/"<<m<<"/"<<y<<endl;
if(d==1)
{
if(m==3)
{
if((y%400==0)||((y%100!=0)&&(y%4==0)))
{
d=29;
m--;

}
else
{

d=28;
m--;
}
}
elseif((m==5)||(m==7)||(m==8)||(m==10)||(m==12))
{
d=30;
m--;
}
elseif((m==2)||(m==4)||(m==6)||(m==9)||(m==11))
{
d=31;
m--;
}
elseif(m==1)
{
d=31;
y--;
m=12;

}
}
else
{
d--;
}
cout<<"Thepreviousdayofthegivendateis"<<d<<"/"<<m<<"/"<<y;
}

7 Writeaprogramtofindallprimefactorsofagivennumberalongwiththeirexponents.
#include<iostream>
usingnamespacestd;
intmain()
{
intn,i,j,t,c;
cout<<"enterthenumber:";
cin>>n;
t=n;
for(i=2;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
break;
}
}
if(j==i)
{ if(n%j==0)
{
c=0;
while(t%j==0)
{
t=t/j;
c=c+1;
}
cout<<j<<"tothepower"<<c<<"\n";
}
}
}
cout<<"\n"<<"okthanku";
}

8 DeveloparecursivefunctionfortowersofHanoiproblem
#include<iostream>
usingnamespacestd;
voidabcd(int,char,char,char);
intmain()
{
intn;
cin>>n;
abcd(n,'A','C','B');
return0;
}
voidabcd(intk,chara,charc,charb)
{
if(k>0)
{
abcd(k-1,a,b,c);
cout<<"Movedisk"<<k<<"fromrod"<<a<<"torod"<<c<<"\n";
abcd(k-1,b,c,a);
}
}
9 WriteafunctiontocalculatenthnumberintheNewman-ConwaysequencedefinedbyP(1)=
P(2)=1,andforn>=3 P(n)=P(P(n-1))+P(n-P(n-1))
#include<iostream>
usingnamespacestd;
intnc(intn);
intmain()
{
intn;
cout<<"enternumber:";
cin>>n;
if(n>=3)
cout<<nc(n);
else
cout<<"enterno.grtrthan2\n";
return0;
}
intnc(intn)
{
intt=n;
while(n>=1)
{
if(n==1||t==1)
return(1);
elseif(n==2||t==2)
return(1);
else
return(nc(nc(n-1))+nc(t-nc(t-1)));
}
}
10 Writeanalgorithmandaprogramtosortanunsortedlistofnelements.Theprogramshould
alsodisplaysthenumberofswappingoperationsperformedforagivenlistofelements.
(Bubblesort,SelectionsortandInsertionsort)
#include<iostream>
usingnamespacestd;
intn;
intmain()
{
cout<<"Enternumberofelements:";
cin>>n;
intA[n],B[n],C[n];
cout<<"Enterthearrayelements:";
for(inti=0;i<n;i++)
{
cin>>A[i]
;
B[i]=A[i];
C[i]=A[i];
}
intcountbubble=0;
for(inti=0;i<n;i++)
{
for(intj=0;j<n-1-i;j++)
{
if(A[j]>A[j+1])
{
intt=A[j];
A[j]=A[j+1];
A[j+1]=t;
countbubble++;
}
}
}
cout<<"SORTEDARRAY:\n";
for(inti=0;i<n;i++)
{
cout<<A[i]<<endl;
}
cout<<"BUBBLESORT:Numberofswappingoperations="<<countbubble<<endl;
intcountselection=0;
for(inti=0;i<n;i++)
{
intM=B[i];
intp=i;
for(intj=i+1;j<n;j++)
{
if(B[j]<M)
{

}
} M=B[j];
if(p!=i) p=j;
{

intt=B[i];
B[i]=M;
B[p]=t;
countselection++;
}
}
cout<<"SELECTIONSORT:Numberofswappingoperations="<<countselection<<endl;
intcountinsertion=0;
for(inti=1;i<n;i++)
{
intt=C[i];
intj=i;
while(j>0&&C[j-1]>t)
{
C[j]=C[j-1]; j--;
countinsertion++;
}
C[j]=t;
}
cout<<"INSERTIONSORT:Numberofswappingoperations="<<countinsertion<<endl;
cout<<"SORTEDARRAY:\n";
return0;
}

11 Writeaprogramtoperformfollowingoperationsonarrays
(i) Insertinganelementatspecifiedposition
(ii) Deletinganelementatspecifiedposition
(iii) Replacinganelementatspecifiedposition
(iv) Searching
#include<iostream>
usingnamespacestd;
intmain()
{
intn;
cout<<"Entersizeofarray:";
cin>>n;
intA[n+1];
cout<<"Enterthearrayelements:\n";
for(inti=0;i<n;i++)
{
cin>>A[i];
}
intv,p;
cout<<"Entervalueandpositionforinsertionrespectively:\n";
cin>>v>>p;
for(inti=n;i>p;i--)
{
A[i]=A[i-1];
}
n++;
A[p]=v;
cout<<"Newarray:";
for(inti=0;i<n;i++)
{
cout<<A[i]<<"";
}
cout<<"\nEnterpositionfordeletion:\n";
cin>>p;
for(inti=p;i<n;i++)
A[i]=A[i+1];
n--;
cout<<"Newarray:";
for(inti=0;i<n;i++)
{
cout<<A[i]<<"";
}
cout<<"\nEnterthenewvalueandpositiontoreplacerespectively:\n";
cin>>v>>p;
A[p]=v;
cout<<"Newarray:";
for(inti=0;i<n;i++)
{
cout<<A[i]<<"";
}
intf=0;

cout<<"\nEnterthenumbertobesearched:";
cin>>v;
for(inti=0;i<n;i++)
{
if(A[i]==v)
{
f=1;
cout<<"Elementfound";
break;
}
}
if(f==0)
cout<<"Elementnotfound";
return0;
}
12 LetAandBbetwoarrays.WriteafunctiontocreateanewarrayCthatcontainselements
alternatelyfromAandBbeginningwiththefirstelementofA. Usepointertoaccessthe
elementsfromthearrayC. Ifyourunoutofelementsinoneofthelists(arrays),thenappend
theremainingelementsoftheotherlist(array)toC.
#include<iostream>
usingnamespacestd;
intmain()
{
inti,j,r,t,a[50],b[50],c[100];
cout<<"entersizeofarrayaandb";
cin>>r>>t;
cout<<"enterelementsofarraya\n";
for(i=0;i<r;i++)
cin>>a[i];
cout<<"enterelementsofarrayb\n";
for(i=0;i<t;i++)
cin>>b[i];
for(i=0;i<r&&i<t;i++)
{
c[2*i]=a[i];
c[(2*i)+1]=b[i];
}
if(i==r)
{
inti=r;
for(j=2*r;j<(r+t);j++)
c[j]=b[i++];
}
else
{
inti=t;
for(j==2*t;j<r+t;j++)
c[j]=a[i++];
}
cout<<"elementsofarrayc\n";
for(i=0;i<r+t;i++)
cout<<c[i]<<"";
return0;
}
13 Createanarraycalledpolynomialandperformthefollowingoperations:
(i)Additionoftwopolynomials(ii)Subtractionoftwopolynomials
(iii)Multiplicationoftwopolynomials.
#include<iostream>
usingnamespacestd;
intmain()
{
intn,i,j;
cout<<"enterthehighestdegree:";
cin>>n;
inta[n],b[n],add[n],sub[n],mul[2*n];
cout<<"entercoeffoffirstpolynomial:"<<endl;
for(i=0;i<n;i++)
{
cout<<"entercoeff.ofx"<<i<<"";
cin>>a[i];
}
cout<<endl<<"entercoeffofsecondpolynomial:"<<endl;
for(i=0;i<n;i++)
{
cout<<"entercoeff.ofx"<<i<<"";
cin>>b[i];
}
for(i=0;i<n;i++)
{
add[i]=a[i]+b[i];
sub[i]=a[i]-b[i];
}
for(i=0;i<(2*n);i++)
mul[i]=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
mul[i+j]=mul[i+j]+(a[i]*b[j]);
}
}
cout<<endl<<"afteradding:";
for(i=0;i<n;i++)
{
cout<<endl<<"coeff.ofx"<<i<<"is"<<add[i];
}
cout<<endl<<"aftersubtracting:";
for(i=0;i<n;i++)
{
cout<<endl<<"coeff.ofx"<<i<<"is"<<sub[i];
}
cout<<endl<<"aftermultiplying:";
for(i=0;i<(2*n);i++)
{
cout<<endl<<"coeff.ofx"<<i<<"is"<<mul[i];
}
return0;
}
15 Asquarematrixisamatrixwiththesamenumberofrowsandcolumns.Writeaprogramto
findthesumofdiagonalelementsofasquarematrixAofsizenxn.Theprogramshould
alsodeterminethegivenmatrixisanuppertriangularmatrixornot.
#include<iostream>
usingnamespacestd;
intmain()
{
intn;
cout<<"Enterorderofarray:";
cin>>n;
intA[n][n];
cout<<"Enterarrayelements:\n";
for(inti=0;i<n;i++)
{
for(intj=0;j<n;j++)
{
cin>>A[i][j];
}
}
intsumdiag=0;
for(inti=0;i<n;i++)
sumdiag+=A[i][i];
cout<<"Sumofprincipaldiagonalelementsis:"<<sumdiag<<endl;
intf=0;
for(inti=1;i<n;i++)
{
for(intj=0;j<i;j++)
{
if(A[i][j]!=0)
{ f=1;
break;
}
}
}
if(f==0)
cout<<"Upperdiagonalmatrix\n";
else
cout<<"Notanupperdiagonalmatrix\n";
return0;
}
16 Writeprogramforbignumberarithmetic.(Addition,SubtractionandMultiplication)
include<iostream>
usingnamespacestd;
intmain()
{intflag,diff,sum=0,i,j,c[30],d[30];
chara[20],b[20];
cout<<"givethetwonumbers"<<endl;
cout<<"pleaseenterthesmallernumberfirst"<<endl;
cin.getline(a,20);
cin.getline(b,20);
for(i=0;a[i]!='\0';i++);
for(j=0;b[j]!='\0';j++);
intlen1=i,len2=j,k=j;
if(len1<=len2)
for(i=len1-1;i>=0;i--)
{
sum=sum+a[i]+b[k-1]-96;
c[k-1]=sum%10;
sum=sum/10;k--;
}
for(i=len2-len1-1;i>0;i--)
{
sum=sum+b[i]-48;
c[i]=sum%10;
sum=sum/10;
}
c[i]=sum+b[i]-48;
cout<<"thesumof2numbersis"<<endl;
for(i=0;i<len2;i++)
cout<<c[i];
cout<<endl;
k=len2;
for(i=len1-1;i>=0;i--)
{
diff=b[k-1]-a[i];
if(diff>=0)
d[k-1]=diff;
else
{
d[k-1]=10+diff;
b[k-2]--;
}
k--;}
for(i=len2-len1-1;i>=0;i--)
d[i]=b[i]-48;
cout<<"thepositivevalueofdifferencebetween2numbersis"<<endl;
for(i=0;i<len2;i++)
cout<<d[i];
cout<<endl;
return0;
}
17 AmazecanberepresentedbyatwodimensionalBooleanarrayinwhichtrueelements
representswallsandfalseonerepresentshall-ways.ForExamplewriteaprogramtomovea
micethroughamazesorepresented.
* * * *
* * *
* * *
* * * *
* * * *

#include<iostream>
usingnamespacestd;
intmain()
{
boolA[5][5];
cout<<"Entermaze(entryfrombottom):\n";
for(inti=0;i<5;i++)
{
for(intj=0;j<5;j++)
{
cin>>A[i][j];
cout<<"";
}
cout<<endl;
}
cout<<"THEMAZEIS:\n";
for(inti=0;i<5;i++)
{
for(intj=0;j<5;j++)
{
if(A[i][j]==0)
cout<<'*';
else
cout<<'';
cout<<'';
}
cout<<endl;
}
intj;
inti=4;

for(j=0;j<5;j++)
{
if(A[i][j])
break;
}
do
{
if(A[i][j]==1)
{
if(A[i-1][j]==1)
{
cout<<"Moveup\n";
i--;
}
elseif(A[i][j+1]==1)
{
cout<<"Moveright\n";
j++;
}
elseif(A[i][j-1]==1)
{
cout<<"Moveleft\n";
j--;

}
else
{
cout<<"Noway";
break;

}
}
}

while(i!=0&&j!=0&&j!=4);
return0;

18 Writeaprogramwhichcallsarecursivefunctiontodeterminethegivenstringispalindrome
ornotandalsowritearecursivefunctiontofindthelengththegivenstring.
#include<iostream>
usingnamespacestd;
boolpal(char[],int);
intlength(char[],int);
intmain()
{
charch[10];
intl=0;
cout<<"Entertheword:";
cin>>ch;
l=length(ch,0);
cout<<"Lengthis"<<l<<endl;
boolb=pal(ch,l);
if(b)
cout<<"Itisapalindromeword"<<endl;
else
cout<<"Itisnotapalindromeword"<<endl;
return0;
}
intlength(chars[],intn)
{
if(s[n]!='\0')
{
n++;
length(s,n);

}
else

returnn;
}

intk=0;
boolpal(chars[],intl)
{
if(s[k]==s[l-1-k])
{
if(k=l/2)
returntrue;
else
k++;
pal(s,l);

}
else

returnfalse;
}
19 Writeaprogramtosortthegivennamesinlexicographicalorder(Alphabetical-order).
Ex:- abc,abcd,aba,abd
Result: aba,abc,abcd,abd
#include<cstdlib>
#include<iostream>
#include<string.h>
usingnamespacestd;
intmain()
{
intn;
cout<<"Entern:";
cin>>n;
chararr[n][20];
cout<<"Enterthenames:\n";
for(inti=0;i<n;i++)
{
cin>>arr[i];
}
for(inti=0;i<n;i++)
{
for(intj=0;j<n-1-i;j++)
{
if(strcmp(arr[j],arr[j+1])>0)
{
chartemp[20];
strcpy(temp,arr[j]);
strcpy(arr[j],arr[j+1]);
strcpy(arr[j+1],temp);
}
}
}
cout<<"Sortedlistis:\n";
for(inti=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
return0;
}

20 Writeaprogramtosearchforagivensubstringinsideagivenstring.Ifitisfoundinthe
string,thenprintthepositionsofalltheoccurrences.
#include<iostream>
#include<string.h>
usingnamespacestd;
intmain()
{
intlength,flag=0,pos;
chars[500],ch[15];
cout<<"Enterthestring:\n";
cin.getline(s,500,'\n');
cout<<"Enterthesubstringtosearch:\n";
cin>>ch;
for(length=0;ch[length]!='\0';length++)
for(inti=0;s[i]!='\0';i++)
{
for(intj=i;s[j]!='\0';j++)
{
if(s[j]==ch[0])
{
pos=j;
intk=0;
while(s[j]==ch[k]){
j++;
k++;
}
if(k>=length)
{
flag=1;
break;
}
}
}
if(flag==1)
break;
}
if(flag==1)
cout<<"Positionofsubstring="<<pos+1<<endl;
else
cout<<"Substringnotpresent";
return0;
}
21 Writeaprogramthatreadsmultiplelinesoftextandfindsthefrequenciesofcharactersand words.
#include<iostream>
#include<cstring>
usingnamespacestd;
intmain()
{
inti,j=0,k,a,c=0;
chars[500],ch[50];
cout<<"Enterthestring\n";
cin.getline(s,500,'\n');
cout<<"Enterthewordorcharyouwanttosearch\n";
cin>>ch;
while(ch[j]!='\0')
j++;
for(i=0;s[i]!='\0';i++)
{
if(j==1)
{
if(s[i]==ch[0])
c++;

}
else
{
if(s[i]==ch[0])
{
k=0; a=i;
while(s[i]==ch[k])
{
i++;
k++;

}
i=a;

if(k==j)
c++;
}
}
}
cout<<ch<<"isrepeated"<<c<<"times\n";
return0;
}
22 Writeaprogramthatinputsatelephonenumberasastringintheform (0870)0870-
311990.Theprogramshouldusefunctionstringtokenizer(strtok())toextracttheareacode
asatoken,thefirstfourdigitsofthephonenumberasatoken,andthelastsixdigitsofthe
phonenumberasatoken.Thetendigitsofthetokenshouldbeconcatenatedintoonestring.
Theprogramshouldconverttheareacodestringtointandtheconvertthephonenumber
stringtolong.Boththeareacodeandthephonenumbershouldbeprinted.
#include<iostream>
#include<string.h>
#include<cstdlib>
usingnamespacestd;
intmain()
{
charphone[15],*tok,phonenum[20],areacode[5];
cout<<"Enterthephonenumber:\n"; cin>>phone;
tok=strtok(phone,"-");
strcpy(areacode,tok);
tok=strtok(NULL,"");
strcpy(phonenum,tok);
cout<<endl<<"Areacode:"<<atoi(areacode);
cout<<endl<<"Phonenumber:"<<phonenum<<endl;

strcat(areacode,phonenum);
cout<<"10digitphonenumberis:"<<atol(areacode);
}

23 WriteaprogramtoreadthecontentfromatextfileIN.TXT,countthenumberofalphabets,
digitsandspecialcharacterspresentinitandwritetheseinformationintoatextfile OUT.TXT.
#include<iostream>
#include<fstream>
usingnamespacestd;
intmain()
{
ifstreamfin;
ofstreamfout;
fin.open("in.txt");
fout.open("out.txt");
inta=0,d=0,sp=0;
chars;
while(!fin.eof())
{
fin>>s;
fout<<s;
if((int(s)>=65&&int(s)<=90)||(int(s)>=97&&int(s)<=122))
a++;
elseif(int(s)>=48&&int(s)<=57)
d++;
else
sp++;
cout<<int(s)<<endl;
}
fout<<"numberofalphabets"<<a<<endl<<"numberofdigits"<<d<<endl<<"numberofspecial
characters"<<sp<<endl;
fin.close();
fout.close();
return0;
}
24 Writeaprogramthatreadsatextfileandcreatesanotherfilethatisidenticalexceptthat
everysequenceofconsecutiveblankspacesisreplacedbyasinglespace.
#include<iostream>
#include<fstream>
usingnamespacestd;
intmain()
{ifstreamfin;
ofstreamfout;
charch[500];inti,j;
fin.open("x.txt");
fout.open("y.txt");
fin>>ch[0];
fout<<ch[0];
for(i=1;fin.get(ch[i]);i++)
{
if(ch[i-1]==''&&ch[i]=='')
continue;
else{

fout<<ch[i];
}}
;

fin.close();
fout.close();
return0;
}

25 TwofilesFILE1andFILE2containsortedlistsofintegers.Writeaprogramtoproduceathird
fileDATAwhichholdsasinglesorted,mergedlistofthesetwolists.
#include<iostream>
#include<fstream>
usingnamespacestd;

intmain()
{
ifstreamfin1,fin2;
ofstreamfout;
fin1.open("File1.txt");
fin2.open("File2.txt");
fout.open("Data.txt");
intcount=0;
intAr[50];
while(fin1.good())
{
fin1>>Ar[count];
count++;
}
while(fin2.good())
{
fin2>>Ar[count];
count++;
}

for(inti=0;i<count-1;i++)
{
for(intj=0;j<count-i-1;j++)
{
if(Ar[j]>Ar[j+1])
{
inttemp=Ar[j];
Ar[j]=Ar[j+1];
Ar[j+1]=temp;
}
}
}
for(inti=0;i<count;i++)
fout<<Ar[i]<<endl;
cout<<"SortedDATA.txtcreatedsuccessfully."<<endl;
return0;

}
26 AssumingthatatextfilenamedFIRST.TXTcontainssometextwrittenintoit,writeafunction
namedvowelwords(),thatreadsthefileFIRST.TXTandcreatesanewfilenamed
SECOND.TXT,tocontainonlythosewordsfromthefileFIRST.TXTwhichstartwitha
lowercasevowel(i.e.,with'a','e','i','o','u').Forexample,ifthefileFIRST.TXTcontains“Carry
umbrellaandovercoatwhenitrains”.ThenthefileSECOND.TXTshallcontain“umbrellaand overcoatit”.
#include<iostream>
#include<fstream>
usingnamespacestd;

voidvowels()
{
ifstreamfin; ofstreamfout;
fin.open("FIRST.txt");
fout.open("SECOND.txt");
charword[10];
while(fin.good())
{
fin>>word;
if(word[0]=='a'||word[0]=='e'||word[0]=='i'||word[0]=='o'||word[0]=='u')
fout<<word<<"";
}
}
intmain(){vowels();}

27 APointonthe2-Dplanecanberepresentedbytwonumbers:anX-coordinateandY-
Coordinate.Forexample,(2,3)representsapoint2unitstotherightoftheoriginalongthex- axisand3-
unitsupthey-axis.Theproductofthetwopointscanbedefinedasnewpoint whosex-
coordinateistheproductoftheX-coordinateofthetwopoints,andwhosey- coordinateistheproductoftheiry-
coordinates.Writeaprogramthatusesastructurecalled
pointtomodelapoint.Definethreepoints,andhavetheuserinputvaluestotwoofthem.
Thensetthethirdpointequaltotheproductoftheothertwo,anddisplaythevalueofthe
newpoint.
#include<iostream>
usingnamespacestd;
structpoint{
intx,y;
};
intmain()
{
pointa,b,c;
cout<<"Enterxandycoordinatesofpointa:";
cin>>a.x>>a.y;
cout<<"Enterxandycoordinatesofpointb:";
cin>>b.x>>b.y;
c.x=a.x*b.x;
c.y=a.y*b.y;
cout<<"Productofpointsis:"<<c.x<<","<<c.y<<endl;
return0;
}

28 WriteaprogramtocreateastructurecalledBankDeposit.Structuremembersareamt
(amounttodepositinbank),tenure(No.ofyearsdeposittobemaintained).Createanother
structurecalledDates,structuremembersareintdate,intmonth,intyear.EnterDOBof
person,DateofdepositusingDatesstructurevariables.Calculatepresentageoftheperson.
Ifthepersonisseniorcitizen(age>=60yrs)thenrateofinterestis9%else8%.Calculatethe
totalamountthatpersonreceivesafterdateofmaturity(dateofdeposit+tenure).
#include<iostream>
usingnamespacestd;
structBankDeposit
{
doubleamt;
inttenure;
};
structDates
{
intday,month,year;
};
intmain()
{
Datesbirth,deposit,present;
BankDepositbank;
cout<<"Enterbirthdateinddmmyyyy:";
cin>>birth.day>>birth.month>>birth.year;
cout<<"Enterdepositdateinddmmyyyy:";
cin>>deposit.day>>deposit.month>>deposit.year;
cout<<"Enterpresentdateinddmmyyyy:";
cin>>present.day>>present.month>>present.year;
cout<<"Enteramount:";
cin>>bank.amt;
cout<<"Entertenure:";
cin>>bank.tenure;
intd,m,y;
d=present.day-birth.day;
if(d<0)
{
d=d+30;
present.day--;
}
m=present.month-birth.month;
if(m<0)
{
m=m+12;
present.year--;
}
y=present.year-birth.year;
if(y>=60)
bank.amt+=bank.tenure*0.09*bank.amt;
else
bank.amt+=bank.tenure*0.08*bank.amt;
cout<<"Amountreceivedatdateofmaturityis"<<bank.amt;
return0;
}
29 Writeaprogramusingstructurestoplaycardsgame.Systemshouldshuffleallthecardsand
usershouldpredictcontinuouslynextcardtobeshown.Ifuserpredictioniscorrectthenuser
winsotherwisecomputerwins.Thisisrepeatedforall52cards,52times.Finallydisplaythe
numberofwinsforbothsystemandtheuser.
#include<iostream>
#include<cstdlib>
#include<string.h>
usingnamespacestd;
structCard{
chartype[10];
intflag=0;
charnumber;
};
intmain()
{
inti,n,w=0,l=0;
charread[10],read2;
Cardc[53],temp;
cout<<"enternoofcards=";
cin>>n;
for(i=1;i<=n;i++)
{
if(i>=1&&i<=13)strcpy(c[i].type,"Hearts");
if(i>13&&i<=26)strcpy(c[i].type,"Diamonds");
if(i>26&&i<=39)strcpy(c[i].type,"Clubs");
if(i>39&&i<=52)strcpy(c[i].type,"Spades");
if(i==11||i==24||i==37||i==50)
c[i].number='j';
if(i==12||i==25||i==38||i==51)
c[i].number='q';
if(i==13||i==26||i==39||i==52)
c[i].number='k';
if(i%13==1)c[i].number='a';
if(i%13>1&&i%13<10)c[i].number=i%13+48;
if(i%13==10)c[i].number='0';
}
for(i=1;i<=n;i++)
{
intj=(rand()%52)+1;
strcpy(temp.type,c[i].type);
temp.number=c[i].number;
c[i].number=c[j].number;
strcpy(c[i].type,c[j].type);
strcpy(c[j].type,temp.type);
c[j].number=temp.number;
}
for(i=1;i<=n;i++)
{
cout<<"Enterthefaceandvalueofthecard"<<i<<"youguess";
cin>>read>>read2;
if(strcmp(read,c[i].type)==0&&read2==c[i].number)
w++;
else
l++;
}
cout<<"Gameover\n";
cout<<"Youhavewon"<<w<<"times\n";
cout<<"Systemhaswon"<<l<<"times\n";
return0;
}

30 Defineastructurecalledcricketthatwilldescribethefollowinginformation:playername,
teamname,battingaverage. Usingcricket,declareanarrayplayerwith50elementsand
writeaprogramtoreadtheinformationaboutallthe50playersandprintateam-wiselist
containingnamesofplayerswiththeirbattingaverage.
#include<iostream>
#include<cstring>
usingnamespacestd;
structcricket{
charpname[50],tname[50];
floatbatavg;
intflag=0;
};
intmain()
{
inti,n,j;
cout<<"Entertheno.ofplayers=";
cin>>n;
cricketp[n];
for(i=0;i<n;i++)
{
cout<<i+1<<endl;
cout<<"\tEnterPlayerName=";
cin>>p[i].pname;
cout<<"\tEnterTeamName=";
cin>>p[i].tname;
cout<<"\tEnterBattingAvg=";
cin>>p[i].batavg;
}
for(i=0;i<n;i++)
{
if(p[i].flag==0)
{
cout<<p[i].tname<<endl;
cout<<"Player:"<<p[i].pname<<endl;
cout<<"BattingAvg:"<<p[i].batavg<<endl;
for(j=i+1;j<n;j++)
{
if((strcmp(p[j].tname,p[i].tname)==0)&&p[j].flag==0)
{
cout<<"Player:"<<p[j].pname<<endl;
cout<<"BattingAvg:"<<p[j].batavg<<endl;
p[j].flag=1;
}
}
p[i].flag=1;
}
}
return0;
}
31 Writeac++programtocreateastructurestudentwiththedatafields;Name,rollno,address
andmarksin3subjectsfor10studentsanddisplayallthestudentsdetails
#include<iostream>
usingnamespacestd;
structstudent
{
charname[30],address[100];
longintrollno;
intm1,m2,m3;
};
intmain()
{
students[3];
for(inti=0;i<3;i++)
{
cout<<endl<<"enterdetailsforstudent"<<i+1<<endl;
cout<<"EnterName=";
cin>>s[i].name;
cout<<"EnterRollno.=";
cin>>s[i].rollno;
cout<<"EnterAddress=";
cin>>s[i].address;
cout<<"EnterMarksforallthesubjects:";cin>>s[i].m1>>s[i].m2>>s[i].m3;
}
for(inti=0;i<3;i++)
{
cout<<"Name="<<s[i].name<<endl;
cout<<"Rollno.="<<s[i].rollno<<endl;
cout<<"Address="<<s[i].address<<endl;
cout<<"Marks="<<endl;
cout<<s[i].m1<<""<<s[i].m2<<""<<s[i].m3<<endl;

}
return0;
}
32 CreateaclasscomplexforperformingarithmeticwithcomplexProvidepublicmember
functionsforeachofthefollowingaddition,substraction,multiplicationoftwocomplex
numbersanddisplayingtheresultnumbers.Writeadriverprogramtotestyourclass.
Complexnumberhavetheform:realpart+imaginarypart*i.Where‘i’issqrt(-1).
#include<iostream>
usingnamespacestd;

classcomplex
{
intreal,imag;
public:
voidgetcomplex()
{
cout<<"Enterrealandimaginarypartsofthecomplexno.=";
cin>>real>>imag;
}
complexadd(complexa,complexb)
{
complexans;
ans.real=a.real+b.real;
ans.imag=a.imag+b.imag;
returnans;
}
complexsub(complexa,complexb)
{
complexans; ans.real=a.real-
b.real; ans.imag=a.imag-
b.imag; returnans;
}
complexmult(complexa,complexb)
{
complexans;
ans.real=(a.real*b.real)-(a.imag*b.imag);
ans.imag=(a.imag*b.real)+(a.real*b.real);
returnans;
}
voidprint()
{
cout<<real<<"+"<<imag<<"i";
}
};

intmain()
{
complexc1,c2,sum,diff,prod;
c1.getcomplex();
c2.getcomplex();
sum=sum.add(c1,c2);
diff=diff.sub(c1,c2);
prod=prod.mult(c1,c2);
cout<<"Sumis";
sum.print(); cout<<endl;
cout<<"Differenceis";
diff.print();
cout<<endl;
cout<<"Productis";
prod.print();
cout<<endl;
return0;
}

33 Createadateclasswithdatamembersmonth,dayandyear.Provideamemberfunctionnext
daytoincrementthegivendaybyone.Thedateobjectshouldalwaysremaininconsistant
state.Writeaprogramthatteststhenextdayfunctioninaloopthatprintsthedateduring
eachiterationofthelooptoillustratethatthenextfunctionworkscorrectly
(i)incrementingintothenextmonth(ii)Incrementingintothenextyear
#include<iostream>
usingnamespacestd;
intleap(inty)
{
if(y%4==0&&y%100!=0)
return1;
if(y%400==0)
return1;
elsereturn0;
}
classdate
{
intday,month,year;
public:
date()
{
cout<<"Enterthedate="<<endl;
cin>>day>>month>>year;
}
voidnextday()
{

if(day==31||(day==30&&(month==4||month==6||month==9||month==11))||(day==29&&leap(yea
r)==1&&month==2)||(day==28&&month==2))
{
day=1;
if(month==12)
{
month=1;
year+=1;
}
else
month+=1;

}
else

day+=1;
}
voidnextmonth()
{
intlimit;
if(month==2&&leap(year)==1)
limit=29;
elseif(month==2&&leap(year)==0)
limit=28;
elseif(month==4||month==6||month==9||month==11)
limit=30;
else
limit=31;
for(inti=1;i<=limit;i++)
nextday();
}
voidnextyear()
{
intlimit;
if(leap(year)==1)
limit=366;
else
limit=365;
for(inti=1;i<=limit;i++)
nextday();
}
voidprint()
{
cout<<day<<"-"<<month<<"-"<<year<<endl;
}
};

intmain()
{
dateDy;
cout<<"NextDay:";
Dy.nextday();
Dy.print();
cout<<"\nNextMonth:";
Dy.nextmonth();
Dy.print();
cout<<"\nNextYear:";
Dy.nextyear();
Dy.print();
return0;
}
34 CreateaSavingsAccountclass.Useastaticmembertocontaintheannualinterestratefor
eachofthesavers.Eachmemberoftheclasscontainsaprivatedatamembersavings
balanceindicatingtheamountthesavercurrentlyhasondeposit.Providea calculatemonthlyinterest
memberfunctionthatcalculatesthemonthlyinterestby
multiplyingthebalancebyannualinterestdividedby12;Thisinterestshouldbeaddedto
savingbalance.Provideamemberfunctionmodifyinterestratethatsetsthestaticannual
interestrateanewvalue.WriteaprogramtotesttheclassSavingsAccount
#include<iostream>
usingnamespacestd;
classSavingAccounts{
floatBal;
public:
floatAnInt=0.2,MoInt;
SavingAccounts()
{

cout<<"EnterInititalAmount:";
cin>>Bal;
}
voidCalculateMonthlyIntrest()
{
cout<<"\nCalculatingBalance.."<<endl;
MoInt=(Bal*AnInt)/12;
Bal+=MoInt;
cout<<"CurrentBalance:"<<Bal<<endl;
}
voidModifyIntrestRate()
{
cout<<"EnterAnnualIntrest:";
cin>>AnInt;
cout<<"\nAnnualIntrestModified."<<endl;

}
};

intmain()
{
SavingAccountsS1;
S1.ModifyIntrestRate();
S1.CalculateMonthlyIntrest();
return0;
}

You might also like