## Chapter 3: Programming Exercise

3.1

Write a function using reference variables as arguments to swap the values of a pair of integers.

```#include<iostream>
#include<iomanip>
using namespace std;
void swap_func(int &a,int &b)
{

cout<<" Before swapping "<<endl
<<" a = "<<a<<endl<<" b = "<<b<<endl<<endl;
int temp;
temp=a;
a=b;
b=temp;
cout<<" After swapping "<<endl
<<" a = "<<a<<endl<<" b = "<<b<<endl<<endl;

}

int main()
{
int x,y;
cout<<"  Enter two integer value : "<<endl;
cin>>x>>y;
swap_func (x,y);
return 0;
}```

## OUTPUT

Enter two integer value : 56 61
Before swapping
a = 56
b = 61
After swapping
a = 56
b = 61

3.2

Write a function that creates a vector of user given size M using new operator.

```#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int m;
int *v;
cout<<"  Enter vector size : "<<endl;
cin>>m;
v=new int [m];
cout<<" to check your performance  insert "<<m<<" integer value"<<endl;
for(int i=0;i<m;i++)
{
cin>>v[i];
}
cout<<" Given integer value are :"<<endl;
for(i=0;i<m;i++)
{

if(i==m-1)
cout<<v[i];
else
cout<<v[i]<<",";

}
cout<<endl;
return 0;
}```

## OUTPUT

Enter vector size : 5
to check your performance insert 5 integer value
7 5 9 6 1
Given integer value are :
7, 5, 9, 6, 1

3.3

Write a program to print the following outputs using for loops

1
22
333
4444
55555
………………

```#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n;
cout<<" Enter your desired number :"<<endl;
cin>>n;
cout<<endl<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
cout<<i;
}
cout<<endl;
}
return 0;
}```

## OUTPUT

Enter your desired number : 6
1
22
333
4444
55555
666666

3.4

Write a program to evaluate the following investment equation
V = P(1+r)n
and print the tables which would give the value of V for various
of the following values of P, r and n:
P: 1000, 2000, 3000,……………,10,000
r: 0.10, 0.11, 0.12,………………….,0.20
n: 1, 2, 3,…………………………………..,10
(Hint: P is the principal amount and V is the value of money at the end of n years. This equation can be recursively written as
V = P(1 + r)
P = V
In other words, the value of money at the end of the first year becomes the principal amount for the next year and so on)

```#include<iostream>
#include<iomanip>
#include[itex]
#define  size 8
using namespace std;
int main()
{
float v,pf;
int n=size;
float p[size]={1000,2000,3000,4000,5000,6000,7000,8000};//9000,1000};
float r[size]={0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18};//,0.19,0.20};

cout<<setw(5)<<"n=1";
for(int i =2;i<=size;i++)
cout<<setw(9)<<"n="<<i;
cout<<"\n";

for(i=0;i<size;i++)
{
cout<<setw(-6)<<"p=";
for(int j=0;j<size;j++)
{
if(j==0)
pf=p[i];

v=pf*(1+r[i]);

cout.precision(2);
cout.setf(ios::fixed, ios::floatfield);
cout<<v<<setw(10);
pf=v;
}
cout<<"\n";

}
return 0;
}```

## OUTPUT

n=1            n=2            n=3           n=4            n=5           n=6           n=7
p=1110     1232.1      1367.63     1518.07     1685.06     1870.41    2076.16
p=2240   2508.8    2809.86     3147.04    3524.68     3947.65   4421.36
p=3390   3830.7    4328.69     4891.42     5527.31     6245.86   7057.82
p=4560   5198.4    5926.18      67 55.84    7701.66     8779.89   10009.08
p=5750    6612.5    7604.37     8745.03     10056.79   11565.3    13300.1
p=6960   8073.6    9365.38    10863.84   12602.05  14618.38  16957.32
p=8190   9582.3    11211.29     13117.21     15347.14    17956.15   21008.7
p=9440   11139.2   13144.26   15510.22    18302.06   21596.43  25483.79

3.5

An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a program to read the ballots and count the vote cast for each candidate using an array variable count. In case, a number read is outside the range 1 to 5, the ballot should be considered as a “spoilt ballot” and the program should also count the numbers of “spoilt ballots”.

```#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int count[5];
int test;
for(int i=0;i<5;i++)
{
count[i]=0;
}
int spoilt_ballot=0;
cout<<" You can vot candidate 1 to 5 "<<endl
<<" press 1 or 2 or 3 or 4 or 5 to vote "<<endl
<<" candidate 1 or 2 or 3 or 4 or 5 respectively "<<endl
<<" press any integer value outside the range 1 to 5 for NO VOTE    "<<endl<<" press any negative value to terminate  and see result :"<<endl;

while(1)
{
cin>>test;

for(int i=1;i<=5;i++)
{
if(test==i)
{
count[i-1]++;
}
}
if(test<0)
break;
else if(test>5)
spoilt_ballot++;
}
for(int k=1;k<=5;k++)
cout<<" candidate "<<k<<setw(12);
cout<<endl;
cout<<setw(7);
for(k=0;k<5;k++)
cout<<count[k]<<setw(13);
cout<<endl;
cout<<" spoilt_ballot "<<spoilt_ballot<<endl;
return 0;
}```

