C program to calculate range of values and an average cost of a personal system.



Problem

A personal system is sold at different costs by the vendors.

Let’s take the list of costs (in hundreds) quoted by some vendors −

25.00, 30.50, 15.00, 28.25, 58.15,

37.00, 16.65, 42.00 68.45, 53.50

Solution

Calculate the average cost and range of values.

The difference between the highest and the lowest values in the series is called range Hence, Range = highest value - lowest value.

Now, find the highest and the lowest values in the series.

Example

Following is the C program to calculate the range of values and average cost of a personal system −

 Live Demo

#include<stdio.h>
main(){
   int count;
   float value, high, low, sum, average, range;
   sum = 0;
   count = 0;
   printf("enter no's in line and at end press any negative number
");    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;       sum = sum + value;    goto input;       output:    average = sum/count;    range = high - low;    printf("

");    printf("Total values : %d
", count);    printf("Highest-value: %f
Lowest-value : %f
", high, low);    printf("Range : %f
Average : %f
", range, average); }

Output

When the above program is executed, it produces the following output −

Enter numbers in line and at end press any negative number
22.4 56.8 12.3 48.6 31.4 19.0 -1
Total values: 6
Highest-value: 56.799999
Lowest-value: 12.300000
Range: 44.500000
Average: 31.750000

Advertisements