## Chapter 3: Programming Exercise

3.1

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

## 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.

## 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
………………

## 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)

## 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”.

## 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.

## 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! + ………

## OUTPUT

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

## 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

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

## 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.

## 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