Ex: No: 08 (D) Functions With Arguments & Return Type
Ex: No: 08 (D) Functions With Arguments & Return Type
AIM: To write a c program to find the smallest element of given array of elements using functions.
ALGORITHM: Step 1: Create a function small() Step 2: Inside the function a. Store the 0th index value into base b. Initialize a index to 1 c. Compare the array index value with base d. If the array index value is smaller than base store the array index value into base e. Increment the index by 1 f. Repeat the steps c & e until index reaches total no. of elements g. Return the base value Step 3: Inside the main function a. Create an integer array with necessary size b. Get the total number of elements c. Read the array elements one by one d. Call the small() function by passing array and no. of elements as arguments
FLOWCHART:
start
Read element n
s=a[i]
s=small(a,n)
stop
PROGRAM: #include<stdio.h> int small(int a[],int n) { int s,i; s=a[0]; for(i=0;i<n;i++) { if(a[i]<s) s=a[i]; } return s; } main() { int i,a[10],n,s; printf("Enter total no. of elements\n"); scanf("%d",&n); printf("Enter Array Elements one by one\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Array Elemets:\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); printf("\n");
s=small(a,n); printf("The Smallest element of given array is %d",s); } OUTPUT: Enter total no. of elements 5 Enter Array Elements one by one 1 98 2 66 0
Array Elemets: 1 98 2 66 0
RESULT: Thus the c program to find the smallest element of given array of elements using functions was written, entered, executed and the output was verified.