## OUTPUT

You can vot candidate 1 to 5
press 1 or 2 or 3 or 4 or 5 to vote
candidate 1 or 2 or 3 or 4 or 5 respectively
press any integer value outside the range 1 to S for NO VOTE
press any negative value to terminate and see result :
1
1
1
5
4
3
5
5
2
1
3
6
-1
candidate 1 candidate 2 candidate 3 candidate 4 candidate S
4   1   2   1   3
spoilt_ballot 1

3.6

A cricket has the following table of batting figure for a series of test matches:

 Player’s name Run Innings Time not ou Sachin 8430 230 18 Saurav 4200 130 9 Rahul 3350 105 11 … … … … … … … …

Write a program to read the figures set out in the above forms, to calculate the batting arranges and to print out the complete table including the averages.

```#include<iostream>
#include<iomanip>
char *serial[3]={" FIRST "," SECOND " ," THIRD "};//global declaration
using namespace std;
int main()
{
int n;
char name[100][40];
int *run;
int *innings;
int *time_not_out;
cout<<" How many players' record would you insert ? :";
cin>>n;
//name=new char[n];
run=new int[n];
innings=new int[n];
time_not_out=new int[n];

for(int i=0;i<n;i++)
{
if(i>2)
{
cout<<"\n Input details of "<<i+1<<"th"<<" player's"<<endl;
}
else
{
cout<<" Input details of "<<serial[i]<<"player's : "<<endl;
}

cout<<" Enter name : ";

cin>>name[i];
cout<<" Enter run : ";
cin>>run[i];
cout<<" Enter innings : ";
cin>>innings[i];
cout<<" Enter times not out : ";
cin>>time_not_out[i];
}

float *average;
average=new float[n];
for(i=0;i<n;i++)
{
float avrg;
average[i]=float(run[i])/innings[i];

}
cout<<endl<<endl;
cout<<setw(12)<<"player's name "<<setw(11)<<"run"<<setw(12)<<"innings"<<setw(16)<<"Average"<<setw(20)<<"times not out"<<endl;
for(i=0;i<n;i++)
{
cout<<setw(14)<<name[i]<<setw(11)<<run[i]<<setw(9)<<innings[i]<<setw(18)<<average[i]<<setw(15)<<time_not_out[i]<<endl;
}
cout<<endl;

return 0;
}```

## OUTPUT

How many players record would you insert ? :2
Input details of FIRST player’s :
Enter name : Sakib-Al-Hassan
Enter run : 1570
Enter innings : 83
Enter times not out : 10
Input details of SECOND player’s :
Enter name : Tamim
Enter run : 2000
Enter innings : 84
Enter times not out : 5
player’s name run innings Average times not out
Sakib-Al-Hassan 1570 83 18.915663 10
Tamim 2000 84 23.809525 5

3.7

Write a program to evaluate the following function to 0.0001% accuracy

(a) sinx = x – x3/3! + x5/5! – x7/7! +…………

(b) SUM = 1+(1/2)2 + (1/3)3 +(1/4)4 + ………

(c) Cosx = 1 –x2/2! + x4/4! – x6/6! + ………

```#include<iostream>
#include[itex]
#include<iomanip>
#define accuracy 0.0001
#define pi 3.1416
using namespace std;
long int fac(int a)
{
if(a<=1)
return 1;
else
return a*fac(a-1);
}
int main()
{
float y,y1,x,fx;
int n=1;
int m;
//const float pi=3.1416;
cout<<" Enter the value of angle in terms of degree:  ";
cin>>x;
float d;
d=x;
int sign;
sign=1;
if(x<0)
{
x=x*(-1);
sign=-1;
}
again:
if(x>90 && x<=180)
{

x=180-x;

}
else if(x>180 && x<=270)
{
x=x-180;
sign=-1;
}
else if(x>270 && x<=360)
{
x=360-x;
sign=-1;
}

else if(x>360)
{
int m=int(x);
float fractional=x-m;
x=m%360+fractional;
if(x>90)
goto again;
else
sign=1;

}
x=(pi/180)*x;
m=n+1;
fx=0;
for(;;)
{
long int h=fac(n);
y=pow(x,n);
int factor=pow(-1,m);
y1=y*factor;
fx+=y1/h;
n=n+2;
m++;
if(y/h<=accuracy)
break;
}

cout<<"sin("<<d<<")= "<<fx*sign<<endl;
return 0;
}
```

## OUTPUT

Enter the value of angle in terms of degree: 120
sin(120)= 0.866027

