# position of max

This is a discussion on position of max within the C++ Programming forums, part of the General Programming Boards category; i have written this code to print the position of max in the array. but how can i find all ...

1. ## position of max

i have written this code to print the position of max in the array.
but how can i find all the positions of max? (3.5.9).

Code:
```#include<stdio.h>
main()
{
int array[10]={4,5,6,9,8,9,4,1,2,9};
int max,pos;

max=array[0];

for(int i=1;i<10;i++)
if(array[i]>max)
{
max=array[i];
pos=i;
}
printf("position:%d",pos);
}```

2. I believe you need a two pass solution since the array is unsorted: first find the max value, then iterate over the array and print each position whose value matches the max value.

3. Keep an array of the positions of the largest value so far. If you find a greater one, clear it and start storing the positions over again.

4. Originally Posted by laserlight
I believe you need a two pass solution since the array is unsorted: first find the max value, then iterate over the array and print each position whose value matches the max value.
you mean this:
Code:
```#include<stdio.h>
main()
{
int array[10]={4,5,6,9,8,9,4,1,2,9};
int max;

max=array[0];

for(int i=1;i<10;i++)
if(array[i]>max)
max=array[i];

for(int j=0;j<10;j++)
if (array[j]==max)
printf("\nposition:%d",j);
}```

5. Originally Posted by -chr-
you mean this:
Roughly yes, but with slightly better indentation, and declaring main to return an int and including <cstdio> instead of <stdio.h>.

A single pass solution is possible by storing the currently known positions of the currently known maximum in a std::vector or array (i.e., what anon later suggested - I was unable to correct my post earlier due to network problems).

6. Originally Posted by anon
Keep an array of the positions of the largest value so far. If you find a greater one, clear it and start storing the positions over again.
i 'll try it although i find it a bit difficult.

7. You may want to use a std::vector for the positions since you don't know how many there will be.