0% found this document useful (0 votes)
2 views139 pages

Computational Programes

The document contains a series of C++ programs designed for an MSc in Computational Physics, covering various topics such as basic arithmetic operations, loops, and selection statements. It includes examples for calculating sums, averages, areas, and kinetic energy, as well as demonstrating the use of for and while loops. Additionally, it explains data types like int, float, and char, and provides examples of conditional statements.

Uploaded by

Usama Zia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views139 pages

Computational Programes

The document contains a series of C++ programs designed for an MSc in Computational Physics, covering various topics such as basic arithmetic operations, loops, and selection statements. It includes examples for calculating sums, averages, areas, and kinetic energy, as well as demonstrating the use of for and while loops. Additionally, it explains data types like int, float, and char, and provides examples of conditional statements.

Uploaded by

Usama Zia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 139

Computational Physics

MSc Part-II
➢ Programs in C++;
1: To calculate sum of two numbers
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

inta,b,c;

a=5;

b=7;

c=a+b;

cout<<" a= "<<a<<endl;

cout<<" b= "<<b<<endl;

cout<<" c= "<<c<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 1


2: To calculate sum of two numbers taking the input from
user
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

inta,b,c;

cout<<" Enter a= ";

cin>>a;

cout<<" Enter b= ";

cin>>b;

c=a+b;

cout<<"\n c= "<<c;

cout<<"\n Thanks "<<endl;

getch (); }

3: To calculate sum of three numbers


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 2


inta,b,c,d;

cout<<" Enter a= ";

cin>>a;

cout<<" Enter b= ";

cin>>b;

cout<<" Enter c= ";

cin>>c;

d=a+b+c;

cout<<"\n d= "<<d<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

4: To calculate sum and average of two numbers


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

floata,b,c,avg;

cout<<" Enter a= ";

cin>>a;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 3


cout<<" Enter b= ";

cin>>b;

c=a+b;

cout<<" c= "<<c;

avg=(a+b)/2;

cout<<"\n avg= "<<avg;

cout<<"\n Thanks "<<endl;

getch(); }

5: To calculate area & circumference of a circle


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

floatr,A,C;

float pi=3.14;

cout<<" Enter r= ";

cin>>r;

A=pi*r*r;

cout<<"\n A= "<<A;

C=2*pi*r;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 4


cout<<"\n C= "<<C;

cout<<"\n Thanks "<<endl;

getch(); }

6: To calculate Area of Ellipse


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

floata,b,A;

float pi=3.14;

cout<<" Enter a= ";

cin>>a;

cout<<" Enter b= ";

cin>>b;

A=pi*a*b;

cout<<" Area of Ellipse= "<<A<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

7: To calculate kinetic Energy

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 5


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

floatm,v,KE;

cout<<" Enter m= ";

cin>>m;

cout<<" Enter v= ";

cin>>v;

KE=(m*v*v)/2;

cout<<" Kinetic Energy= "<<KE<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

8: To calculate hero formula


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

floata,b,c,A,s;

cout<<" Enter a= ";

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 6


cin>>a;

cout<<" Enter b= ";

cin>>b;

cout<<" Enter c= ";

cin>>c;

s=(a+b+c)/2;

A=sqrt(s*(s+a)*(s+b)*(s+c));

cout<<"\n A= "<<A<<endl;

cout<<"\n Thanks "<<endl;

getch() ; }

❖ Repetition:- (Loop)
There are two types of repetition(loop):

1).For loop:
To repeat for n times.

2).While loop:
To repeat while condition is true.

“FOR-LOOP”
9: To print out 'welcome' for 5 times (Hint= using for loop)
#include<iostream.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 7


#include<conio.h>

#include<math.h>

void main() {

for(int i=1; i<=5; i++)

cout<<" Welcome "<<endl;

cout<<"\n Thanks "<<endl;

getch() ; }

10: To find out even numbers from 1 to 15


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

for(int i=2; i<=15; i=i+2)

cout<<i<<" ";

cout<<"\n Thanks "<<endl;

getch(); }

11: To print out odd numbers from 1 to 15


#include<iostream.h>

#include<conio.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 8


#include<math.h>

void main() {

for(int i=1; i<=15; i=i+2)

cout<<i<<" ";

cout<<"\n Thanks "<<endl;

getch(); }

12: To find out even numbers from 1 to 10 and their sum


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int s=0;

for(int i=2; i<=10; i=i+2){

cout<<i<<" ";

s=s+i; }

cout<<" sum= "<<s<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

13: To find out odd numbers from 1 to 15 and their sum


#include<iostream.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 9


#include<conio.h>

#include<math.h>

void main() {

int s=0;

for(int i=1; i<=15; i=i+2) {

cout<<i<<" ";

s=s+i; }

cout<<"\n sum= "<<s<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

14: To print out table of 5


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int n=5;

for(int i=1; i<=10; i++)

cout<<n<<" x "<<i<<" = "<<(n*i)<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 10


15: To print out table of any number
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int n;

cout<<" Enter n= ";

cin>>n;

for(int i=1; i<=10; i++)

cout<<"\n"<<n<<" x "<<i<<" = "<<(n*i)<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

16: To calculate sum & average of 1st n integers


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

floatn,avg;

float s=0;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 11


cout<<" Enter n ";

cin>>n;

for(int i=1; i<=n; i++) {

cout<<i<<" ";

s=s+i; }

avg=s/n;

cout<<"\n sum= "<<s<<endl;

cout<<"\n avg= "<<avg<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

17: To calculate the factorial of a number taking input from


user
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float n;

float p=1;

cout<<" Enter n= ";

cin>>n;

for(int i=1; i<=n; i++) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 12


cout<<i<<" ";

p=p*i; }

cout<<"\n Factorial= "<<p<<endl;

cout<<"\n Thanks "<<endl;

getch (); }

18: To calculate Kinetic Energy for different values of m & v


taking from user
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

floatm,v,KE;

cout<<"\n m\tv\tKE ";

for(int i=1; i<=3; i++) {

cout<<"\n Enter m,v ";

cin>>m>>v;

KE=(m*v*v)/2;

cout<<m<<"\t"<<v<<"\t"<<KE<<endl; }

cout<<"\n Thanks "<<endl;

getch (); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 13


19: To calculate area for different values of r taking from
user
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

floatr,A;

float pi=3.14;

cout<<" r\tA "<<endl;

for(int i=1; i<=3; i++) {

cout<<"\n Enter r= "; cin>>r;

A=pi*r*r;

cout<<r<<"\t"<<A<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

20: To calculate area & circumference for different values of


r (if not taking from user)
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 14


floatr,A,C;

float pi=3.14;

cout<<" r\tA\tC "<<endl;

for(int i=1; i<=6; i++) {

r=i;

A=pi*r*r;

C=2*pi*r;

cout<<r<<"\t"<<A<<"\t"<<C<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

21: To calculate current for different values of V & R


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float V,R,I;

cout<<" V\tR\tI "<<endl;

for(int i=1; i<=4; i++) {

cout<<"\n Enter V= ";

cin>>V;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 15


cout<<"\n Enter R= ";

cin>>R;

I=V/R;

cout<<V<<"\t"<<R<<"\t"<<I<<endl; }

cout<<"\n Thanks "<<endl;

getch();}

Q:Difference between int,float and char?


1.int:-

An int variable is used to store an integer, as name suggested. It is a number


without decimal point.

2.float:-

It is used to store decimal numbers (numbers with floating point


value) with single precision. It is a number that has decimal place.

3.char:-

The most basic data type in computer programing. It stores a single character &
requires a single byte of memory in almost all compilers.

❖ Selection:
Selection is to select(skip) due to condition.

There are three types of selection:

i). If selection ii). If-else selection iii). If-else-If selection

“IF-Selection”

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 16


22: To compare two numbers
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int x,y;

cout<<" Enter x= "; cin>>x;

cout<<" Enter y= "; cin>>y;

if(x>y)

cout<<" x is greater than y ";

if(y>x)

cout<<" y is greater than x ";

cout<<"\n Thanks "<<endl;

getch();}

23: To compare two numbers


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int x,y;

cout<<" Enter x= "; cin>>x;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 17


cout<<" Enter y= "; cin>>y;

cout<<" x= "<<x<<" y= "<<y<<endl;

if(x>y)

cout<<" x is greater than y "<<endl;

if(x<y)

cout<<" y is greater than x "<<endl;

if(x==y)

cout<<" x is equal to y "<<endl;

if(x!=y)

cout<<" x is not equal to y "<<endl;

if(x<=y)

cout<<" x is less than or equal to y "<<endl;

if(x>=y)

cout<<" x is equal to or greater than y "<<endl;

cout<<"\n Thanks "<<endl;

getch();}

24: To find out maximum out of n numbers (taking input


from user)
#include<iostream.h>

#include<conio.h>

#include<math.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 18


void main() {

int n;

int k;

cout<<" Enter n "; cin>>n;

int max=-1;

for(int i=1; i<=n; i++) {

cout<<" Enter k= "; cin>>k;

if(k>max)

max=k;

cout<<k<<endl; }

cout<<" max= "<<max<<endl;

cout<<"\n Thanks "<<endl;

getch();}

