Search This Blog

Showing posts with label sohail_progs. Show all posts
Showing posts with label sohail_progs. Show all posts

Tuesday, 2 February 2016

Array of classes

#include<iostream>
#include<iomanip>

using namespace std;

class EMPLOYEE
{
string name;
int age;
public:
void getdata();
void putdata();
};

void EMPLOYEE::getdata()
{
cout << "\t\tEnter name of the employee;";
cin >> name;
cout << "\n\t\tEnter age of the employee:";
cin >> age;
}

void EMPLOYEE::putdata()
{
cout << "\t\t-------------------------" << endl;
cout << "\t\t" << setw(15) << left << "Name - " << name << endl;
cout << "\t\t" << setw(15) << left << "Age - " << age << endl;
cout << "\t\t-------------------------" << endl;
}

int main()
{
int n;
cout << "\t\tEnter no. of employees:";
cin >> n;
EMPLOYEE emp[n];
for(int i=0;i<n;i++)
emp[i].getdata();
for(int i=0;i<n;i++)
emp[i].putdata();
return 0;
}

Demonstrating Static Functions and Static Members

 Practical 12


#include<iostream>
#include<iomanip>

using namespace std;

class STAT
{
static int a;
int b;
public:
void getdata();
static void display();
void display2();
};

void STAT::getdata()
{
cout << "\t\tEnter number : ";
cin >> b;
}

void STAT::display()
{
cout << "Static number is " << a++ << endl;
}

void STAT::display2()
{
cout << "Static number is " << a++ << endl; 
cout << "Non Static number is " << b << endl;
}

int STAT::a;

int main()
{
STAT a;
a.getdata();
STAT::display();
a.display2();
return 0;
}



output:



enter number : 2
static number is : 0
static number is 1
non static number is : 2

Finding largest of 2 numbers using nesting of member function

Practical 11


#include<iostream>
#include<iomanip>

using namespace std;

class Number 
{
int a,b;
int largest();
public:
void getdata();
void putdata();
};

void Nimber::getdata()
{
cout << "\t\tEnter number 1: ";
cin >> a;
cout << "\t\tEnter number 2: ";
cin >> b;
}

void Number::putdata()
{
cout << "\t\tNumbers are " << a << " and " << b << endl
<< "\t\tLargest of them is " << largest() << endl;
}

int Number::largest()
{
if(a>b)
return a;
else
return b;
}

int main()
{
Number a;
a.getdata();
a.putdata();
return 0;
}



output:

enter num1 : 4
enter num2 : 8

largest of them is 8

Roots of Quadratic equation using Classes

#include<iostream>
#include<math.h>

using namespace std;

class quadratic
{
int a,b,c,d;
float x1,x2;
public:
quadratic();
void roots();
};

quadratic::quadratic()
{
cout << "Enter value of x^2" << endl;
cin >> a;
cout << "Enter value of x^1" << endl;
cin >> b;
cout << "Enter value of x^0" << endl;
cin >> c;
}

void quadratic::roots()
{
d=(pow(b,2)-4*a*c);
if(d<0)
cout << "Roots are imaginary" << endl;
else if(d==0)
{
x1=x2=-b/(2*a);
cout << "Roots are real and equal for x = " << x1 << endl;
}
else
{
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
cout << "Roots are real and distinct and x1 = "<< x1 << " and x2 = " << x2 << endl;
}
}
int main()
{
quadratic a;
a.roots();
return 0;
}

Addition and Multiplication of matrices using Classes

#include<iostream>
#include<math.h>

using namespace std;

class matrix
{
int m,n,a[10][10];
public:
matrix();
matrix(int,int);
void putdata();
int sum(matrix* ,matrix*);
int mul(matrix* ,matrix*);
void display();
};

matrix::matrix(){}

void matrix::putdata()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout << "Enter value of element in " << i << " row  and " << j << " column:" << endl;
cin >> a[i][j];
}
}
}

matrix::matrix(int a,int b)
{
m=a,n=b;
}

int matrix::sum(matrix *m1,matrix *m2)
{
if(m1->m != m2->m || m1->n != m2->n)
{
cout << "Matrices are of different order and cannot be added" << endl;
return(0);
}
else
{
m=m1->m;
n=m1->n;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
a[i][j]=m1->a[i][j]+m2->a[i][j];
}
return(1);
}
}

int matrix::mul(matrix *m1,matrix *m2)
{
if(m1->n != m2->m)
{
cout << "Matrices cannot be multiplied" << endl;
return(0);
}
else
{
m=m1->m;
n=m2->n;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=0;
for(int k=0;k<m1->n;k++)
a[i][j]=m1->a[i][k]*m2->a[k][j]+a[i][j];
}
}
return(1);
}
}

void matrix::display()
{
for(int i=0;i<m;i++)
{
cout << "| ";
for(int j=0;j<n;j++)
cout << a[i][j] << " ";
cout << "|" << endl;
}
}

int main()
{
int m,n,flag=-1,ch;
cout << "Enter no. of rows of matrix A" << endl;
cin >> m;
cout << "Enter no. of columns of matrix A" << endl;
cin >> n;
matrix a(m,n);
a.putdata();
cout << "Enter no. of rows of matrix B" << endl;
cin >> m;
cout << "Enter no. of columns of matrix B" << endl;
cin >> n;
matrix b(m,n);
b.putdata();
matrix c;
do
{
cout << "Enter choice " << endl 
<< "1.Sum " << endl
<< "2.Multiplication" << endl
<< "0.Exit" << endl;
cin >> ch;
switch(ch)
{
case 1:
flag=c.sum(&a,&b);
break;
case 2:
flag=c.mul(&a,&b);
break;
}
if(flag!=0)
c.display();
}while(ch!=0);
}



