Sorry if this is a really silly question, is there a way of sorting an array, or to be more precise outputting the largest number in an array without using sort ?? The reason I am asking is I have just started learning the basics and I'm not to confident with the different kinds of sort and I didn't want to get into the whole thing of libraries just yet, as that will be covered later on in my syllabus..
#include <iostream>
usingnamespace std;
int main ()
{
constint m=3;
int v[3]={-1, 5,19};
int max=v[0];
int i;
for (i=0; i<m; i++)
if (max<v[i])
max=v[i];
cout<<"The max value in the arryay is: "<<max<<endl;
cin.get(); cin.get();
return 0;
}
Janlan, if you put 0 into max, then you could start at 1...
What Janlan did is correct though
1. grab the first element
2. check the next value, if it's greater, grab that value replacing the one you have
3. repeat 2, until you hit the end of the array...
@ Janlan, this worked a treat but one thing I think I'm getting wrong... when I run my program, it outputs the 1st entry of the array straight away with the console message.... (I think what craniumonempty warned about) I am using >> cin, to input the array entries (from the user) could this be the problem ???