25: To find out minimum out of n numbers (taking input


from user)

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 19


int n;

int k;

cout<<" Enter n "; cin>>n;

int min=100;

for(int i=1; i<=n; i++) {

cout<<" Enter k= "; cin>>k;

if(k<min)

min=k;

cout<<k<<endl; }

cout<<" minimum= "<<min<<endl;

cout<<"\n Thanks "<<endl;

getch();}

26: To find out maximum as well as minimum out of n


numbers (taking input from user)
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int n;

int k;

cout<<" Enter n "; cin>>n;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 20


int min=100;

int max=-1;

for(int i=1; i<=n; i++) {

cout<<" Enter k "; cin>>k;

if(k>max)

max=k;

if(k<min)

min=k;

cout<<k<<endl; }

cout<<" maximum= "<<max<<endl;

cout<<" minimum= "<<min<<endl;

cout<<"\n Thanks "<<endl;

getch();}

27: To find out maximum out of three numbers


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

int a,b,c,max;

cout<<" enter a= "; cin>>a;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 21


cout<<" enter b= "; cin>>b;

cout<<" enter c= "; cin>>c;

max=-1;

if(a>max)

max=a;

if(b>max)

max=b;

if(c>max)

max=c;

cout<<" Maximum number= "<<max<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

28: To find out minimum out of three numbers


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

int a,b,c,min;

cout<<" enter a= "; cin>>a;

cout<<" enter b= "; cin>>b;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 22


cout<<" enter c= "; cin>>c;

min=100;

if(a<min)

min=a;

if(b<min)

min=b;

if(c<min)

min=c;

cout<<" Minimum number= "<<min<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

29: To find out minimum out of 4 numbers


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

int a,b,c,d,min;

cout<<" enter a= "; cin>>a;

cout<<" enter b= "; cin>>b;

cout<<" enter c= "; cin>>c;

cout<<" enter d= "; cin>>d;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 23


min=100;

if(a<min)

min=a;

if(b<min)

min=b;

if(c<min)

min=c;

if(d<min)

min=d;

cout<<" Minimum number= "<<min<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

30: To find out maximum out of 4 numbers

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

int a,b,c,d,max;

cout<<" enter a= "; cin>>a;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 24


cout<<" enter b= "; cin>>b;

cout<<" enter c= "; cin>>c;

cout<<" enter d= "; cin>>d;

max=-1;

if(a>max)

max=a;

if(b>max)

max=b;

if(c>max)

max=c;

if(d>max)

max=d;

cout<<" Maximum number= "<<max<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

31: write a progarm for "D=b square-4ac" for D>0,D<0 & D=0
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 25


float a,b,c,D;

cout<<" enter a= "; cin>>a;

cout<<" enter b= "; cin>>b;

cout<<" enter c= "; cin>>c;

D=b*b-4*a*c;

if(D>0)

cout<<" Roots are real "<<endl;

else if(D<0)

cout<<" Roots are imaginary "<<endl;

else

cout<<" Roots are equal "<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

32. To calculate k=5λ2


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

float k,lambda;

cout<<" enter lambda= ";

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 26


cin>>lambda;

k=5*lambda*lambda;

cout<<k<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

33: To calculate “ z=x2+y2/x+y “ x=rcosϴ & y=rsinϴ


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

float x,y,z,r,theta;

cout<<" enter r= ";

cin>>r;

cout<<" enter theta= ";

cin>>theta;

x=r*cos(theta);

y=r*sin(theta);

z=(x*x+y*y)/x+y;

cout<<" z= "<<z<<endl;

cout<<"\n Thanks "<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 27


getch(); }

34: To print out number divisible by 3.


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float n;

cout<<" Enter n= ";

cin>>n;

for(int i=1; i<=n; i++)

if(i%3==0)

cout<<" i= "<<i<<endl;

cout<<"\n Thanks "<<endl;

getch() ; }

35: To print out number divisible by 9.


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 28


float n;

cout<<" Enter n= ";

cin>>n;

for(int i=1; i<=n; i++)

if(i%9==0)

cout<<" i= "<<i<<endl;

cout<<"\n Thanks "<<endl;

getch() ; }

36: To print out number divisible by 3 as well as 7.


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float n;

cout<<" Enter n= ";

cin>>n;

for(int i=1; i<=n; i++)

if(i%3==0 && i%7==0)

cout<<" i= "<<i<<endl;

cout<<"\n Thanks "<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 29


getch() ; }

37: To calculate and print x against f(x) valuse


such that
f(x)=x3+2x2-4x-25 ; 1≤x≤5
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

int x,y;

cout<<" x\tf(x)\n ";

