Wednesday, February 16, 2011

Decision Making and Branching

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, 29.00, 53.45, 62.50
Determine the average cost and the range of values.
Range = highest value - lowest value
Program: A program to determine the range of values and the average cost of a personal computer in the market is given.

#include <stdio.h>
void 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;
sum = sum + value;
goto input;
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);
getch();
}

No comments:

Post a Comment