Wednesday, 8 January 2014

misc


Q. Program of array

#include<iostream>
using namespace std;
class array
{
int arr[100],size;
public:
array()
{
for(int i=0;i<size;i++)
arr[i]=0;
}
void insert();
void show();
int max();
int min();
int search(int);
void sort();
void reverse();
int sum_element();
int frequency(int);
};
void array::insert()
{
cout<<"\nEnter number of terms=";
cin>>size;
cout<<"\nEnter the numbers=";
for(int i=0;i<size;i++)
cin>>arr[i];
}
void array::show()
{
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
}
int array:: max()
{
int m=arr[0];
for(int i=1;i<size;i++)
{
if(m<arr[i])
m=arr[i];
}
return m;
}
int array:: min()
{
int m=arr[0];
for(int i=1;i<size;i++)
{
if(m>arr[i])
m=arr[i];
}
return m;
}
int array:: search(int ele)
{
int loc=-1;
for(int i=0;i<size;i++)
if(ele==arr[i])
{
loc=i;
break;
}
return loc;
}
void array:: sort()
{
int temp;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
void array:: reverse()
{
int temp;
for(int i=0;i<=size/2;i++)
{
temp=arr[i];
arr[i]=arr[size-i-1];
arr[size-i-1]=temp;
}
}
int array:: sum_element()
{
int sum=0;
for(int i=0;i<size;i++)
{
sum+=arr[i];
}
return sum;
}
int array::frequency(int ele)
{
int c=0;
for(int i=0;i<size;i++)
{
if(ele==arr[i])
{
c++;
}
}
return c;
}
main()
{
array a1;
int x;
a1.insert();
a1.show();
cout<<"\nThe maximum element="<<a1.max();
cout<<"\nThe mimimum element="<<a1.min();
cout<<"\nThe sum of the elements="<<a1.sum_element();
cout<<"\nEnter an element to search=";
cin>>x;
if(a1.search(x)==-1)
cout<<"\nElement not found\n";
else
cout<<"\nElement found in the position "<<a1.search(x)<<endl;

cout<<"\nEnter a number=";
cin>>x;
cout<<"Frequency of "<<x<<" in the array="<<a1.frequency(x);
cout<<"\nThe reversed array=";
a1.reverse();
a1.show();
cout<<"\nThe sorted array=";
a1.sort();
a1.show();
return 0;
}



Q. Program of Matrices.

#include<iostream>
using namespace std;
class matrix
{
int a[100][100],r,c;
public:
matrix(){}
matrix(int m,int n)
{
r=m;c=n;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
a[i][j]=0;
}
void read();
void show();
friend matrix addmatrix(matrix,matrix);
friend matrix multiplication(matrix,matrix);
friend matrix transpose(matrix);
};
void matrix::read()
{
cout<<"\nEnter row number=";
cin>>r;
cout<<"\nEnter column number=";
cin>>c;
cout<<"\nEnter the elements of the matrix"<<endl;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>a[i][j];
}
void matrix:: show()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
}
matrix addmatrix(matrix x,matrix y)
{
matrix temp(x.r,x.c);
if(x.r!=y.r || x.c!=y.c)
cout<<"\nCannot added";
else
{
for(int i=0;i<x.r;i++)
{
for(int j=0;j<x.c;j++)
{
temp.a[i][j]=temp.a[i][j]+x.a[i][j]+y.a[i][j];
}
}
}
return temp;
}
matrix multiplication(matrix x,matrix y)
{
matrix temp(x.r,y.c);
if(x.r!=y.c)
cout<<"\nCannot multiply";
else
{
for(int i=0;i<x.r;i++)
{
for(int j=0;j<y.c;j++)
{
for(int k=0;k<temp.c;k++)
{
temp.a[i][j]=temp.a[i][j]+x.a[i][k]*y.a[k][j];
}
}
}
}
return temp;
}
matrix transpose(matrix x)
{
matrix temp(x.c,x.r);
for(int i=0;i<temp.r;i++)
for(int j=0;j<temp.c;j++)
temp.a[i][j]=x.a[j][i];
return temp;
}
main()
{
matrix m1,m2,sum,mul,tran;
m1.read();
m2.read();
cout<<"\nThe 1st matrix "<<endl;
m1.show();
cout<<"\nThe 2st matrix "<<endl;
m2.show();
sum=addmatrix(m1,m2);
cout<<"\nThe sum matrix "<<endl;
sum.show();
mul=multiplication(m1,m2);
cout<<"\nThe multiplication matrix "<<endl;
mul.show();
tran=transpose(m1);
cout<<"\nThe transpose matrix "<<endl;
tran.show();
return 0;
}




Q. Program of complex numbers.

#include<iostream>
using namespace std;
class complex
{
int real,imaginary;
public:
complex()
{
real=0;
imaginary=0;
}
void read();
void show();
complex add(complex,complex);
complex mul(complex,complex);
};
void complex::read()
{
cout<<"\nReal part=";
cin>>real;
cout<<"\nImaginary part=";
cin>>imaginary;
}
void complex::show()
{
if(real==0)
{
if(imaginary==0)
cout<<0;
else
cout<<imaginary<<"i";
}
else
{
cout<<real;
if(imaginary>0)
cout<<"+"<<imaginary<<"i";
else if(imaginary<0)
cout<<imaginary<<"i";
}
cout<<"\n";
}
complex complex::add(complex a,complex b)
{
real=a.real+b.real;
imaginary=a.imaginary+b.imaginary;
}
complex complex::mul(complex a,complex b)
{
real=a.real*b.real-a.imaginary*b.imaginary;
imaginary=a.real*b.imaginary+b.real*a.imaginary;
}
main()
{
complex c1,c2,c3,c4,c5,c6;
c1.read();
c2.read();
c3.add(c1,c2);
c3.show();
c4.mul(c1,c2);
c4.show();
return 0;
}


No comments:

Post a Comment