for(int i=1; i<=5; i++) {

x=i;

y=pow(x,3)*2*x*x-4*x-25;

cout<<x<<"\t"<<y<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

38. To calculate and print x against f(x) valuse taking input


from user
such that

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 30


f(x)=x3+2x2-4x-25 ; 1≤x≤5
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

int x,y;

cout<<" x\tf(x)\n ";

for(int i=1; i<=5; i++) {

cout<<" enter x= "; cin>>x;

y=pow(x,3)*2*x*x-4*x-25;

cout<<x<<"\t"<<y<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

39: To calculate and print x against f(x) valuse


such that
f(x)=x3+2x2-4x-25 ; For even values of x from 1
through 10
#include<iostream.h>

#include<conio.h>

#include<math.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 31


void main (){

int x,y;

cout<<" x\tf(x)\n ";

for(int i=2; i<=10; i=i+2) {

x=i;

y=pow(x,3)*2*x*x-4*x-25;

cout<<x<<"\t"<<y<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

40: To calculate and print x against f(x) valuse


such that
f(x)=x3+2x2-25 ; For odd values of x from 1 through 15
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

int x,y;

cout<<" x\tf(x)\n ";

for(int i=1; i<=15; i=i+2) {

x=i;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 32


y=pow(x,3)*2*x*x-25;

cout<<x<<"\t"<<y<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

41: To calculate and print x against f(x) valuse


such that
f(x)=x3+2x2-4x-25 ; -5≤x≤5
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main (){

int x,y;

cout<<" x\tf(x)\n ";

for(int i=-5; i<=5; i++) {

x=i;

y=pow(x,3)*2*x*x-4*x-25;

cout<<x<<"\t"<<y<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 33


42: To calculate and print x against f(x) valuse taking input
from user
such that
f(x)=x3+2x2-4x-25 ; 1≤x≤5
& also find out maximum of f(x).
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int x,y,max;

max=-100;

cout<<" x\tf(x)\n ";

for(int i=1; i<=5; i++) {

cout<<" enter x= "; cin>>x;

y=pow(x,3)*2*x*x-4*x-25;

cout<<x<<"\t"<<y<<endl;

if(y>max)

max=y; }

cout<<" Maximum of f(x)= "<<max<<endl;

cout<<"\n Thanks "<<endl;

getch() ; }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 34


43: To calculate and print x against f(x) valuse taking input
from user
such that
f(x)=x3+2x2-4x-25 ;
& also find out minimum of f(x).
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int x,y,min;

min=1000000;

cout<<" x\tf(x)\n ";

for(int i=1; i<=5; i++) {

cout<<" enter x= "; cin>>x;

y=pow(x,3)*2*x*x-4*x-25;

cout<<x<<"\t"<<y<<endl;

if(y<min)

min=y; }

cout<<" Minimum of f(x)= "<<min<<endl;

cout<<"\n Thanks "<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 35


getch() ; }

44: Step function

𝒙𝟑 + 𝟐𝒙𝟐 − 𝟒𝒙 − 𝟐𝟓 𝒊𝒇 𝒙 ≥ 𝟎
𝒇(𝒙) = { 𝟐
𝒙 −𝟏 𝒊𝒇 𝒙 < 0
Such that -15≤x≤15
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int x,y;

cout<<" x\tf(x)\n ";

for(int i=1; i<=15; i++) {

x=i;

if(x>0)

y=pow(x,3)+pow(x,2)-4*x-25;

else

y=x*x-1;

cout<<x<<"\t"<<y<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 36


45: To calculate and print factorial of 1st 10 numbers
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int p=1;

cout<<" Number\tFactorial\n ";

for(int i=1; i<=10; i++) {

p=p*i;

cout<<i<<"\t"<<p<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

❖ Array:-
Collection of same consecutive memory location form an array.

➢ The advantages of Array:-

1) Arrays can store a large number of values with a single name.

2) The values stored in the array can be sorted easily.

3) A search process can be easily applied over the arrays.

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 37


The syntax of declaring an array is as follow:
Data Type Array Name[Length]

✓ Data Type: indicate the data type such as int,float & char.
✓ Array Name: indicates the name of the array.
✓ Length: indicates the size of array which must be constant.

For Example:

Int a[5] OR int a[5]={2,5,9,1,7}

2 5 9 1 7
a[0] a[1] a[2] a[3] a[4]

46: To calculate b=2a by using array of size 5


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int a[5],b[5];

cout<<" Enter a= ";

for(int i=0; i<5; i++) {

cin>>a[i];

b[i]=2*a[i]; }

cout<<" a\tb\n ";

for(int i=0; i<5; i++)

cout<<a[i]<<"\t"<<b[i]<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 38


cout<<"\n Thanks "<<endl;

getch(); }

47: To calculate c=a+b by using array of size 7


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int a[7],b[7],c[7];

cout<<" Enter a= ";

for(int i=0; i<7; i++)

cin>>a[i];

cout<<" Enter b= ";

for(int i=0; i<7; i++){

cin>>b[i];

c[i]=a[i]+b[i]; }

cout<<" a\tb\tc\n ";

for(int i=0; i<7; i++)

cout<<a[i]<<"\t"<<b[i]<<"\t"<<c[i]<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 39


48: To calculate “A=πr2 & C=2πr” by using array of size 4
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float r[4],A[4],C[4];

cout<<" Enter r= ";

for(int i=0; i<4; i++){

cin>>r[i];

A[i]=3.14*r[i]*r[i];

C[i]=2*3.14*r[i]; }

cout<<" r\tA\tC\n ";

for(int i=0; i<4; i++)

cout<<r[i]<<"\t"<<A[i]<<"\t"<<C[i]<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

49: To calculate the dot product of two vectors. i.e.

𝑺 = ∑𝟑𝒊=𝟏 𝒂ibi
#include<iostream.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 40


#include<conio.h>

#include<math.h>

void main() {

float a[3],b[3],s[3];

float sum=0;

cout<<" Enter components of A= ";

for(int i=0; i<3; i++)

cin>>a[i];

cout<<" Enter components of B= ";

for(int i=0; i<3; i++)

cin>>b[i];

for(int i=0; i<3; i++) {

s[i]=a[i]*b[i];

sum=sum+s[i]; }

cout<<" The dot product= "<<sum<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

50: To calculate the dot product of two vectors. i.e.


𝑺 = ∑𝟑𝒊=𝟏 𝒂ibi
and also calculate average.

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 41


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float a[3],b[3],s[3];

float sum=0,avg;

cout<<" Enter components of A= ";

for(int i=0; i<3; i++)

cin>>a[i];

cout<<" Enter components of B= ";

for(int i=0; i<3; i++)

cin>>b[i];

for(int i=0; i<3; i++) {

s[i]=a[i]*b[i];

sum=sum+s[i]; }

avg=sum/3;

cout<<" The dot product= "<<sum<<endl;

cout<<" Average= "<<avg<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 42


51: To calculate the dot product of two vectors. i.e.
𝑺 = ∑𝟑𝒊=𝟏 𝒂ibi
and also find average,maximum number in array and its
position by using array of size 3.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float a[3],b[3],s[3];

float sum=0,avg,max,i;

cout<<" Enter components of A= ";

for(int i=0; i<3; i++)

cin>>a[i];

cout<<" Enter components of B= ";

for(int i=0; i<3; i++)

cin>>b[i];

for(int i=0; i<3; i++) {

s[i]=a[i]*b[i];

sum=sum+s[i]; }

avg=sum/3;

max=s[0];

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 43


for(int i=0; i<3; i++)

if(s[i]>max)

max=s[i];

cout<<" The dot product= "<<sum<<endl;

cout<<" Average= "<<avg<<endl;

cout<<" The max position= "<<(i+1)<<endl;

cout<<" Maximum S= "<<max<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

52: To find out maximum voltage and minimum voltage for


5 resistance.Also find avg voltage by using array
Note: V=IR

V[i]=I[i]*R[i]

For a series network, I is same through each resistance.So

V[i]=I*R[i]

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float V[5],R[5],I;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 44


float sum=0,avg,max;

cout<<" Enter I= ";

cin>>I;

cout<<" Enter R= ";

for(int i=0; i<5; i++) {

cin>>R[i];

V[i]=I*R[i]; }

cout<<" I \tR \t V \n "<<endl;

for(int i=0; i<5; i++)

cout<<I<<" \t "<<R[i]<<" \t "<<V[i]<<endl;

max=V[0];

for(int i=0; i<5; i++)

if(V[i]>max)

max=V[i];

float min=V[0];

for(int i=0; i<5; i++)

if(V[i]<min)

min=V[i];

for(int i=0; i<5; i++)

sum=sum+V[i];

avg=sum/5;

cout<<" Maximum voltage= "<<max<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 45


cout<<" Minimum voltage= "<<min<<endl;

cout<<" Average voltage= "<<avg<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

53: Analyzing Temprature


Temprature out side the room:

A 65 62 65 54 60
Tempertaure inside the room:

B 60 53 65 62 63
1: Average temperature per day C=(A+B)/2

2: Average temperature for week

3: Average temp. inside room / week

4: Average temp. outside room / week

5: Location of day when A>B

6: Find out no.of days and corresponding day when A>B

7: Find out when A=B

8: Find out day when A>60

9: Find out maximum of A

10: Find out minimum of A


#include<iostream.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 46


#include<conio.h>

#include<math.h>

void main() {

float C[5]; int i=5,cn=0;

float A[5]={65,62,65,54,60};

float B[5]={60,53,65,62,63};

for(int i=0; i<5; i++) {

C[i]=(A[i]+B[i])/2;

cout<<" A= "<<A[i]<<"\t"<<endl; }

for(int i=0; i<5; i++)

cout<<" B= "<<B[i]<<"\t"<<endl;

cout<<" C= "<<C[i]<<"\t"<<endl;

float sc=0;

for(int i=0; i<5; i++)

sc=sc+C[i];

cout<<" Average temp. per week= "<<(sc/5)<<endl;

float sA=0;

for(int i=0; i<5; i++)

sA=sA+A[i];

cout<<" Average temp. inside room per week= "<<(sA/5)<<endl;

float sB=0;

for(int i=0; i<5; i++)

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 47


sB=sB+B[i];

cout<<" Average temp. outside room per week= "<<(sB/5)<<endl;

for(int i=0; i<5; i++)

if(A[i]>B[i])

cout<<" Location of day when A>B= "<<(i+1)<<endl;

for(int i=0; i<5; i++)

if(A[i]>B[i])

cn=cn+1;

cout<<" No.of days when A>B= "<<cn<<endl;

for(int i=0; i<5; i++)

if(A[i]==B[i])

cout<<" No.of days when A=B = "<<(i+1)<<endl;

for(int i=0; i<5; i++)

if(A[i]>60)

cout<<" No.of days when A>60 = "<<(i+1)<<endl;

float maxA=A[i];

for(int i=0; i<5; i++)

if(A[i]>maxA)

maxA=A[i];

cout<<" Maximum of A = "<<maxA<<endl;

float minA=A[i];

for(int i=0; i<5; i++)

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 48


if(A[i]<minA)

minA=A[i];

cout<<" Minimum of A = "<<minA<<endl;

cout<<" Thanks "<<endl;

getch(); }

54: Find out minimum in the array and its location


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float a[5];

cout<<" Enter a= ";

for(int i=0; i<5; i++)

cin>>a[i];

float max=a[0]; float j=0;

for(int i=0; i<5; i++)

if(a[i]>max) {

max=a[i];

j=i; }

cout<<" Maximum of array = "<<max<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 49


cout<<" Location of max = "<<(j+1)<<endl;

cout<<" Thanks "<<endl;

getch(); }

❖ Multidimensional Array:-
Consecutive memory location having m rows
and n coloumns.

Multidimensional array is declare as:


a[ ][ ]
Array name Rows Coloumns

To input an array use two for loops as:

for(int m=0; m<5; m++)

for(int n=0; n<5; n++)

55: To input & print values in multidimensional array.


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float a[3][3];

cout<<" Enter matrix entries row wise= ";

for(int m=0; m<3; m++)

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 50


for(int n=0; n<3; n++)

cin>>a[m][n];

cout<<" The input matrix is as= "<<endl;

for(int m=0; m<3; m++){

for(int n=0; n<3; n++)

cout<<a[m][n]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch() ; }

❖ Random Numbers:-
A number without any order and sequence.

e.g.: 37,27,30,12,54,9,15

➢ Command for random number:


#include<stdlib.h>
stdlib Standard library
➢ Function:
rand( )
➢ Formula to generate random numbers:
n = a + rand( ) % b;
Where; a scaling factor
b shifting factor
✓ The random seed is initialized to a value representing the
current

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 51


time(calling time) to generate a different value every time the program
is run.

For this we use “ #include<time.h> “ as a command and function is

“ srand(time(0)) “.

56: To generate 20 random numbers in the range of 1 to 10.


#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<time.h>

void main (){

int n;

srand(time(0));

for(int i=1; i<=20;i++){

n=1+rand()%10;

cout<<n<<" "; }

cout<<"\n Thanks "<<endl;

getch(); }

57. To simulate dice for 100 roles.


#include<iostream.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 52


#include<conio.h>

#include<stdlib.h>

#include<time.h>

void main (){

int n;

srand(time(0));

for(int i=1; i<=100;i++){

n=1+rand()%6;

cout<<n<<" "; }

cout<<"\n Thanks "<<endl;

getch(); }

58: To simulate dice for 20 roles & calculate sum.


#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<time.h>

void main (){

int n;

int s=0;

srand(time(0));

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 53


for(int i=1; i<=20;i++){

n=1+rand()%6;

cout<<n<<" ";

s=s+n; }

cout<<"\n Sum= "<<s<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

59: To simulate dice for 20 roles & calculate frequency of


each face & caculate total score.
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<time.h>

void main (){

int n,score;

int f1=0,f2=0,f3=0,f4=0,f5=0,f6=0;

srand(time(0));

for(int i=1; i<=20;i++){

n=1+rand()%6;

cout<<n<<" ";

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 54


if(n==1) f1=f1+1;

if(n==2) f2=f2+1;

if(n==3) f3=f3+1;

if(n==4) f4=f4+1;

if(n==5) f5=f5+1;

if(n==6) f6=f6+1; }

cout<<"\n Face 1= "<<f1<<endl;

cout<<" Face 2= "<<f2<<endl;

cout<<" Face 3= "<<f3<<endl;

cout<<" Face 4= "<<f4<<endl;

cout<<" Face 5= "<<f5<<endl;

cout<<" Face 6= "<<f6<<endl;

score=1*f1+2*f2+3*f3+4*f4+5*f5+6*f6;

cout<<"\n Total score= "<<score<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

60:
#include<iostream.h>

#include<conio.h>

#include<math.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 55


#include<stdlib.h>

#include<time.h>

void main (){

float x1,y1,x2,y2,x,y,d,s=0;

float xa[1000],ya[1000];

int ind=0;

srand(time(0));

cout<<"\n (x , y) \n";

for(int i=0; i<20; i++) {

x=1+rand()%10;

y=1+rand()%10;

xa[i]=x;

ya[i]=y;

cout<<" ( "<<x<<" , "<<y<<" ) "<<endl;

ind=ind+1; }

x1=xa[0];

y1=ya[0];

for(int i=1; i<ind; i++) {

x2=xa[i];

y2=ya[i];

d = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );

cout<<"\n d= "<<d<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 56


x1=x2;

y1=y2;

s=s+d; }

cout<<" Total distance= "<<s<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

❖ Some other programs of Multidimensional Array:

61: To find out B=2*A


➢ In Multidimensional array: “ B[i][j]=2*A[i][j] “
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3],B[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 57


for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

B[i][j]=2*A[i][j];

cout<<"\n B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<B[i][j]<<" ";

cout<<endl; }

getch(); }

62: Print out 4x4 matrix of A.


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[4][4];

cout<<" Enter A= ";

for(int i=0; i<4; i++)

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 58


for(int j=0; j<4; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<4; i++) {

for(int j=0; j<4; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

63: Print out matrix of A & find B= transpose of A.


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3],B[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 59


for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

B[i][j]=A[j][i];

cout<<"\n B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<B[i][j]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

64: Find out C=A+B


➢ In Multidimensional array: “ C[i][j]=A[i][j]+B[i][j] “
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 60


int A[3][3],B[3][3],C[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

cout<<"\n Enter B= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>B[i][j];

cout<<"\n B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<B[i][j]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

C[i][j]=A[i][j]+B[i][j];

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 61


cout<<"\n C= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<C[i][j]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

65: Find out C=A+Transpose of B.


➢ In Multidimensional array: “ C[i][j]=A[i][j]+B[j][i] “
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3],B[3][3],C[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 62


for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

cout<<"\n Enter B= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>B[i][j];

cout<<"\n B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<B[i][j]<<" ";

cout<<endl; }

cout<<"\n Transpose of B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<B[j][i]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

C[i][j]=A[i][j]+B[j][i];

cout<<"\n C= \n";

for(int i=0; i<3; i++) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 63


for(int j=0; j<3; j++)

cout<<C[i][j]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

66: Find out C=4A+7B


➢ In Multidimensional array: “ C[i][j]=4*A[i][j]+7*B[i][j] “
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3],B[3][3],C[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 64


cout<<endl; }

cout<<"\n Enter B= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>B[i][j];

cout<<"\n B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<B[i][j]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

C[i][j]=4*A[i][j]+7*B[i][j];

cout<<"\n C= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<C[i][j]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

67: Find out C=(4A+7B)/7.

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 65


➢ In Multidimensional array: “ C[i][j]=(4*A[i][j]+7*B[i][j])/7 “
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3],B[3][3],C[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

cout<<"\n Enter B= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>B[i][j];

cout<<"\n B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 66


cout<<B[i][j]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

C[i][j]=(4*A[i][j]+7*B[i][j])/7;

cout<<"\n C= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<C[i][j]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

68: Find out C=A+3* transpose of B+7.


➢ In Multidimensional array: “ C[i][j]=A[i][j]+3*B[j][i]+7 “
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3],B[3][3],C[3][3];

cout<<" Enter A= ";

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 67


for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

cout<<"\n Enter B= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>B[i][j];

cout<<"\n B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<B[i][j]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

C[i][j]=A[i][j]+3*B[j][i]+7;

cout<<"\n C= \n";

for(int i=0; i<3; i++) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 68


for(int j=0; j<3; j++)

cout<<C[i][j]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

69: Find out max element of matrix A.


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 69


int max=A[0][0];

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

if(A[i][j]>max)

max=A[i][j];

cout<<" Maximum element of matrix A= "<<max<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

70: Find out minimum element of matrix A.


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 70


for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

int min=A[0][0];

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

if(A[i][j]<min)

min=A[i][j];

cout<<" Minimum element of matrix A= "<<min<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

71: Find out sum of all elements of matrix A & also find its
average.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 71


cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

float sum=0,avg;

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

sum=sum+A[i][j];

avg=sum/9;

cout<<" sum of all element of matrix A= "<<sum<<endl;

cout<<" Average of all element of matrix A= "<<avg<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

72: Find out C=A*B.


➢ In Multidimensional array: “ C[i][j]=A[i][j]*B[i][j] “
#include<iostream.h>

#include<conio.h>

#include<math.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 72


void main() {

int A[3][3],B[3][3],C[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

cout<<" Enter B= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>B[i][j];

cout<<"\n B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<B[i][j]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 73


C[i][j]=0;

for(int k=0; k<3; k++)

C[i][j]=C[i][j]+A[i][k]*B[k][j]; }

cout<<"\n C= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<C[i][j]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

73: Find out C=2A*4B


➢ In Multidimensional array: “ C[i][j]=2*A[i][j]*4*B[i][j] “
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int A[3][3],B[3][3],C[3][3],D[3][3];

cout<<" Enter A= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 74


cin>>A[i][j];

cout<<"\n A= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<A[i][j]<<" ";

cout<<endl; }

cout<<" Enter B= ";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cin>>B[i][j];

cout<<"\n B= \n";

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++)

cout<<B[i][j]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++) {

C[i][j]=0;

for(int k=0; k<3; k++)

C[i][j]=C[i][j]+A[i][k]*B[k][j]; }

cout<<"\n C= \n";

for(int i=0; i<3; i++) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 75


for(int j=0; j<3; j++)

cout<<C[i][j]<<" ";

cout<<endl; }

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

D[i][j]=8*C[i][j];

cout<<"\n D= \n";

for(int i=0; i<3; i++){

for(int j=0; j<3; j++)

cout<<D[i][j]<<" ";

cout<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

➢ User Define Function:


A function which is defined by user.

74: To print numbers from 1 through 5 & also find out their
square by using “user define function”.
#include<iostream.h>

#include<conio.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 76


#include<math.h>

int sq(int);

void main (){

int a,b;

for(int i=1; i<=5; i++) {

a=i;

b=sq(a);

cout<<"number="<<a<<"\t sq="<<b<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

int sq(int k) {

return (k*k); }

75: To print numbers from 1 through 5 & also find out their
cube by using “user define function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

int cube(int);

void main (){

int a,b;

for(int i=1; i<=5; i++){

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 77


a=i;

b=cube(a);

cout<<"number="<<a<<"\t cube="<<b<<endl;}

cout<<"\n Thanks "<<endl;

getch();}

int cube(int k){

return (k*k*k); }

76: To print out numbers from 1 through 5 and also


calculate Area & Circumference by using “user define
function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

float area(float);

float cir(float);

void main (){

float r,A,C;

for(int i=1; i<=5; i++){

r=i;

A=area(r);

C=cir(r);

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 78


cout<<"r="<<r<<"\t area="<<A<<"\t cir="<<C<<endl;}

cout<<"\n Thanks "<<endl;

getch();}

float area(float k){

return (3.14*k*k); }

float cir(float j){

return (2*3.14*j); }

77: To find out current by different values of R & V by using


“user define function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

float current(float,float);

void main (){

float I,V,R;

for(int i=1; i<=5; i++){

cout<<" Enter V= "; cin>>V;

cout<<" Enter R= "; cin>>R;

I=current(V,R);

cout<<"V="<<V<<"\t R="<<R<<"\t I="<<I<<endl;

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

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 79


cout<<"\n Thanks "<<endl;

getch();}

float current(float k,float j){

return (k/j); }

78: To calculate kinetic energy for different values of m & v


by using “user define function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

float Kinet(float,float);

void main() {

float m,v,KE;

for(int i=1; i<=4; i++) {

cout<<" Enter m= "; cin>>m;

cout<<" Enter v= "; cin>>v;

KE=Kinet(m,v);

cout<<"m="<<m<<"\tv="<<v<<"\t Kinetic Energy= "<<KE<<endl;

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

cout<<"\n Thanks "<<endl;

getch(); }

float Kinet(float k,float j) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 80


return (k*j*j/2); }

79: To print numbers from 1 through 5 & calculate their sum


by using “user define function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

int sum(int);

void main (){

int a,b;

for(int i=1; i<=5; i++) {

a=i;

b=sum(a);

cout<<"number="<<a<<"\t sum="<<b<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

int sum(int k) {

return (k+k); }

80: To print numbers from 1 through 5 & also find out their
factorial by using “user define function”.
#include<iostream.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 81


#include<conio.h>

#include<math.h>

int factorial(int);

void main() {

int a,b;

for(int i=1; i<=5; i++) {

a=i;

b=factorial(a);

cout<<"n="<<a<<"\tfactorial="<<b<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

int factorial(int k) {

int fact=1;

for(int i=1; i<=k; i++)

fact=fact*i;

return fact; }

81: To print numbers & also find out their factorial taking
input from user by using “user define function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 82


int factorial(int);

void main() {

int a,b;

for(int i=1; i<=4; i++) {

cout<<"Enter a="; cin>>a;

b=factorial(a);

cout<<"n="<<a<<"\tfactorial="<<b<<endl;

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

cout<<"\n Thanks "<<endl;

getch(); }

int factorial(int k) {

int fact=1;

for(int i=1; i<=k; i++)

fact=fact*i;

return fact; }

82: To print numbers from 1 through 5 & also find out their
factorial by using “user define function” & als0 find

𝒔 = ∑𝟓𝒊=𝟏 𝒊!
#include<iostream.h>

#include<conio.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 83


#include<math.h>

int factorial(int);

void main() {

int a,b,c,s=0;

for(int i=1; i<=5; i++) {

a=i;

b=factorial(a);

s=s+b;

cout<<"n="<<a<<"\tfactorial="<<b<<endl;}

cout<<" sum of factorial= "<<s<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

int factorial(int k) {

int fact=1;

for(int i=1; i<=k; i++)

fact=fact*i;

return fact; }

83: To print numbers & also find out their factorial taking
input from user by using “user define function” and also find

𝒔 = ∑𝟓𝒊=𝟏 𝒊!

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 84


#include<iostream.h>

#include<conio.h>

#include<math.h>

int factorial(int);

void main() {

int a,b,s=0;

for(int i=1; i<=4; i++) {

cout<<"Enter a="; cin>>a;

b=factorial(a);

s=s+b;

cout<<"n="<<a<<"\tfactorial="<<b<<endl;

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

cout<<" Sum of factorial= "<<s<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

int factorial(int k) {

int fact=1;

for(int i=1; i<=k; i++)

fact=fact*i;

return fact; }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 85


84: To print numbers from 1 through 5 & also find out their
factorial by using “user define function” & als0 find

𝒔 = ∑𝟓𝒊=𝟏 𝟏/𝒊!
#include<iostream.h>

#include<conio.h>

#include<math.h>

int factorial(int);

void main() {

float a,b,s=0;

for(int i=1; i<=5; i++) {

a=i;

b=factorial(a);

cout<<"n="<<a<<"\tfactorial="<<b<<endl;

s=s+(1/b); }

cout<<" sum of factorial= "<<s<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

int factorial(int k) {

int fact=1;

for(int i=1; i<=k; i++)

fact=fact*i;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 86


return fact; }

85: To print numbers from 1 through 10 & also find out


their factorial by using “user define function” & als0 find
𝟏
𝒔 = ∑𝟏𝟎 𝒊
𝒊=𝟏(−𝟏) (𝒊!)

#include<iostream.h>

#include<conio.h>

#include<math.h>

int factorial(int);

void main() {

float a,b,s=0;

for(int i=1; i<=10; i++) {

a=i;

b=factorial(a);

cout<<"n="<<a<<"\tfactorial="<<b<<endl;

s=s+pow(-1,i)*(1/b); }

cout<<" sum of factorial= "<<s<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

int factorial(int k) {

int fact=1;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 87


for(int i=1; i<=k; i++)

fact=fact*i;

return fact; }

86: To print even numbers from 1 through 10 & also find


out their factorial by using “user define function” & als0 find

𝒔 = ∑𝟏𝟎
𝒊=𝟐 𝒊!

#include<iostream.h>

#include<conio.h>

#include<math.h>

int factorial(int);

void main() {

int a,b,s=0;

for(int i=2; i<=10; i=i+2) {

a=i;

b=factorial(a);

s=s+b;

cout<<"n="<<a<<"\tfactorial="<<b<<endl;}

cout<<"\n sum of factorial= "<<s<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 88


int factorial(int k) {

int fact=1;

for(int i=1; i<=k; i++)

fact=fact*i;

return fact; }

87: To print numbers from 1 through 5 & also find out their
factorial by using “user define function” & als0 find
𝟏 𝟑 𝟓 𝟕 𝟗
𝒔 = ∑𝟏𝟎
𝒊=𝟐(𝟐! + 𝟒! + 𝟔! + 𝟖! + 𝟏𝟎!)

#include<iostream.h>

#include<conio.h>

#include<math.h>

float factorial(float);

void main() {

float s=0,p;

for(int i=1; i<=10; i++)

s=s+((2*i-1)/factorial(p));

cout<<" S= "<<s<<endl;

cout<<" Thanks "<<endl;

getch(); }

float factorial(float k1) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 89


int p=1;

for(int i=2; i<=10; i=i+2) {

p=p*i; }

return p; }

88: To calculate Area & Circumference for different values


of r by using “user define function”.
When no input & no output
#include<iostream.h>

#include<conio.h>

#include<math.h>

void area(void);

void cir(void);

void main (){

for(int i=1; i<=5; i++){

area();

cir();

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

cout<<"\n Thanks "<<endl;

getch(); }

void area(void) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 90


float r,A;

cout<<" r= "; cin>>r;

A=3.14*r*r;

cout<<"r="<<r<<"\t area="<<A<<endl; }

void cir(void) {

float r,C;

cout<<" r= "; cin>>r;

C=2*3.14*r;

cout<<"r="<<r<<"\t Cir="<<C<<endl; }

89: To find out maximum of three numbers by using “user


define function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

int max3(int,int,int);

void main (){

int a,b,c,max;

for(int i=1; i<=3; i++) {

cout<<" Enter a= "; cin>>a;

cout<<" Enter b= "; cin>>b;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 91


cout<<" Enter c= "; cin>>c;

max=max3(a,b,c);

cout<<" a= "<<a<<"\t b= "<<b<<"\t c= "<<c<<endl;

cout<<" Maximum of 3 numbers= "<<max<<endl;

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

cout<<"\n Thanks "<<endl;

getch(); }

int max3(int c,int d,int e) {

int max=c;

if(c>d && c>e)

max=c;

if(d>c && d>e)

max=d;

if(e>d && e>c)

max=e;

return max; }

90: To find out minimum of three numbers by using “user


define function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 92


int min3(int,int,int);

void main (){

int a,b,c,min;

for(int i=1; i<=3; i++) {

cout<<" Enter a= "; cin>>a;

cout<<" Enter b= "; cin>>b;

cout<<" Enter c= "; cin>>c;

min=min3(a,b,c);

cout<<" a= "<<a<<"\t b= "<<b<<"\t c= "<<c<<endl;

cout<<" Minimum of 3 numbers= "<<min<<endl;

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

cout<<"\n Thanks "<<endl;

getch(); }

int min3(int c,int d,int e) {

int min=c;

if(c<d && c<e)

min=c;

if(d<c && d<e)

min=d;

if(e<d && e<c)

min=e;

return min; }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 93


91: To find out maximum of four numbers by using “user
define function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

int max4(int,int,int,int);

void main (){

int a,b,c,d,max;

for(int i=1; i<=3; i++) {

cout<<" Enter a= "; cin>>a;

cout<<" Enter b= "; cin>>b;

cout<<" Enter c= "; cin>>c;

cout<<" Enter d= "; cin>>d;

max=max4(a,b,c,d);

cout<<" a= "<<a<<"\t b= "<<b<<"\t c= "<<c<<" d= "<<d<<endl;

cout<<" Maximum of 4 numbers= "<<max<<endl;

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

cout<<"\n Thanks "<<endl;

getch(); }

int max4(int c,int d,int e,int f) {

int max=c;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 94


if(c>d && c>e && c>f)

max=c;

if(d>c && d>e && d>f)

max=d;

if(e>d && e>c && e>f)

max=e;

if(f>d && f>c && f>e)

max=f;

return max; }

92: To find out minimum of four numbers by using “user


define function”.
#include<iostream.h>

#include<conio.h>

#include<math.h>

int min4(int,int,int,int);

void main (){

int a,b,c,d,min;

for(int i=1; i<=3; i++) {

cout<<" Enter a= "; cin>>a;

cout<<" Enter b= "; cin>>b;

cout<<" Enter c= "; cin>>c;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 95


cout<<" Enter d= "; cin>>d;

min=min4(a,b,c,d);

cout<<" a= "<<a<<"\t b= "<<b<<"\t c= "<<c<<" d= "<<d<<endl;

cout<<" Minimum of 4 numbers= "<<min<<endl;

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

cout<<"\n Thanks "<<endl;

getch(); }

int min4(int c,int d,int e,int f) {

int min=c;

if(c<d && c<e && c<f)

min=c;

if(d<c && d<e && d<f)

min=d;

if(e<d && e<c && e<f)

min=e;

if(f<d && f<c && f<e)

min=f;

return min; }

93:
Evaluate the Integral:

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 96


𝟔
𝒇(𝒙) = ∫𝟏 𝒆√𝒙 𝒅𝒙
𝒃−𝒂 𝟓
For n=6 and h= 𝒏
= 𝟔 = 𝟎. 𝟖𝟑𝟑𝟑

i) Trapizoidal rule:
𝒉
I= 𝟐 [(f0+f6)+2(f1+f2+f3+f4+f5)]
𝟏
ii) Simpson’s (𝟑) rule:
𝒉
I= 𝟑 [(f0+f6)+2(f2+f4)+4(f1+f3+ f5)]
𝟑
iii) Simpson’s (𝟖) rule:
𝟑𝒉
I= [(f0+f6)+3(f1+f2+f4+f5)+2f3]
𝟖
iv) Boole’s rule:
𝟐𝒉
I= 𝟒𝟓 [7(f0+f6)+32(f1+f3+f5)+12(f2+f4)]
v) Weddle’s rule:
𝟑𝒉
I= [(f0+f6)+(f2+f4)+5(f1+f5)+6f3]
𝟖

X f(x)
1 2.718 f0
1.833 3.872 f1
2.666 5.118 f2
3.499 6.492 f3
4.332 8.015 f4
5.165 9.705 f5

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 97


5.998≈6 11.578 f6

Now, by putting above values;we have

i) I=33.612
ii) I=33.553
iii) I=33.552
iv) I=33.295
v) I=33.552
Now, programming of these five rules in C++;

i) Trapizoidal rule:
#include<iostream.h>

#include<conio.h>

#include<math.h>

float f(float x) {

return (exp(sqrt(x))); }

void main() {

float a,b,n,sum,h,I;

cout<<" Enter upper limit= "; cin>>b;

cout<<" Enter lower limit= "; cin>>a;

cout<<" Enter n= "; cin>>n;

h=(b-a)/n;

sum=f(a)+f(b);

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 98


for(int i=1; i<n; i++)

sum=sum+2*f(a+i*h);

I=(sum*h)/2;

cout<<" I by Trapizoidal rule= "<<I<<endl;

cout<<" Thanks "<<endl;

getch(); }

𝟏
ii) Simpson’s (𝟑) rule:

#include<iostream.h>

#include<conio.h>

#include<math.h>

float f(float x) {

return (exp(sqrt(x))); }

void main() {

float a,b,n,sum,h,I;

cout<<" Enter upper limit= "; cin>>b;

cout<<" Enter lower limit= "; cin>>a;

cout<<" Enter n= "; cin>>n;

h=(b-a)/n;

sum=f(a)+f(b);

for(int i=1; i<n; i++)

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 99


if(i%2==0)

sum=sum+2*f(a+i*h);

else

sum=sum+4*f(a+i*h);

I=(sum*h)/3;

cout<<" I by simpson's (1/3) rule= "<<I<<endl;

cout<<" Thanks "<<endl;

getch(); }

𝟑
iii) Simpson’s (𝟖) rule:

#include<iostream.h>

#include<conio.h>

#include<math.h>

float f(float x) {

return (exp(sqrt(x))); }

void main() {

float a,b,n,sum,h,I;

cout<<" Enter upper limit= "; cin>>b;

cout<<" Enter lower limit= "; cin>>a;

cout<<" Enter n= "; cin>>n;

h=(b-a)/n;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 100


sum=(f(a)+f(b));

for(int i=1; i<n; i++)

if(i==3)

sum=sum+2*f(a+i*h);

else

sum=sum+3*f(a+i*h);

I=(3*sum*h)/8;

cout<<" I by Simpson's (3/8) rule= "<<I<<endl;

cout<<" Thanks "<<endl;

getch(); }

iv) Boole’s rule:


#include<iostream.h>

#include<conio.h>

#include<math.h>

float f(float x) {

return (exp(sqrt(x))); }

void main() {

float a,b,n,sum,h,I;

cout<<" Enter upper limit= "; cin>>b;

cout<<" Enter lower limit= "; cin>>a;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 101


cout<<" Enter n= "; cin>>n;

h=(b-a)/n;

sum=7*(f(a)+f(b));

for(int i=1; i<n; i++)

if(i%2==0)

sum=sum+12*f(a+i*h);

else

sum=sum+32*f(a+i*h);

I=(2*sum*h)/45;

cout<<" I by Boole's rule= "<<I<<endl;

cout<<" Thanks "<<endl;

getch(); }

v) Weddle’s rule:
#include<iostream.h>

#include<conio.h>

#include<math.h>

float f(float x) {

return (exp(sqrt(x))); }

void main() {

float a,b,n,sum,h,I;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 102


cout<<" Enter upper limit= "; cin>>b;

cout<<" Enter lower limit= "; cin>>a;

cout<<" Enter n= "; cin>>n;

h=(b-a)/n;

sum=f(a)+f(b);

for(int i=1; i<n; i++)

if(i%2==0)

sum=sum+f(a+i*h);

else if(i==3)

sum=sum+6*f(a+i*h);

else

sum=sum+5*f(a+i*h);

I=(3*sum*h)/10;

cout<<" I by Weddle's rule= "<<I<<endl;

cout<<" Thanks "<<endl;

getch(); }

▪ Some other programs of Step Functions:


𝑥 2 + 2𝑥 + 7 𝑖𝑓 𝑥 > 5
94: 𝐹(𝑥) = {
𝑥 + 1 𝑖𝑓 𝑥 ≤ 5
and 𝟏 ≤ 𝒙 ≤ 𝟏𝟓 by array method.
#include<iostream.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 103


#include<conio.h>

#include<math.h>

void main() {

int x[15],y[15]; int ind=0;

int i=1;

cout<<" x\tf(x)\n ";

while( i<=15) {

x[ind]=i;

if(x[ind]>5)

y[ind]=x[ind]*x[ind]+2*x[ind]+7;

else

y[ind]=x[ind]+1;

ind=ind+1;

i=i+1; }

for( int i=0; i<ind; i++)

cout<<x[i]<<"\t"<<y[i]<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 104


𝟏 𝒊𝒇 𝒙 > 0
95: 𝒇(𝒙) = { 𝟎 𝒊𝒇 𝒙 = 𝟎
−𝟏 𝒊𝒇 𝒙 < 0
Such that −𝟏𝟓 ≤ 𝒙 ≤ 𝟏𝟓
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int x,y;

cout<<" x\tf(x)\n ";

for(int i=-15; i<=15; i++) {

x=i;

if(x>0)

y=1;

else if (x==0)

y=0;

else

y=-1;

cout<<x<<"\t"<<y<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 105


𝒙 𝒊𝒇 𝒙 ≥ 𝟎
96: 𝒇(𝒙) = {
−𝒙 𝒊𝒇 𝒙 < 0
Such that −𝟏𝟓 ≤ 𝒙 ≤ 𝟏𝟓
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int x,y;

cout<<" x\tf(x)\n ";

for(int i=-15; i<=15; i++) {

x=i;

if(x>=0)

y=x;

else

y=(-1)*x;

cout<<x<<"\t"<<y<<endl; }

cout<<"\n Thanks "<<endl;

getch(); }

97: Half-Wave Rectifier:


𝑽𝒔 = 𝒔𝒊𝒏(𝝅𝒕) for 𝟎 ≤ 𝒕 ≤ 𝟏𝟎, 𝒉 = 𝟎. 𝟓

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 106


𝑽 𝒊𝒇 𝑽𝒔 > 0
𝑽𝑳 = { 𝑳
𝟎 𝒊𝒇 𝑽𝒔 ≤ 𝟎
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float vL[1000],vS[1000],t=-5,pi=3.14; int ind=0;

cout<<" vS\tvL "<<endl;

while (t<=5) {

vS[ind]=sin(pi*t);

if(vS[ind]>0)

vL[ind]=vS[ind];

else

vL[ind]=0;

ind=ind+1;

t=t+.5; }

for(int i=0; i<ind; i++)

cout<<vS[i]<<"\t"<<vL[i]<<endl;

cout<<" Thanks "<<endl;

getch (); }

98: Full-Wave Rectifier:

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 107


𝑽𝒔 = 𝒔𝒊𝒏(𝝅𝒕) for 𝟎 ≤ 𝒕 ≤ 𝟏𝟎, 𝒉 = 𝟎. 𝟓
𝑽𝒔 𝒊𝒇 𝑽𝒔 > 0
𝑽𝑳 = {
−𝑽𝒔 𝒊𝒇 𝑽𝒔 ≤ 𝟎
#include<iostream.h>

#include<conio.h>

#include<math.h>

// Full wave rectifier

void main() {

float vL[1000],vS[1000],t=-5,pi=3.14; int ind=0;

cout<<" vS\tvL "<<endl;

while (t<=5) {

vS[ind]=sin(pi*t);

if(vS[ind]>0)

vL[ind]=vS[ind];

else

vL[ind]=(-1)*vS[ind];

ind=ind+1;

t=t+.5; }

for(int i=0; i<ind; i++)

cout<<vS[i]<<"\t"<<vL[i]<<endl;

cout<<" Thanks "<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 108


getch (); }

99: Clipper Circuit:


i) Positive clipper:

𝑽𝒔 = 𝟒𝒔𝒊𝒏(𝝅𝒕) for 𝟎 ≤ 𝒕 ≤ 𝟓, 𝒉 = 𝟎. 𝟓
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float vL[1000], vS[1000],n;

cout<<" clipping limit= "; cin>>n;

int ind=0;

float t=0,pi=3.14;

cout<<" vS\tvL\n ";

while(t<=5) {

vS[ind]=4*sin(pi*t);

if(vS[ind]<n)

vL[ind]=vS[ind];

else

vL[ind]=n;

ind=ind+1;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 109


t=t+0.5; }

for(int i=0; i<ind; i++)

cout<<vS[i]<<"\t"<<vL[i]<<endl;

cout<<" \nThanks "<<endl;

getch(); }

ii) Negative clipper:

𝑽𝒔 = 𝟒𝒔𝒊𝒏(𝝅𝒕) for 𝟎 ≤ 𝒕 ≤ 𝟓, 𝒉 = 𝟎. 𝟓
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float vL[1000], vS[1000],n;

cout<<" clipping limit= "; cin>>n;

int ind=0;

float t=0,pi=3.14;

cout<<" vS\tvL\n ";

while(t<=5) {

vS[ind]=4*sin(pi*t);

if(vS[ind]>n)

vL[ind]=vS[ind];

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 110


else

vL[ind]=n;

ind=ind+1;

t=t+0.5; }

for(int i=0; i<ind; i++)

cout<<vS[i]<<"\t"<<vL[i]<<endl;

cout<<" \nThanks "<<endl;

getch(); }

100: Clamper Circuit:


i) Positive clamper:

𝑽𝒔 = 𝟒𝒔𝒊𝒏(𝝅𝒕) for 𝟎 ≤ 𝒕 ≤ 𝟓, 𝒉 = 𝟎. 𝟓
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float vL[1000], vS[1000];

int ind=0;

float t=0,pi=3.14;

cout<<" vS\tvL\n ";

while(t<=5) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 111


vS[ind]=4*sin(pi*t);

vL[ind]=vS[ind]+3;

ind=ind+1;

t=t+0.5; }

for(int i=0; i<ind; i++)

cout<<vS[i]<<"\t"<<vL[i]<<endl;

cout<<" \nThanks "<<endl;

getch(); }

ii) Negative clamper:

𝑽𝒔 = 𝟒𝒔𝒊𝒏(𝝅𝒕) for 𝟎 ≤ 𝒕 ≤ 𝟓, 𝒉 = 𝟎. 𝟓
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float vL[1000], vS[1000];

int ind=0;

float t=0,pi=3.14;

cout<<" vS\tvL\n ";

while(t<=5) {

vS[ind]=4*sin(pi*t);

vL[ind]=-vS[ind]-3;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 112


ind=ind+1;

t=t+0.5; }

for(int i=0; i<ind; i++)

cout<<vS[i]<<"\t"<<vL[i]<<endl;

cout<<" \nThanks "<<endl;

getch(); }

