Dain1234 / Design-and-analysis-algorithm

0 stars 0 forks source link

minimum and maximum in list #12

Open Dain1234 opened 2 years ago

Dain1234 commented 2 years ago

image

Dain1234 commented 2 years ago

include

include

// Function to find the minimum and // maximum element of the array void findMinimumMaximum(int arr[], int N) { int i;

// variable to store the minimum
// and maximum element
int minE = INT_MAX, maxE = INT_MIN;

// Traverse the given array
for (i = 0; i < N; i++) {

    // If current element is smaller
    // than minE then update it
    if (arr[i] < minE) {
        minE = arr[i];
    }

    // If current element is greater
    // than maxE then update it
    if (arr[i] > maxE) {
        maxE = arr[i];
    }
}

// Print the minimum and maximum element
printf("The minimum element is %d", minE);
printf("\n");
printf("The maximum element is %d", maxE);

return;

}

// Driver Code int main() {

// Given array
int arr[] = { 1, 2, 4, -1 };

// length of the array
int N = sizeof(arr) / sizeof(arr[0]);

// Function call
findMinimumMaximum(arr, N);

return 0;

}