'high' is outside the loop
#include <iostream>
using namespace std;
int main(){
int n,high;
cin >> n;
int arr[n]={};
for(int i=0; i<n; i++){
cin >> arr[i];
}
high = arr[0];
for(int i=0; i<n; i++){
if(high<arr[i]){
high = arr[i];
}
}
cout << "High ==> " << high << endl;
return 0;
}
'high' is inside the loop
#include <iostream>
using namespace std;
int main(){
int n,high;
cin >> n;
int arr[n]={};
for(int i=0; i<n; i++){
cin >> arr[i];
}
for(int i=0; i<n; i++){
high = arr[0];
if(high<arr[i]){
high = arr[i];
}
}
cout << "High ==> " << high << endl;
return 0;
}
Here is my input,
4
2 5 6 4
If I use high = arr[0];
outside the loop then output is High ==> 6
, which is correct. But if i use high = arr[0];
inside the loop then, output is High ==> 4
, which is incorrect.
But, why I should not expect High ==> 6
while I used it inside or how this High ==> 4
is coming?