101: Double Clipper Circuit:


𝑽𝒔 = 𝟒𝒔𝒊𝒏(𝝅𝒕) for 𝟎 ≤ 𝒕 ≤ 𝟓, 𝒉 = 𝟎. 𝟓
#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<time.h>

void main() {

float vL[1000], vS[1000],n1,n2;

int ind=0;

float t=0;

cout<<" upper clipping= "; cin>>n1;

cout<<" lower clipping= "; cin>>n2;

cout<<" vS\tvL\n ";

while(t<=5) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 113


vS[ind]=4*sin(3.14*t);

if(vS[ind]<n1 && vS[ind]>0)

vL[ind]=vS[ind];

else

vL[ind]=n1;

if(vS[ind]<0 && vS[ind]>n2)

vL[ind]=vS[ind];

else

vL[ind]=n2;

ind=ind+1;

t=t+0.5; }

for(int i=0; i<ind; i++)

cout<<vS[i]<<"\t"<<vL[i]<<endl;

cout<<" \nThanks "<<endl;

getch(); }

102: To check location/ to find an element in matrix or in


vector.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 114


float M[3][3],k;

cout<<" Enter Matrix Entries: ";

for(int m=0; m<3; m++)

for(int n=0; n<3; n++)

cin>>M[m][n];

