I want to find the max number in an array. But my code show me unexpected answer.
is there any wrong in my code?
Here's my code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter array size: ";
cin>>n;
int a[n];
int maximum = 0;
maximum=a[0];
for(int i=0; i<n; i++)
{
cin>>a[i];
if(maximum<a[i])
{
maximum=a[i];
}
}
cout<<"Maximum number: "<<maximum;
return 0;
}
Here my input:
Enter array size: 3
4 5 6
Output:
16
My expected output must be 6
is there any wrong in my code?