Program To Find Range In C



In statistics and mathematics, range is said to be the difference between the highest and lowest value of a given set.

Algorithm

Algorithm of this program is very easy −

START
      
   Step 1 → Take an array A
          
   Step 2 → Loop for every value of A

   Step 3 → Find Highest and Lowest value in A
   
   Step 4 → Display Highest - Lowest as Range

STOP

Pseudocode

We derive pseudocode from the above mentioned algorithm, as follows −

procedure find_range()
   
   Array A
   
   FOR EACH x in A
      IF x > highest
         highest = x
      END IF
      
      IF x < lowest
         lowest = x
      END IF
   END FOR

   DISPLAY range is highest - lowest

end procedure

Implementation

Implementation of this algorithm is given below −

#include <stdio.h>

int main() {
   int size = 5;
   int a[5] = { 17, 3, 8, 24, 12};
   int i, highest, lowest;

   highest = lowest = a[0];
   
   for(i = 1; i < size; i++) {
      if( a[i] > highest )
         highest = a[i];

      if( a[i] < lowest)
         lowest = a[i];
   }

   printf("Highest = %d, Lowest = %d", highest, lowest);
   printf("\nRange is = %d\n", (highest-lowest)); 
   
   return 0;
}

Output

Output of the program should be −

Highest = 24, Lowest = 3
Range is = 21 
mathematical_programs_in_c.htm
Advertisements