cout<<" Mattrix:\n";

for(int m=0; m<3; m++){

for(int n=0; n<3; n++)

cout<<M[m][n]<<" ";

cout<<"\n"; }

cout<<"\n Enter Number: ";

cin>>k;

for(int m=0; m<3; m++)

for(int n=0; n<3; n++)

if(k==M[m][n])

cout<<"\n No. in present at location= "<<(m+1)<<","<<(n+1);

cout<<"\n Thanks "<<endl;

getch(); }

Binary Numbers:

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 115


103: To convert decimal number to binary number for
base 2.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int r,n;

int ar[100];

int ind=0;

int k;

cout<<" Enter n= "; cin>>n;

k=n;

while (n>=1) {

r =n%2;

n=n/2;

cout<<r<<" ";

ar[ind]=r;

ind=ind+1; }

cout<<endl;

cout<<"\n Number= "<<k<<endl;

for(int i=ind-1; i>=0; i--)

cout<<ar[i]<<" ";

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 116


cout<<"\n Thanks "<<endl;

getch (); }

104: To convert decimal number to binary number for base


5.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int r,n;

int ar[100];

int ind=0;

int k;

cout<<" Enter n= "; cin>>n;

k=n;

while (n>=1) {

r =n%5;

n=n/5;

cout<<r<<" ";

ar[ind]=r;

ind=ind+1; }