Finding Roots of Algebraic Equation using Bisection Method

#include<iostream>
#include<math.h>

using namespace std;

class Equation
{
int a[20],n;
double x,y,root,m;
public:
Equation();
void roots();
double solution(double t);
};

Equation::Equation()
{
cout << "\t\tEnter highest power of equation: ";
cin >> n;
cout << endl;
for(int i=n;i>=0;i--)
{
cout << "\t\tEnter coefficient of x^" << i << ":";
cin >> a[i];
cout << endl;
}
cout << "\t\tEnter interval for roots:";
cin >> x >> y;
cout << endl;
}

void Equation::roots()
{
if((solution(x)*solution(y))>0)
cout << "Roots does not exist in given interval (" << x << "," << y << ")" << endl;
else
{
if((y-x)<=0.00001)
cout << "Root is " << x << endl;
else
{
m=(x+y)/2;
if((solution(x)*solution(m))<0)
{
y=(x+y)/2;
roots();
}
else
{
x=(x+y)/2;
roots();
}
}
}
}

double Equation::solution(double t)
{
double x=0;
for(int i=0;i<=n;i++)
{
x=x+(a[i]*pow(t,i));
}
return x;
}

int main()
{
Equation a;
a.roots();
return 0;
}

Rail-Fence Cipher Program

#include<iostream>
#include<string.h>
#include<math.h>

using namespace std;

const int size=1000;

class Matrix
{
int d,l;
char a[size][size];
public:
Matrix(char[]);
void display();
};

Matrix::Matrix(char x[])
{
l=strlen(x);
int flag=1;
cout << "\t\tEnter depth of the text:";
cin >> d;
int j=0;
for(int i=1;i<=l;i++)
{
int flag2=0;
for(int k=0;k<d;k++)
{
if(j==k && flag2==0)
{
a[j][i-1]=x[i-1];
if(i%d==0)
flag=flag*-1;
j+=flag;
if(j<0)
j=1;
flag2=1;
}
else
a[k][i-1]='$';
}
}
}

void Matrix::display()
{
for(int i=0;i<d;i++)
{
for(int j=0;j<l;j++)
{
if(a[i][j]!='$')
cout << a[i][j];
}
}
}

int main()
{
char a[1000];
cout << "\t\tEnter string:";
cin >> a;
Matrix c(a);
c.display();
return 0;
}

enter string :
abcdef

a        e
  b   d    f
    c
enter depth :
3

aebdfc

Trans positional cipher program

#include<iostream>
#include<string.h>
#include<math.h>

using namespace std;

const int size=10;

class Matrix
{
float n;
char a[size][size];
int key[size];
public:
Matrix(char[]);
void display();
};

Matrix::Matrix(char x[])
{
int u=strlen(x);
n=ceil(sqrt(u));
int k=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(k<u)
a[i][j]=x[k++];
else
a[i][j]='$';
}
}
}

void Matrix::display()
{
cout << "\t\tEnter key using space";
for(int i=0;i<n;i++)
cin >> key[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[j][key[i]-1]!='$')
cout << a[j][key[i]-1];
}
}
}

int main()
{
char a[1000];
cout << "\t\tEnter string:";
cin >> a;
Matrix c(a);
c.display();
return 0;
}


input 

1 2 3
4 5 6
7 8 9
enter key: 3 2 1
output : 
369258147

Saturday, 23 January 2016

Sorting of Array in C++

#include<iostream>

using namespace std;

void sort(int a[],int n);                                    // call by reference is used
int max(int a[],int n);                                     
// call by reference is used 
 
int main()
{
    int n;
    cout << "Enter size of array" << endl;
    cin >> n;
    int a[n];                                                       // Dynamic Initialization of array
    for(int i=0;i<n;i++)
        cin >> a[i];
    sort(a,n);
    cout << "Sorted array:" << endl;
    for(int i=0;i<n;i++)
        cout << a[i] << endl;
    return 0;
}

void sort (int a[],int n)
{
    int temp,ind;
    for(int i=n-1;i>0;i--)
    {
        ind=max(a,i);
        temp=a[ind];
        a[ind]=a[i];
        a[i]=temp;
    }   
}

int max(int a[],int n)
{
    int max=0,ind;
    for(int i=0;i<=n;i++)
    {
        if(max<a[i])
        {
            max=a[i];
            ind=i;
        }
    }
    return ind;
}

Function Overloading In C++

#include<iostream>

using namespace std;

void power(double m,int n=2);                //default value of n is set to 2
void power(int m,int n=2);                      
//default value of n is set to 2
 
int main()
{
    int m,n;
    double dm;
    cout << "Enter the integer value of m"<<endl;
    cin >> m;
    cout << "Enter the double value of m"<<endl;
    cin >> dm;
    cout << "Enter the integer value of n"<<endl;
    cin >> n;
    power(dm,n);
    power(m,n);
    power(dm);                                            //default value from prototype is used
    power(m);   
                                           //default value from prototype is used
 }

void power(double m,int n)
{
    double r=m;
    for(int i=1;i<n;i++)
    r=r*m;
    cout << "Value of "<< m <<" raise to "<< n <<" is "<<r<<endl;
}

void power(int m,int n)
{
    int r=m;
    for(int i=1;i<n;i++)
    r=r*m;
    cout << "Value of "<< m <<" raise to "<< n <<" is "<<r<<endl;
}