Professional Documents
Culture Documents
Case Studies: 1. Range of Numbers
Case Studies: 1. Range of Numbers
1. Range of Numbers
Problem: A survey of the computer market shows that personal computers are sold at varying
costs by the vendors. The following is the list of costs (in hundreds) quoted by some vendors:
35.00, 40.50, 25.00, 31.25, 68.15,
47.00, 26.65, 29.00 53.45, 62.50
Determine the average cost and the range of values.
Problem analysis: Range is one of the measures of dispersion used in statistical analysis of a
series of values. The range of any series is the difference between the highest and the lowest
values in the series. That is
Range = highest value - lowest value
It is therefore necessary to find the highest and the lowest values in the series.
Program: A program to determine the range of values and the average cost of a personal
computer in the market is given in Fig.5.14.
RANGE OF NUMBERS
Program
main()
{
int count;
float value, high, low, sum, average, range;
sum = 0;
count = 0;
printf("Enter numbers in a line :
input a NEGATIVE number to end\n");
input:
scanf("%f", &value);
if (value < 0) goto output;
count = count + 1;
if (count == 1)
high = low = value;
else if (value > high)
high = value;
else if (value < low)
low = value;
output:
average = sum/count;
range = high - low;
printf("\n\n");
printf("Total values : %d\n", count);
printf("Highest-value: %f\nLowest-value : %f\n",
high, low);
printf("Range : %f\nAverage : %f\n",
range, average);
}
Output
Total values : 10
Highest-value: 68.150002
Lowest-value : 25.000000
Range : 43.150002
Average : 41.849998
When the value is read the first time, it is assigned to two buckets, high and low, through the
statement
For subsequent values, the value read is compared with high; if it is larger, the value is assigned
to high. Otherwise, the value is compared with low; if it is smaller, the value is assigned to low.
Note that at a given point, the buckets high and low hold the highest and the lowest values read
so far.
The values are read in an input loop created by the goto input; statement. The control is
transferred out of the loop by inputting a negative number. This is caused by the statement
Note that this program can be written without using goto statements. Try.
2. Pay-Bill Calculations
Problem: A manufacturing company has classified its executives into four levels for the benefit of
certain perks. The levels and corresponding perks are shown below:
Perks
Level -----------------------------------------------------------
Conveyance Entertainment
allowance allowance
1 1000 500
2 750 200
3 500 100
4 250 -
An executive's gross salary includes basic pay, house rent allowance at 25% of basic pay and
other perks. Income tax is withheld from the salary on a percentage basis as follows:
Write a program that will read an executive's job number, level number, and basic pay and then
compute the net salary after withholding income tax.
Problem analysis:
The computation of perks depends on the level, while the income tax depends on the gross
salary. The major steps are:
1. Read data.
2. Decide level number and calculate perks.
3. Calculate gross salary.
4. Calculate income tax.
5. Compute net salary.
6. Print the results.
Program: A program and the results of the test data are given in Fig. 5.15. Note that the last
statement should be an executable statement. That is, the label stop: cannot be the last line.
PAY-BILL CALCULATIONS
Program
Output
Enter level, job number, and basic pay
Enter 0 (zero) for level to END
1 1111 4000
1 1111 5980.00
2 2222 3000
2 2222 4465.00
3 3333 2000
3 3333 3007.00
4 4444 1000
4 4444 1500.00
0
END OF THE PROGRAM