Replace your line for(int i=1;i<N-1;i++)
with for(int i=1;i<N;i++)
Because you are otherwise not editing the value of the last value in the array. This means that this (possibly very large) number is throwing off your calculations.
#include <iostream>
int max(int array[], int elements)
{
int max_num = 0;
for(int i = 0; i < elements; i++)
{
if(array[i] > max_num)
{
max_num = array[i];
}
}
return max_num;
}
int main()
{
int N;
int input;
int maximum;
int myarray[50];
std::cout << "Enter no. of elements (not greater than 50): ";
std::cin >> N;
std::cout << "Enter the integers:" << std::endl;
for(int i = 0; i<N; i++)
{
std::cin >> input;
myarray[i] = input;
}
maximum = max(myarray, N);
std::cout << "The maximum is: " << maximum << std::endl;
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int N ,num[50],max=0;
cout<<"Enter the number of integers :";
cin>>N;
cout<<"Enter the integers: ";
//cin>>num[0];
//max=num[0];
for(int i=0;i<N;i++)
{cin>>num[i];//}
//for(int i=0;i<N;i++)
//{
if(num[i]>max)
max=num[i];}
cout<<max;
return 0;
}