Write a Program to Read in 10 Numbers and Compute the Average, Maximum and Minimum Values.
C program to find the maximum and minimum chemical element in an array – In this article, nosotros will brief in on the many ways to detect the maximum and minimum element in an array in C programming.
Suitable examples and sample programs have also been added and then that you tin empathise the whole affair very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
Every bit we all know, arrays are a collection of a agglomeration of elements in a sequential pattern in a horizontal direction. Arrays form a very important of C programming.
Every bit given in the example above, firstly, enter the size of the array that you desire to define.
The size of the array in the example mentioned is 5.
After that, you need to enter the elements of the assortment.
The elements entered in the assortment are equally follows:
1 ii 35 0 -ane
And so, the minimum of the array is -1 and the maximum of the array is 35.
Thus, doing the same using multiple methods in C programming is as follows:
Using Standard Method
- Read the entered array size and shop that value into the variable n.
ii)Read the entered elements using scanf and store the entered assortment elements into the array using for loop for(i=0;i<n;i++).
3)Initialise min, max values with the 1st chemical element of the array.
four)Compare min, max values with a[i],
If min value is greater than a[i] then initialise min=a[i] and if max value is less than a[i] then initialise max=a[i]. Repeat this step for each element of the string using for loop which is having the structure for(i=1;i<n;i++).
Print the minimum of the array and maximum of the array values.
ane ii 3 4 v vi 7 viii 9 10 11 12 13 14 15 sixteen 17 xviii 19 20 21 22 23 24 25 26 27 28 29 xxx 31 | #include <stdio.h> #include <conio.h> int primary ( ) { int a [ k ] , i , n , min , max ; printf ( "Enter size of the array : " ) ; scanf ( "%d" , & north ) ; printf ( "Enter elements in array : " ) ; for ( i = 0 ; i < n ; i ++ ) { scanf ( "%d" , & a [ i ] ) ; } min = max = a [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { if ( min > a [ i ] ) min = a [ i ] ; if ( max < a [ i ] ) max = a [ i ] ; } printf ( "minimum of assortment is : %d" , min ) ; printf ( "\nmaximum of array is : %d" , max ) ; return 0 ; } |
Output:
Enter size of the array : five Enter elements in array : 1 2 three 4 5 minimum of an array is : 1 maximum of an array is : 5 |
Using Role
- A function is a group of statements which perform a particular job. In this plan sumofarray() is a role which finds the minimum and maximum of an array.
ii) The chief() function calls the sumofarray() function past passing an assortment, size of the assortment value every bit arguments.
iii) The function sumofarray() compares the min, max values with assortment elements and prints the minimum of array element and maximum of array element values.
1 two 3 four 5 6 vii viii 9 10 11 12 13 14 15 16 17 18 19 xx 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 | #include <stdio.h> #include <conio.h> int sumofarray ( int a [ ] , int n ) { int min , max , i ; min = max = a [ 0 ] ; for ( i = i ; i < north ; i ++ ) { if ( min > a [ i ] ) min = a [ i ] ; if ( max < a [ i ] ) max = a [ i ] ; } printf ( "minimum of array is : %d" , min ) ; printf ( "\nmaximum of assortment is : %d" , max ) ; } int master ( ) { int a [ thousand ] , i , n , sum ; printf ( "Enter size of the array : " ) ; scanf ( "%d" , & northward ) ; printf ( "Enter elements in array : " ) ; for ( i = 0 ; i < n ; i ++ ) { scanf ( "%d" , & a [ i ] ) ; } sumofarray ( a , n ) ; } |
Output:
Enter size of the array : v Enter elements in array : ane 2 35 0 - 1 minimum of an array is : - 1 maximum of an assortment is : 35 |
Using Recursion
- A role which calls itself until some condition is called recursive function.
2) In this plan, we accept two recursive functions bachelor.one is minimum() and another i is maximum(). Both these functions telephone call by itself.
three) The main() function calls the minimum() past passing array,assortment size,1 as arguments.
Then the office minimum()
a) Checks the condition i<due north, If it is true
b) So it compares a[min]>a[i] if it is also true
c)And then min initialised to i and calls the role by itself by increasing i value until the status a[min]>a[i] becomes simulated. This function returns the min to the master office.
master() function prints the a[min] value of the array.
4)The master() function calls the maximum() function past passing array,array size,ane as arguments.
And then the function maximum()
a)Checks the condition i<northward, if it is truthful
b)And so it compares a[max]<a[i] if it is true
c)And so max initialise to i and calls the office itself past increasing i value until the condition a[max]<a[i] becomes false. This function returns the max to the main function.
main() function prints the a[max] value of the array.
i 2 iii four 5 6 7 8 ix ten 11 12 13 14 fifteen sixteen 17 18 19 20 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 36 37 38 39 forty 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <stdio.h> #include <conio.h> int minimum ( int a [ ] , int northward , int i ) { static int min = 0 ; ; if ( i < n ) { if ( a [ min ] > a [ i ] ) { min = i ; minimum ( a , n , ++ i ) ; } } return min ; } int maximum ( int a [ ] , int n , int i ) { static int max = 0 ; ; if ( i < n ) { if ( a [ max ] < a [ i ] ) { max = i ; maximum ( a , n , ++ i ) ; } } return max ; } int main ( ) { int a [ 1000 ] , i , n , sum ; printf ( "Enter size of the array : " ) ; scanf ( "%d" , & n ) ; printf ( "Enter elements in array : " ) ; for ( i = 0 ; i < due north ; i ++ ) { scanf ( "%d" , & a [ i ] ) ; } printf ( "minimum of array is : %d" , a [ ( minimum ( a , n , ane ) ) ] ) ; printf ( "\nmaximum of array is : %d" , a [ ( maximum ( a , n , 1 ) ) ] ) ; } |
Output:
Enter size of the array : 5 Enter elements in array : 9 8 7 6 0 minimum of an assortment is : 0 maximum of an array is : 9 |
Source: https://javatutoring.com/c-program-find-maximum-minimum-element-in-array/
0 Response to "Write a Program to Read in 10 Numbers and Compute the Average, Maximum and Minimum Values."
Post a Comment