C program to find Smallest and largest elements from an array.

QuestionsCategory: QuestionsC program to find Smallest and largest elements from an array.
XorientXorient asked 1 year ago

Write a C program to find smallest and largest element of single dimensional array of size n by passing 1d array to a function named small_large using call by reference

1 Answers
Mahesh DeshmaneMahesh Deshmane answered 1 year ago

Here is C code snippet to find small and large number from 1d array


// C program to find maximum in arr[] of size n
#include  

int main() 
{ 
    int arr[] = {10, 324, 45, 90, 9808}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    largest(arr, n);
    return 0; 
    
} 

void largest(int arr[], int n) 
{ 
    
    int max = arr[0]; 
    int min = arr[0];
    
    for (int i = 1; i < n; i++)
    {
        if (arr[i] > max) 
            max = arr[i];
        
        if (arr[i] < min) 
            min = arr[i];
    }
    
    printf("Largest number in given array is %d", max); 
    printf("\nSmallest number in given array is %d", min); 
    
}