cout<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 117


cout<<"\n Number= "<<k<<endl;

cout<<"Binary Numbers= ";

for(int i=ind-1; i>=0; i--)

cout<<ar[i]<<" ";

cout<<"\n Thanks "<<endl;

getch (); }

105: To convert decimal number to binary number for


base 8.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int r,n;

int ar[100];

int ind=0;

int k;

cout<<" Enter n= "; cin>>n;

k=n;

while (n>=1) {

r =n%8;

n=n/8;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 118


cout<<r<<" ";

ar[ind]=r;

ind=ind+1; }

cout<<endl;

cout<<"\n Number= "<<k<<endl;

for(int i=ind-1; i>=0; i--)

cout<<ar[i]<<" ";

cout<<"\n Thanks "<<endl;

getch (); }

106: To convert decimal number to binary number for


base 16.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int r,n;

int ar[100];

int ind=0;

int k;

cout<<" Enter n= "; cin>>n;

k=n;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 119


while (n>=1) {

r =n%16;

n=n/16;

cout<<r<<" ";

ar[ind]=r;

ind=ind+1; }

cout<<endl;

cout<<"\n decimal Number= "<<k<<endl;

cout<<"\n binary numbers= ";

for(int i=ind-1; i>=0; i--)

if(ar[i]<=9)

