+ 1

Find the maximum and minimum between n number of array

#include <iostream> using namespace std; int main() { int n; cin>>n; int arr[n]; for(int i=0;i<n;i++){ cin>>arr[i]; } int maxNo=0; int minNo =0; for(int i=0;i<n;i++){ maxNo=max(maxNo,arr[i]); minNo=min(minNo,arr[i]); } cout<<maxNo<<" "<<minNo; return 0; }

22nd Feb 2021, 10:37 AM
soni singh
soni singh - avatar
3 Antworten
+ 4
#include <iostream> using namespace std; int main() { int n; cin>>n; int arr[n]; cin>>arr[0] int maxNo=arr[0]; int minNo =arr[0]; for(int i=1;i<n;i++){ cin>>arr[i]; maxNo=max(maxNo,arr[i]); minNo=min(minNo,arr[i]); } cout<<maxNo<<" "<<minNo; return 0; }
22nd Feb 2021, 10:54 AM
visph
visph - avatar
+ 3
Let's assume <n> is 5 1. Read value for 1st element (arr[0]) 2. Assign that value for <minNo> and <maxNo> 3. Begin the loop while 1 < 5 rather than while 0 < 5 * No change is needed in loop body
22nd Feb 2021, 10:45 AM
Ipang
+ 1
yeah it's working thanks 😁
22nd Feb 2021, 10:56 AM
soni singh
soni singh - avatar