Tuesday 10 March 2020

CPP program to work with the two pair classes

Source Code :
#include<iostream>
#include<utility>
using namespace std;
int main()
{
pair<string,string> number1("Suvadeep","983");
cout<<number1.first<<":"<<number1.second<<endl;
pair<string,int> student1("Soumyodeep",40);
cout<<student1.first<<":"<<student1.second<<endl;
return 0;
}

Program Output:

CPP program to use maps

Source Code:
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string,int> grades;
grades["Suva"]=78;
grades["Soumyo"]=83;
grades["Nitai"]=92;
string name;
cout<<"Enter a name to find: ";
cin>>name;
if(grades.find(name)!=grades.end())
{
cout<<name<<": "<<grades[name]<<endl;
}
else
{
cout<<name<<" "<<"not found."<<endl;
}
double average=0.0;
int sum=0;
map<string,int>::iterator it=grades.begin();
while(it!=grades.end())
{
sum+=it->second;
++it;
}
cout<<endl;
average=sum/grades.size();
cout<<"The average grade is: "<<average<<endl;
return 0;
}

Program Output :

CPP program to implement increment of x and store it in y variable

Source Code:
#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"Enter X = ";
cin>>x;
y=x++;
cout<<"Y is = "<<y<<endl;
cout<<"X is = "<<x<<endl;
return 0;
}


Program Output:

CPP program to find transpose of a matrix

Source Code :
#include<iostream>
using namespace std;
int main()
{
int a[2][2],b[2][2],i,j;
cout<<"Enter a matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
b[i][j]=a[j][i];
}
}
cout<<"The transposed matrix is: ";

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<b[i][j]<<" ";
}

cout<<" "<<endl<<" ";
}

return 0;

}


Program Output:

CPP program to find modified even odd using function outside loop with input 2

Source Code:
#include<iostream>
using namespace std;
void EvenOdd(int T[],int c)
{
int i;
cout<<"Enter C:-";
cin>>c;
for(i=0;i<=c-1;i++)
{
if(T[i]%2==0)
{
T[i]=T[i]+2;
}
else
{
T[i]=T[i]+1;
}

}

cout<<"Modified content will be :- ";
for(i=0;i<=c-1;i++)
{
cout<<T[i];
}
}

int main()
{
int A[10],z;
EvenOdd(A,z);
return 0;
}

Program Output :

CPP program to find modified even odd using function inside loop with input 2

Source Code:
#include<iostream>
using namespace std;
void EvenOdd(int T[],int c)
{
int i;
cout<<"Enter C:-";
cin>>c;
for(i=0;i<=c-1;i++)
{
if(T[i]%2==0)
{
T[i]=T[i]+2;
}
else
{
T[i]=T[i]+1;
}

cout<<"Modified content will be :- ";
for(i=0;i<=c-1;i++)
{
cout<<T[i];
   }
}
}

int main()
{
int A[10],z;
EvenOdd(A,z);
return 0;
}


Program Output:

CPP program to find half of the numbers that is put in a float array

Source Code:
#include<iostream>
using namespace std;
int main()
{
float a[10];
int i;
cout<<"Enter the numbers: ";
for(i=0;i<10;i++)
{
cin>>a[i];
}

cout<<"The numbers are: ";
for(i=0;i<10;i++)
{
cout<<(a[i])/2;
}

return 0;

}


Program Output :

CPP program to create a class tourist and calculate the charges of touring

Source Code:
#include<iostream>
#include<string.h>
using namespace std;
class Tourist
{
int CNo;
char CType[40];
int Perkm;
int Distance;
public:
Tourist()
{
CType[40] ='A';
strcpy(CType,"0000");
}
void Citycharges()
{
if(CType[40]=='A')
Perkm=20;
if(CType[40]=='B')
Perkm=18;
if(CType[40]=='C')
Perkm=15;
}

void RegisterCab()
{
cout<<"Enter the cab.no: ";
cin>>CNo;
cout<<"Enter the cab type: ";
cin>>CType;
Citycharges();
}

void Display()
{
cout<<"Enter the Distance: ";
cin>>Distance;
cout<<"The cab no is: ";
cout<<CNo;
cout<<endl;
cout<<"Cab type is: "<<CType<<endl;
cout<<"Perkm charges are: "<<Perkm<<endl;
cout<<"The amount is: "<<Perkm*Distance<<endl;
}
};

int main()
{
Tourist T;
T.RegisterCab();
T.Display();
return 0;
}


Program Output :