Sunday, August 14, 2011

Calculating the maximum and minimum element of Array in C

This program illustrates the concept of One-Dimensional Array.  In this program some elements are taken in array and maximum and minimum element are found  out. Complete source code and Output is given here..

Source Code

//finding the maximum and minimum element of array
#include<stdio.h>
int findmax(int array[], int n){
    int i, maximum;
    maximum = array[0];
    for(i = 0; i < n; i++){
        if(maximum < array[i])
            maximum = array[i];
    }
    return maximum;
}
int findmin(int array[], int n){
    int i, minimum;
    minimum = array[0];
    for(i = 0; i < n; i++){
        if(minimum > array[i])
            minimum = array[i];
    }
    return minimum;
}
int main(){
    int array[10]; //array declaration of maximum size 10
    int max,min,n,i;
    printf("Enter the no of element: ");
    scanf("%d",&n);
    printf("Enter the array: ");
    for(i = 0; i < n; i++){
        scanf("%d",&array[i]);
    }
    max = findmax(array, n);
    min = findmin(array, n);
    printf("\nThe maximum element is %d", max);
    printf("\nThe minimum element is %d\n\n", min);
    return 0;
}
Output

array

2 comments: