Professional Documents
Culture Documents
C++ Basic Programs
C++ Basic Programs
Assignment ON:
C++ programs
Submitted to:
Dr. Fayyaz Hussain
Submitted by:
Ghulam Abbas
Roll No.
BSM-P-19-09
Class:
Bs-Physics (Morning)5th Semester
Department of
physics
Program# 01
*//multiplication of matrices//*
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main ()
{
Clrscr ();
int I , j, k ,a[10][10],b[10][10],c[10][10]={0};
int r1, c1, r2, c2;
cout<<"enter 1st matrix rows and columns=";
cin>>r1>>c1;
cout<<"enter 2nd matrix rows and columns=";
cin>>r2>>c2;
if(c1==r2)
{
cout<<"multiplictaion is possible:"<<endl;
if(c1!=r2)
cout<<"multiplication is not possible:"<<endl;
}
for(i=0;i<r1;i++)
{
cout<<endl;
cout<<"enter elements of matrix a=";
for(j=0;j<c1;j++)
cin>>a[i][j];
}
for(i=0;i<r2;i++)
{
cout<<endl;
cout<<"enter elements of matrix b=";
for(j=0;j<c2;j++)
cin>>b[i][j];
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j] + a[i][k]*b[k][j];
}}}
cout<<endl<<"multiplication of matrices="<<endl;
for (i=0; i<r1; i++)
{
for(j=0;j<c1;j++)
cout<<c[i][j]<<" ";
}
cout<<endl;
}
getch();
}
Output
enter 1st matrix rows and columns=3 3
enter 2nd matrix rows and columns=3 2
multiplication is possible:
enter elements of matrix a=1 2 3 4 5 6 7 8 9
enter elements of matrix b= 1 2 3 4 5 6
multiplication of matrices=
22 28
49 64
76 100
Program#02
*//Addition of matrices//*
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,j,r1,c1,r2,c2,a[20][20],b[20][20],c[20][20];
cout<<"enter rows and columns of 1st matrix=";
cin>>r1>>c1;
cout<<"enter rows and columns of 2nd matrix=";
cin>>r2>>c2;
if((r1!=r2)&&(c1!=c2))
{
cout<<"addition is not possible";
}
for(i=0;i<r1;i++)
{
for(j=o;j<c1;j++)
}
cout<<"enter elements of 1st matrix=";
cin>>a[i][j];
for(i=0;i<r2;i++)
{
cout<<endl;
for(j=0;j<c2;j++)
}
cout<<"enter elements of 2nd matrix=";
cin>>b[i][j];
for(i=0;i<r1;i++)
{
cout<<endl;
for(j=0;j<c1;j++)
cout<<endl;
{
c[i][j]=a[i][j]+b[i][j];
cout<<"addition of matrices="<<endl;
for(i=0;i<r1;i++)
{
cout<<endl;
for(j=0;j<c1;j++)
}}}
cout<<c[i][j]<<endl;
getch ();
}
Output
enter rows and columns of 1st matrix=2 2
enter rows and columns of 2nd matrix=2 2
enter elements of 1st matrix= 1 2 3 4
enter elements of 2nd matrix= 1 2 3 4
addition of matrices=
2 4
6 8
Program#03
*//Subtraction of matrices//*
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,j,r1,c1,r2,c2,a[20][20],b[20][20],c[20][20];
cout<<"enter rows and columns of 1st matrix=";
cin>>r1>>c1;
cout<<"enter rows and columns of 2nd matrix=";
cin>>r2>>c2;
if((r1!=r2)&&(c1!=c2))
{
cout<<"subtraction is not possible";
}
for(i=0;i<r1;i++)
{
for(j=o;j<c1;j++)
}
cout<<"enter elements of 1st matrix=";
cin>>a[i][j];
for(i=0;i<r2;i++)
{
cout<<endl;
for(j=0;j<c2;j++)
}
cout<<"enter elements of 2nd matrix=";
cin>>b[i][j];
for(i=0;i<r1;i++)
{
cout<<endl;
for(j=0;j<c1;j++)
cout<<endl;
{
c[i][j]=a[i][j]+b[i][j];
cout<<"addition of matrices="<<endl;
for(i=0;i<r1;i++)
{
cout<<endl;
for(j=0;j<c1;j++)
}}}
cout<<c[i][j]<<endl;
getch ();
}
Output
Program#04
*//Inverse of matrix//*
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,j,k,a[3][3],b[3][3];
double c[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"enter the element"<<i+1<<":"<<j+1<<"=";
cin>>a[i][j];
}
}
b[0][0]=a[1][1]*a[2][2]-a[1][2]*a[2][1];
b[0][1]=-1*(a[1][0]*a[2][2]-a[1][2]*a[2][0]);
b[0][2]=a[1][0]*a[2][1]-a[1][1]*a[2][0];
b[1][0]=-1*(a[0][1]*a[2][2]-a[0][2]*a[2][1]);
b[1][1]=a[0][0]*a[2][2]-a[0][2]*a[2][0];
b[1][2]=-1*(a[0][0]*a[2][1]-a[0][1]*a[2][0]);
b [2][0] =a[0][1]*a[1][2]-a[0][2]*a[1][1];
b[2][1]=-1*(a[0][0]*a[1][2]-a[0][2]*a[1][0]);
b[2][2]=a[0][0]*a[1][1]-a[0][1]*a[1][0];
long int det=a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2])-
a[0][1]*(a[1][0]*a[2][2]-a[2][0]*a[1][2])+a[0][2]*(a[1][0]*a[2][1]-
a[2][0]*a[1][1]);
//for adjoint
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=b[j][i];
}
//for inverse
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=c[i][j]/det;
}
if(det!=0)
{
cout<<"inverse of matrix is:";
//for displaying output
for(i=0;i<3;i++)
{
cout<<endl<<endl<<endl;
for(j=0;j<3;j++)
cout<<c[i][j]<<" ";
}}
else
cout<<"inverse is not possible";
getch();
}
Output
Program#05
*//Newton method//*
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#define f(x) (x*x*x-2*x-5)
#define F(x) (3*x*x-2)
float main()
{
clrscr();
int n,i=0;
float a,b,t,diff,x0,x;
cout<<"enter the initial value=";
cin>>a;
cout<<"enter the final value=";
cin>>b;
cout<<"enter the no. of iterations=";
cin>>n;
cout<<"enter the tolerance=";
cin>>t;
if(f(a)*f(b)>0)
{
cout<<"root does not lie between"<<a<<"and"<<b<<endl;
cout<<"please enter another interval=";
}
if(f(a)>f(b))
x0=a;
else
x0=b;
do
{
x=x0-(f(x0)/F(x0));
i++;
diff=fabs((x-x0)/2.0);
x0=x;
{
cout<<"Root="<<x<<endl;
}}
while((i<n)&&(f(x)!=0)&&(diff>t));
getch();
return(x);
}
Output
*//Simpson method//*
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x)
void main()
{
clrscr();
int interval,a,b,x,k;
float sum,h,xk,area;
cout<<"enter the initial vlue=";
cin>>a;
cout<<"enter the final value=";
cin>>b;
cout<<"enter the interval =";
cin>>interval;
h=(b-a)/interval;
sum=f(a)+f(b);
for(k=1;k<interval;k++)
{
xk = a+k*h;
if(k%2==0)
sum=sum+2*f(xk);
else
sum=sum+4*f(xk);
}
area=sum*h/3;
{
cout<<"Answer="<<area<<endl;
}
getch();
}
Output
*//Trapezoidal method//*
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x)
void main()
{
clrscr();
int interval,a,b,x,k;
float sum,h,xk,area;
cout<<"enter the initial vlue=";
cin>>a;
cout<<"enter the final value=";
cin>>b;
cout<<"enter the interval =";
cin>>interval;
h=(b-a)/interval;
sum=f(a)+f(b);
for(k=1;k<interval;k++)
{
xk = a+k*h;
sum=sum+2*f(xk);
}
area=sum*h/2;
{
cout<<"Answer="<<area<<endl;
}
getch();
}
Output
Program#08
*//Bisection method//*
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) (sin(x)-5*x+2)
float main()
{
clrscr();
int n,i=0;
float a,b,t,c,interval;
cout<<"enter initial value=";
cin>>a;
cout<<"enter final value=";
cin>>b;
cout<<"enter the no. of iterations=";
cin>>n;
cout<<"enter tolerance=";
cin>>t;
if(f(a)*f(b)<0)
do
{
c=(a+b)/2.0;
if(f(a)*f(c)<0)
b=c;
else
a=c;
i++;
interval=fabs((b-a)/2.0);
cout<<"Answer="<<c<<endl;
}
while((i<n)&&(f(c)!=0)&&(interval>t));
getch();
return(c);
}
Output
Program#09
Output
enter the initial value=0.4
enter the final value=0.6
enter the no. of iterations=20
enter tolerance=0.0005
Answer=0.495008
Program#10
*//secant method//*
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) (sin(x)-5*x+2)
float main()
{
clrscr();
int n,i=0;
float a,b,t,c,interval;
cout<<"enter initial value=";
cin>>a;
cout<<"enter final value=";
cin>>b;
cout<<"enter number of itrations=";
cin>>n;
cout<<"enter tolerance=";
cin>>t;
do
{
c=a-f(a)*(b-a)/(f(b)-f(a));
i++;
interval=fabs((b-a)/2.0);
a=b;
b=c;
{
cout<<"Answer="<<c<<endl;
}}
while((i<n)&&(f(c)!=0)&&(interval>t));
getch();
return(c);
}
Output
Program#11
*//Jacobi method//
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,j,m,n,itt;
float x[10],a[10][10],b[10],c[10];
cout<<"enter no. of equations to be solved=";
cin>>n;
cout<<"enter no. of iterations=";
cin>>itt;
cout<<"enter the constants terms of equations=";
for(i=0;i<n;i++)
{
cin>>b[i];
}
cout<<"enter the coefficients of equations=";
for(i=0;i<n;i++)
{
x[i]=0;
for(j=0;j<n;j++)
cin>>a[i][j];
}
m=1;
line:
for(i=0;i<n;i++)
{
c[i]=b[i];
for(j=0;j<n;j++)
{
if(i!=j)
{
c[i]=c[i]-a[i][j]*x[j];
}}}
for(i=0;i<n;i++)
{
x[i]=c[i]/a[i][i];
}
m++;
if(m<=itt)
{
goto line;
}
else
{
cout<<"the solution of equations is:"<<endl;
for(i=0;i<n;i++)
cout<<"\n""x("<<i<<")="<<x[i]<<endl;
}
getch();
}
Output
Program#12
Output
Output
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
0.5 0.5 0
0.5 0 0.5
0 0.5 0.5
0.5 0.5 1
0.5 1 0.5
1 0.5 0.5
Program#14
Output
enter the number=1
0 0 0
0 0 4.09
0 4.09 0
0 4.09 4.09
4.09 0 0
4.09 0 4.09
4.09 4.09 0
4.09 4.09 4.09
0 2.045 2.045
2.045 0 2.045
1.825 1.825 0
3.65 1.825 1.825
1.825 3.65 1.825
1.825 1.825 3.65
Program#15
Output
Enter the number=1
0 0 0
0 0 4.09
0 4.09 0
0 4.09 4.09
4.09 0 0
4.09 0 4.09
4.09 4.09 0
4.09 4.09 4.09
0 1.825 1.825
1.825 0 1.825
1.825 1.825 0
3.65 1.825 1.825
1.825 3.65 1.825
1.825 1.825 3.65
Program#16
Output
enter the number=1
0 0 0
0 0 3.65
0 4.09 0
0 4.09 4.09
4.09 0 0
4.09 0 4.09
4.09 4.09 0
4.09 4.09 4.09
0 1.825 1.825
1.825 0 1.825
1.825 1.825 0
3.65 1.825 1.825
1.825 3.65 1.825
1.825 1.825 3.65
Program#17