HOW TO FIND THE POSITION OF THE LARGEST ELEMENT

I'm trying to figure out how do you return the position of the largest number? Here is what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
unsigned size = 8;
const unsigned a [] = {3,4,6,1,5,6,10,8};


unsigned maxPos(const unsigned a[], unsigned size)
{

unsigned count=0;
unsigned maxposition = 0;
unsigned compare = 0;



for (count =0; count < size; count++)
{

	

	if (compare < a[count])
	{
	
	maxposition++;
	compare = a[count];
	
	}


}

return maxposition;
}


I have to return 6 because number 10 is the highest number which is in element 6.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
    int numbers[6] = {1,4,3,7,6,5};
    int compare = 0;
    int largest = 0;
    for (int i = 0; i < 6; i ++)
    {
        if (numbers[i] > compare)
        {
            compare = numbers[i];
            largest = i+1; // because array index starts @ 0
        }
    }
    cout << "The largest is : " << largest << " .";
    return 0;

}

Last edited on
KooooOL!! THank you!!!!!
This is what I have so far. I have one more question. Shouldn't it return 0 because the number 300 is the highest and locating at element 0?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

unsigned size = 8;
const unsigned a [] = {300,4,6,1,5,6,1,8};

unsigned maxPos(const unsigned a[], unsigned size)
{

unsigned count;
unsigned maxposition = 0;
unsigned compare = 0;



for (count =0; count < size; count++)
{

	

	if (compare < a[count])
	{
	
	maxposition = count + 1;
	compare = a[count];
	
	}


}

return maxposition;
}
Topic archived. No new replies allowed.