Problem Description
Write a program to find the range of the elements in the array.
Range of an array is the difference between the maximum and minimum element in an array,
Input and Output Format:
Input consists of n+1 integers where n corresponds to the number of elements in the array.
The first integer corresponds to n and the next n integers correspond to the elements in the array.
Output consists of an integer which corresponds to the range of the array.
Assume that the maximum number of elements in the array is 20.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.Test Case 1
Input (stdin)5 1 2 4 3 5
Expected OutputThe range of the array is=4
Test Case 2
Input (stdin)10 100 105 200 123 325 450 565 878 102 99
Expected OutputThe range of the array is=779
Program
#include <stdio.h> int main() { int temp,a[100],n,i,j; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } printf("The range of the array is=%d",-a[0]+a[n-1]); return 0; }
No comments:
Post a Comment
Note: only a member of this blog may post a comment.