position of lowest and highest values of array

I have to find the position of the lowest and highest values of an array.The array starts at 0.For example:
INPUT:5
8 7 9 2 5
OUTPUT: 4 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{ int n, a[1000],i, min=999999, max=0;
int *imin,*imax;
 cin>>n;
 for(i=0;i<n;i++)
    cin>>a[i];
 for(i=0;i<n;i++)
  if(a[i]<min)
   {min=a[i];
   imin=&a[i];}
  for(i=0;i<n;i++)
    if(a[i]>max)
      {max=a[i];
      imax=&a[i];}
    cout<<imin<<" "<<imax;
    return 0;
}

  

I can't understand what I'm doing wrong since my program displays 0x22ef64 0x22ef60 for the values I mentioned before.
I think you want:
 
imax=i;
I know it works like that, but I wanted to implement pointers into my exercises.
Oh, I see. You still need an index. How about something like:

1
2
3
4
5
6
7
8
9
int* ap=a; //decl pointer, same as int* ap=&a[0]
...
for(i=0; i<n; i++) {
  if(*ap < min) {
      min = *ap; //dereference
      imin = i;
   }
   ap++;      //next element
}
Last edited on
omg I can't believe I missed that! Thank you so much!!!
Registered users can post here. Sign in or register to post.