Q-31: Write a C/C++ program to find the maximum and minimum value in a given array.
    

Using - C


#include<stdio.h> void displayArray(int array[], int size); void findMinMax(int array[], int size); void main() { int size, temp, i; printf("Enter the size of array: "); scanf("%d", &size); int array[size]; printf("Enter %d Elements:\n", size); for(i=0; i < size; i++) { scanf("%d", &array[i]); } printf("GIVEN ARRAY: "); displayArray(array,size); findMinMax(array,size); } void displayArray(int array[], int size) { int i; for(i=0; i < size; i++) { printf("%d ", array[i]); } printf("\n"); } void findMinMax(int array[], int size) { int i, min = array[0], max = array[0]; for(i=0; i < size; i++) { if(array[i] > max) { max = array[i]; } if(array[i] < min) { min = array[i]; } } printf("Minimum Value: %d\nMaximum Value: %d", min, max); }

OUTPUT

Enter the size of array:  6
Enter 6 Elements:
55
89
12
21
35
98
GIVEN ARRAY: 55  89  12  21  35  98
Minimum  Value:  12
Maximum Value:  98

        

Using - C++


#include<iostream> using namespace std; void displayArray(int array[], int size); void findMinMax(int array[], int size); int main() { int size, temp; cout<< "Enter the size of array: "; cin>> size; int array[size]; cout<< "Enter "<< size <<" Elements:"<< endl; for(int i=0; i < size; i++) { cin>> array[i]; } cout<< "GIVEN ARRAY: "; displayArray(array,size); findMinMax(array,size); return 0; } void displayArray(int array[], int size) { for(int i=0; i < size; i++) { cout<< array[i]<< " "; } cout<< endl; } void findMinMax(int array[], int size) { int min = array[0], max = array[0]; for(int i=0; i < size; i++) { if(array[i] > max){ max = array[i]; } if(array[i] < min){ min = array[i]; } } cout<< "Minimum Value: "<< min << "\nMaximum Value: "<< max; }

OUTPUT

Enter the size of array:  5
Enter 5 Elements:
45
21
22
15
39
GIVEN ARRAY: 45  21  22  15  39
Minimum  Value:  15
Maximum Value:  45