```#include<iostream>
#include[itex]
#define accuracy 0.0001
using namespace std;
int main()
{
int n;
float sum,n1,m;
n=1;sum=0;
for(int i=1;;i++)
{
n1=float(1)/n;
m=pow(n1,i);
sum+=m;
if(m<=accuracy)
break;

n++;
}
cout<<sum<<"\n";
return 0;
}
Sample Output(b)

Solution: (c)
#include<iostream.h>
#include<math.h>
#define accuracy 0.0001

long int fac(int n)
{
if(n<=1)
return 1;
else
return n*fac(n-1);
}

int main()
{

float y,y1,x,fx;
int n=1;
int m;
const float pi=3.1416;
cout<<" Enter the value of angle in terms of degree:  ";
cin>>x;
if(x<0)
x=x*(-1);
x=(pi/180)*x;

fx=1;

m=2;
float y2;
long int h;
for(;;)
{
h=fac(m);
int factor=pow(-1,n);
y1=pow(x,m);
y2=(y1/h)*factor;
fx+=y2;
if(y1/h<=accuracy)
break;
m=m+2;
n++;
}
cout<<fx<<"\n";
}```

## OUTPUT

Enter the value of angle in terms of degree: 60
0.866025

3.8

Write a program to print a table of values of the function

Y = e-x

For x varying from 0 to 10 in steps of 0.1. The table should appear as follows

TABLE FOR Y =EXP[-X];

 X     0.1      0.2       0.3      0.4      0.5      0.6     0.7      0.8      0.900                                    1.0 . . 9.0

```#include<iostream>
#include<iomanip>
#include[itex]
using namespace std;
int main()
{
float x,y;
cout<<"                          TABLE FOR  Y=EXP(-X)             :\n\n";
cout<<"x";
for(float k=0;k<.7;k=k+0.1)
cout<<setw(10)<<k;
cout<<"\n";
for(k=0;k<10*.7;k=k+0.1)
cout<<"-";
cout<<"\n";
for(float j=0;j<10;j++)
{
cout<<j<<setw(4);
for(float i=0;i<.7;i=i+0.1)
{
x=i+j;
y=exp(-x);
cout.precision(6);
cout.setf(ios::fixed,ios::floatfield);
cout<<setw(10)<<y;
}
cout<<"\n";
}
return 0;
}```

Note: Here we work with 0.4 for a good looking output.

## OUTPUT

TABLE FOR Y=EXP(-X)

x               0               0.1                  0.2                0.3                    0.4

0              1               0.904837      0.818731       0.740818      0.67032

1       0.367879       0.332871       0.301194        0.272532     0.246597

2       0.135335       0.122456       0.110803        0.100259     0.090718

3       0.049787      0.045049      0.040762       0.036883     0.033373

4       0.018316       0.016573       0.014996       0.013569      0.012277

5       0.006738      0.006097      0.005517        0.004992     0.004517

6       0.002479      0.002243      0.002029       0.001836      0.001662

7       0.000912      0.000825      0.000747        0.000676     0.000611

8       0.000335     0.000304      0.000275        0.000249      0.000225

9       0.000123      0.000112       0.000101        0.000091       0.000083

3.9

Write a program to calculate the variance and standard deviation of

N numbers

Variance =1/N ∑(xi -x)2

Standard deviation=√1/N ∑(xi -x)2

Where x = 1/N ∑xi

```#include<iostream>
#include[itex]
using namespace std;
int main()
{
float *x;
cout<<" How many number ? :";
int n;
cin>>n;
x=new float[n];
float sum;
sum=0;
for(int i=0;i<n;i++)
{
cin>>x[i];
sum+=x[i];
}
float mean;
mean=sum/n;
float v,v1;
v1=0;
for(i=0;i<n;i++)
{
v=x[i]-mean;
v1+=pow(v,2);
}
float variance,std_deviation;
variance=v1/n;
std_deviation=sqrt(variance);
cout<<"\n\n variance = "<<variance<<"\n standard deviation = "<<std_deviation<<"\n";

return 0;
}```

## OUTPUT

How many number ? :5
10
2
4
15
2
variance = 26.24
standard deviation = 5.122499

3.10

An electricity board charges the following rates to domestic users to

discourage large consumption of energy:

For the first 100 units – 60P per unit

For the first 200 units – 80P per unit

For the first 300 units – 90P per unit

All users are charged a minimum of Rs. 50.00. If the total amount is more than Rs. 300.00 then an additional surcharge of 15% is added.

Write a program to read the names of users and number of units consumed and print out the charges with names.

```#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int unit;
char name[40];
while(1)

{
input:
cout<<" Enter consumer name & unit consumed :";
cin>>name>>unit;
if(unit<=100)
{
charge=50+(60*unit)/100;
}
else if(unit<=300 && unit>100)
{
charge=50+(80*unit)/100;
}
else if(unit>300)
{
charge=50+(90*unit)/float(100);
}
cout<<setw(15)<<"Name"<<setw(20)<<"Charge"<<endl;
cout<<setw(15)<<name<<setw(20)<<charge<<endl;
cout<<" Press o for exit / press 1 to input again :";
int test;
cin>>test;
if(test==1)
goto input;
else if(test==0)
break;
}
return 0;
}```

## OUTPUT

Enter consumer name & unit consumed :sattar 200
Name                 Charge

sattar                 210

Press o for exit / press 1 to input again :1

Enter consumer name & unit consumed :santo 300

Nmae             Charge

santo              290

Press o for exit / press 1 to input again : 0