C Programming (Turbo C++ Compiler) - Calculate Range
I have given the simple C program for calculating Range for a set of numbers. The other related linkes are,
Calculate Mean
Calculate Median
Calculate Mode
Calculate Range
Calculate Variance
Calculate Standard Deviation
Source Code
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
const char* CalculateRange(float *arrValue, int max)
{
static char range[128];
int i, small, big;
small = big = arrValue[0];
for(i = 0; i < max; i++)
{
if(arrValue[i] > big)
big = arrValue[i];
if(arrValue[i] < small)
small = arrValue[i];
}
sprintf(range, "%d - %d", small, big);
return range;
}
int main()
{
float arrNumbers[100];
int i, max;
const char *range;
char buf[1024];
printf("Total Number of Elements: ");
scanf("%d", &max);
for(i = 0; i < max; i++)
{
printf("Enter [%d] Number: ", i + 1);
scanf("%f", &arrNumbers[i]);
}
printf("Total Numbers: %d\n", max);
range = CalculateRange(arrNumbers, max);
printf("Range: %s", range);
return 0;
}
Output
Total Number of Elements: 10
Enter [1] Number: 6
Enter [2] Number: 7
Enter [3] Number: 8
Enter [4] Number: 7
Enter [5] Number: 6
Enter [6] Number: 5
Enter [7] Number: 2
Enter [8] Number: 2
Enter [9] Number: 9
Enter [10] Number: 3
Total Numbers: 10
Range: 2 - 9
|