cout<<ar[i]<<" ";

else if(ar[i]==10)

cout<<"A";

else if(ar[i]==11)

cout<<"B";

else if(ar[i]==12)

cout<<"C";

else if(ar[i]==13)

cout<<"D";

else if(ar[i]==14)

cout<<"E";

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 120


else if(ar[i]==15)

cout<<"F";

else

cout<<" invalid option ";

cout<<"\n Thanks "<<endl;

getch (); }

107: To convert binary numbers to decimal number.


#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

int r,n; int number=0;

int ar[100];

int ind=0;

int k;

cout<<" Binary Numbers= "; cin>>n;

k=n;

while (n>=1) {

r =n%10;

n=n/10;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 121


cout<<r<<" ";

ar[ind]=r;

ind=ind+1; }

cout<<endl;

cout<<"\n Number= "<<k<<endl;

for(int i=0; i<ind; i++)

number=number+ar[i]*pow(2,i);

cout<<" Decimal number="<<number<<endl;

cout<<"\n Thanks "<<endl;

getch (); }

➢ Applications of Euler’s Method:


108: Q: Write C++ program to calculate and print time
against position values for SHO(a mass attached to spring)
using Euler’s Method.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float ta[1000],xa[1000];

floatt,h,tmax,x,v,a,k,m;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 122


x=0; v=15; t=0; h=0.3; tmax=5.5;

k=1; m=1;

intind=0;

while(t<=tmax) {

a=(-k*x)/m;

ta[ind]=t;

xa[ind]=x;

x=x+v*h;

v=v+a*h;

t=t+h;

ind=ind+1; }

cout<<" t\tx "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<xa[i]<endl;

cout<<" Thanks "<<endl;

getch(); }

109: Write C++ program to calculate and print time against


position,acceleration and velocity values for SHO(a mass
attached to spring) using Euler’s Method.
#include<iostream.h>

#include<conio.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 123


