How to find largest number in array

Does anybody have any ideas on how to find the largest number in an array, but keep the location it's stored in? For instance, I want to find the largest number in array "frequency[8]", and still know whether it's stored in "frequency[0]", "frequency[1]" and so on.


Last edited on
The function max_element() does that in C++.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int frequency[8] = {1,2,3,10,5,6,7,-8};
    int* p = max_element(frequency, frequency+8);
    cout << "The largest number is " << *p
         << " and it is in frequency[" << (p - frequency) << "]\n";
}        

demo: http://ideone.com/EYIfwi
Got it - also, since I'm still kind of a beginner, what's with the "*" symbol before variable p in line 6/7? Also, how would you suggest reading from that? I also need to then set the value of an integer to where the value of "frequency" is saved. (For example, I need to make integer "p1turn" where the highest number is stored - from the code sample above, that would be in "frequency[3]" so p1turn = 3.
Last edited on
It means 'p' is a pointer to a integer. You must have almost just began C++, as it is very common for average users to use pointers.
http://www.cplusplus.com/doc/tutorial/pointers/
Topic archived. No new replies allowed.