Write a C++ program that prompts the user to enter an even integer N which represents how many numbers he/she wants to enter. The program then reads N decimal numbers and finds the minimum number in the first half and the maximum number in the second half.
#include <bits/stdc++.h>
using namespace std;
void findMax(int arr[], int n)
{
int max1 = INT_MIN;
int mid = n / 2;
for (int i = 0; i < mid; i++)
max1 = max(max1, arr[i]);
if (n % 2 == 1)
max1 = max(max1, arr[mid]);
int max2 = INT_MIN;
for (int i = mid; i < n; i++)
max2 = max(max2, arr[i]);
cout << max1 << ", " << max2;
}
int main()
{
int N;
int i,arr[10];
cout<<"Enter an even number N: ";
cin>>N;
cout<<"Enter 10 elements:";
for(i=0;i<10;++i)
cin>>arr[i];
int nn = sizeof(arr) / sizeof(arr[0]);
findMax(arr, nn);
return 0;
}
Comments
Leave a comment