Write a function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function.
#include <iostream>
using namespace std;
int minIndex(int val, int* array, int size)
{
for (int i = 0; i < size; i++)
{
if (array[i] == val)
{
return i;
}
}
return -1;
}
int main()
{
int A[10] = { 1, 2, 3, 5, 6, 9, 5, 10, 5, 8 };
cout << "The min index of 5 value in array is: " << minIndex(5, A, 10) << endl;
return 0;
}
Comments
Leave a comment