#include<math.h>

void main() {

float ta[1000],xa[1000],aa[1000],va[1000];

floatt,h,tmax,x,v,a,k,m;

x=0; v=15; t=0; h=0.3; tmax=5.5;

k=1; m=1;

intind=0;

while(t<=tmax) {

a=(-k*x)/m;

ta[ind]=t;

xa[ind]=x;

va[ind]=v;

aa[ind]=a;

x=x+v*h;

v=v+a*h;

t=t+h;

ind=ind+1; }

cout<<" t\tx\tv\ta "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<xa[i]<<"\t"<<va[i]<<"\t"<<aa[i]<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 124


110: Write C++ program to calculate and print time against
position,acceleration and velocity values for DHO using Euler’s
Method.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float ta[1000],xa[1000],aa[1000],va[1000];

float t,h,tmax,x,v,a,k,m,b;

x=0; v=15; t=0; h=0.3; tmax=5.5;

k=1; m=1;

int ind=0;

while(t<=tmax) {

a=(((-k)*x)-(b*v))/m;

ta[ind]=t;

xa[ind]=x;

va[ind]=v;

aa[ind]=a;

x=x+v*h;

v=v+a*h;

t=t+h;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 125


ind=ind+1; }

cout<<" t\tx\tv\ta "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<xa[i]<<"\t"<<va[i]<<"\t"<<aa[i]<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

111: Write C++ program to calculate and print time against


position values for DHO using Euler’s Method.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float ta[1000],xa[1000];

float t,h,tmax,x,v,a,k,m,b;

x=0; v=15; t=0; h=0.3; tmax=5.5;

k=1; m=1;

int ind=0;

while(t<=tmax) {

a=(((-k)*x)-(b*v))/m;

ta[ind]=t;

xa[ind]=x;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 126


x=x+v*h;

v=v+a*h;

t=t+h;

ind=ind+1; }

cout<<" t\tx "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<xa[i]<endl;

cout<<" Thanks "<<endl;

getch(); }

112: Write C++ program to calculate and print time against


position values for FHO using Euler’s Method.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float ta[1000],xa[1000];

float t,h,tmax,x,v,a,k,m,b,fo,w;

x=0; v=15; t=0; h=0.3; tmax=5.5; fo=1; w=1;

k=1; m=1;

int ind=0;

while(t<=tmax) {

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 127


a=((-k*x)-(b*v)+(fo*cos(w*t)))/m;

ta[ind]=t;

xa[ind]=x;

x=x+v*h;

v=v+a*h;

t=t+h;

ind=ind+1; }

cout<<" t\tx "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<xa[i]<endl;

cout<<"\n Thanks "<<endl;

getch(); }

113: Write C++ program to calculate and print time against


position,acceleration and velocity values for FHO using Euler’s
Method.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float ta[1000],xa[1000],aa[1000],va[1000];

float t,h,tmax,x,v,a,k,m,b,fo,w;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 128


x=0; v=15; t=0; h=0.3; tmax=5.5; b=1; fo=1; w=1;

k=1; m=1;

int ind=0;

while(t<=tmax) {

a=((-k*x)-(b*v)+(fo*cos(w*t)))/m;

ta[ind]=t;

xa[ind]=x;

va[ind]=v;

aa[ind]=a;

x=x+v*h;

v=v+a*h;

t=t+h;

ind=ind+1; }

cout<<" t\tx\tv\ta "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<xa[i]<<"\t"<<va[i]<<"\t"<<aa[i]<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

➢ RC-Series Circuit:

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 129


114: write C++ program to calculate and print time against
charge values(growth of charge) using Euler’s Method for RC-
series circuit.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float ta[1000],qa[1000];

floatt,h,tmax,q,v,c,r,i;

q=0; v=3; t=0; h=0.1; tmax=2.5;

c=0.5; r=5;

intind=0;

while(t<=tmax) {

i=(v-(q/c))/r;

ta[ind]=t;

qa[ind]=q;

q=q+i*h;

t=t+h;

ind=ind+1; }

cout<<" t\tq "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<qa[i]<<endl;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 130


cout<<"\n Thanks "<<endl;

getch(); }

115: write C++ program to calculate and print time against


charge values(decay of charge) using Euler’s Method for RC-
series circuit.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float ta[1000],qa[1000];

float t,h,tmax,q,v,c,r,i;

q=2; v=0; t=0; h=0.1; tmax=2.5;

c=0.5; r=5;

int ind=0;

while(t<=tmax) {

i=(v-(q/c))/r;

ta[ind]=t;

qa[ind]=q;

q=q+i*h;

t=t+h;

ind=ind+1; }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 131


cout<<" t\tq "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<qa[i]<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

116: write C++ program to calculate and print time against


charge values(growth of charge) using Euler’s Method & also
find maximum charge for RC-series circuit.
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main() {

float ta[1000],qa[1000];

float t,h,tmax,q,v,c,r,i,qo;

q=0; v=3; t=0; h=0.1; tmax=2.5;

c=0.5; r=5;

int ind=0;

while(t<=tmax) {

i=(v-(q/c))/r;

ta[ind]=t;

qa[ind]=q;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 132


q=q+i*h;

t=t+h;

ind=ind+1; }

cout<<" t\tq "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<qa[i]<<endl;

float qmax=qa[0];

for(int i=0; i<ind; i++)

if(qa[i]>qmax)

qmax=qa[i];

cout<<" Maximum charge "<<qmax<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

➢ RL-Series Circuit:
117: write C++ program to calculate and print time against
current values(growth of current) using Euler’s Method for RL-
series circuit.
#include<iostream.h>

#include<conio.h>

#include<math.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 133


void main() {

float ta[1000],ia[1000];

float t,h,tmax,l,v,r,k,i;

i=0; l=10; v=10; t=0; h=0.2; tmax=2.5; r=10;

int ind=0;

while(t<=tmax) {

k=(v-i*r)/l;
ta[ind]=t;

ia[ind]=i;
i=i+k*h;

t=t+h;

ind=ind+1; }

cout<<" t\ti "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<ia[i]<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

118: write C++ program to calculate and print time against


current values(decay of current) using Euler’s Method for RL-
series circuit.
#include<iostream.h>

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 134


#include<conio.h>

#include<math.h>

void main() {

float ta[1000],ia[1000];

float t,h,tmax,l,v,r,k,i;

l=10; i=3; v=0; t=0; h=0.2; tmax=2.5; r=10;

int ind=0;

while(t<=tmax) {

k=(v-i*r)/l;

ta[ind]=t;

ia[ind]=i;

i=i+k*h;

t=t+h;

ind=ind+1; }

cout<<" t\ti "<<endl;

for(int i=0; i<ind; i++)

cout<<"\n"<<ta[i]<<"\t"<<ia[i]<<endl;

cout<<"\n Thanks "<<endl;

getch(); }

➢ Ascending & Descending order:

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 135


119: To print random numbers in the range 1 through 10 &
data is sorted in descending order.
#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<time.h>

#include<stdlib.h>

void main() {

float f[10],c;

srand(time(0));

cout<<" Random Numbers= ";

for(int i=0; i<10; i++) {

f[i]=1+rand()%6;

cout<<f[i]<<" "; }

for(int p=0; p<9; p++)

for(int i=0; i<9; i++)

if(f[i+1]>f[i]) {

c=f[i];

f[i]=f[i+1];

f[i+1]=c; }

cout<<"\n Descending order= ";

for(int i=0; i<10; i++)

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 136


cout<<f[i]<<" ";

cout<<"\n Thanks "<<endl;

getch(); }

120: To print random numbers in the range 1 through 10 &


sorted data in ascending order.
#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<time.h>

#include<stdlib.h>

void main() {

float f[10],c;

srand(time(0));

cout<<" Random Numbers= ";

for(int i=0; i<10; i++) {

f[i]=1+rand()%6;

cout<<f[i]<<" "; }

for(int p=0; p<9; p++)

for(int i=0; i<9; i++)

if(f[i+1]<f[i]) {

c=f[i];

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 137


f[i]=f[i+1];

f[i+1]=c; }

cout<<"\n Ascending order= ";

for(int i=0; i<10; i++)

cout<<f[i]<<" ";

cout<<"\n Thanks "<<endl;

getch(); }

121: To print random numbers in the range 1 through 10 &


sorted data in ascending order as well as in descending order
by using array reversal.
#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<time.h>

#include<stdlib.h>

void main() {

float f[10],c;

srand(time(0));

cout<<" Random Numbers= ";

for(int i=0; i<10; i++) {

f[i]=1+rand()%6;

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 138


cout<<f[i]<<" "; }

for(int p=0; p<9; p++)

for(int i=0; i<9; i++)

if(f[i+1]>f[i]) {

c=f[i];

f[i]=f[i+1];

f[i+1]=c; }

cout<<"\n Descending order= ";

for(int i=0; i<10; i++)

cout<<f[i]<<" ";

cout<<"\n Ascending order= ";

for(int i=9; i>=0; i--)

cout<<f[i]<<" ";

cout<<"\n Thanks "<<endl;

getch(); }

University Of The Punjab z a i n a l i b h a h @ g m a i l . c o m / Page 139

You might also like