In: Computer Science
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.
The main function should print the smallest number and the index of the smallest number.
SOURCE CODE IN C++:
#include <iostream>
using namespace std;
//function to return the first index of the smallest number in the array
//arr[]: array to search minimum in
//n: size of the array
int smallestIndex(int arr[], int n)
{
int min=0;
for(int
i=1; i
{
if(arr[i]
min=i;
}
return min;
}
int main()
{
int arr[10]; //creating the array
for(int i=0; i<10; i++)
{
cout << "Enter element #" << (i+1) << ": "; //input prompt
cin >> arr[i]; //input
}
int min=smallestIndex(arr, 10); //finding smallest index
cout << "The smallest number is " << arr[min] << " at index " << min << "\n"; //output
return 0;
}
OUTPUT: