i'm new to c++ and i've been trying to figure out a way of finding out the largest number after inputing > 25 numbers. the use of if..else statements doesnt seem to be viable at all.
any suggestions?
Nandors version won't work, at least not without a slight modification. vctr.end() does not return the last element in the vector, it indicates that you have gone past the last element. To make it work you would have to decrement the iterator by one before trying to dereference it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main()
{
cout<<"Enter 25 numbers";
int input[25];
int aux;
for(aux=0;aux<25;aux++)
{
cin>>input[aux];
}
vector<int>vctr(input,input+25);
vector<int>::iterator it;
sort(vctr.begin(),vctr.end());
it=vctr.end();
it--; // move the iterator back to the last element
cout<<"The highest is:";
cout<<*it;
}
int main()
{
cout<<"Enter 25 numbers\n";
int input[25];
int aux;
int high=0;
for(aux=0;aux<5;aux++)
{
cin>>input[aux];
if(input[aux]>high)
high=input[aux];
}
cout<<"The highest is: ";
cout<<high;
getche();
}