Answer to Question #181181 in C++ for luna

Question #181181

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.



1
Expert's answer
2021-04-14T03:29:42-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS