returning a value

I can't figure how to arrange my for loop to return the index value.

As is now, after user date input,
ex:
0 11
1 14
2 33
4 9
5 27
my minIndex returns the value of 9.
I need it to return the index of 4 (which is the index for 9).


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

using namespace std;

void getlist (int [], int &);
void putlist (const int [], int);
int minIndex (const int [], int);

const int MAXSIZE = 50;
int i, min, index;
int mylist [MAXSIZE];

int main ( void )
{
    
    getlist (mylist, index);
    putlist (mylist, index);
                
    cout << "The minimum value index is " << minIndex (mylist, index) << endl;
                     
    system ("PAUSE");
    return 0;
}
void getlist (int mylist[], int & index)    
{
    
    cout << "Enter the number of Array Elements, Maximum " << MAXSIZE << endl;
    cin >> index;
    
    for (i = 0; i < index; i ++)
        {
        cout << "Enter the next array element\n";
        cin >> mylist [i];
        }
}

void putlist (const int mylist[], int index)
{    
    cout << "Array elements\n";
    for (i = 0; i < index; i ++)
    cout << i << " " << mylist [i] << endl;
}
int minIndex (const int mylist[], int index)
{       
  
    int i, min = mylist[0];
    
    for (i = 1; i < index; i++)
        if (min > mylist[i])
        min = mylist[i];
    
    return min;

}
Last edited on
You need to store both the minimum value and the index of the minimum value.
Alternatively, you can store only the index of the minimum and then compare to mylist[minIndex].
I'm not sure how to compare the index to mylist[minIindex].

1
2
if (min > mylist[i])
        min = mylist[i];


this finds the lowest value and stores the value.

Now if i store both values, I can print just one.
How can i associate the index to the lowest value?


Last edited on
Topic archived. No new replies allowed.