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).
#include <iostream>
usingnamespace std;
void getlist (int [], int &);
void putlist (constint [], int);
int minIndex (constint [], int);
constint 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 (constint mylist[], int index)
{
cout << "Array elements\n";
for (i = 0; i < index; i ++)
cout << i << " " << mylist [i] << endl;
}
int minIndex (constint mylist[], int index)
{
int i, min = mylist[0];
for (i = 1; i < index; i++)
if (min > mylist[i])
min = mylist[i];
return min;
}
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].