In: Computer Science
Please provide your answer in the following format
Write a C++ 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.
You must insert the following comments at the beginning of your program and write our commands in the middle:
Write a C++ Program:
/*
// Name: Your Name
// ID: Your ID
// Purpose Statement:~~~
*/
#include <iostream>
using namespace std;
const int ARRAY_SIZE = 15;
void printArray(const int x[], int sizeX);
int smallestIndex(const int x[], int sizeX);
int main()
{
int list[ARRAY_SIZE] = {56, 34, 67, 54, 23, 87, 66, 92, 15, 32, 55, 54, 88, 22, 30};
…
…
YOUR CODE HERE
…
…
return 0;
}
//Function to print the array
void printArray(const int list[], int listSize)
{
int index;
for (index = 0; index < listSize; index++)
cout << list[index] << " ";
}
// Function to find and return the index of the
// smallest element of an array
int smallestIndex(const int list[], int listSize)
{
…
…
YOUR CODE HERE
…
…
}
Program
/*
// Name: Your Name
// ID: Your ID
// Purpose Statement:~~~
*/
#include <iostream>
using namespace std;
const int ARRAY_SIZE = 15;
void printArray(const int x[], int sizeX);//function proto type for printing array elements
int smallestIndex(const int x[], int sizeX);//function prototype for finding index of first occurrence of smallest element
int main()
{
int list[ARRAY_SIZE] = {56, 34, 67, 54, 23, 87, 66, 92, 15, 32, 55, 54, 88, 22, 30}; // initialization array
int index_Smallest; //declaration of integer variable
index_Smallest
cout<<"Array elements:";
/* call the function printArray(list,ARRAY_SIZE) forprinting all
the element in array list */
printArray(list,ARRAY_SIZE);
/*call the function smallestIndex(list,ARRAY_SIZE) for
finding index of first occurrence of smallest element
.function returns corresponding index */
index_Smallest=smallestIndex(list,ARRAY_SIZE);
cout<<endl;
//print the location of first occurrence of smallest
element
cout<<"First occurrence of smallest element
"<<list[index_Smallest]<<" located at array index
"<<index_Smallest<<endl;
}
//Function to print the array
void printArray(const int list[], int listSize)
{
int index;
for (index = 0; index < listSize; index++) //print array element one by one
cout << list[index] << " ";
}
// Function to find and return the index of the
// smallest element of an array
int smallestIndex(const int list[], int listSize)
{
int index=0,smallest,index_Smallest; // declare integer
variables
smallest=list[0]; //assume smallest element as first element of
array -list[0]
index_Smallest=0; // and corresponding index as 0
for(index=0;index<listSize;index++) //traverse array list from
first element to last element
{
if(smallest>list[index]) //if our assumption is not currect for
element list[index]
{
/* then take smallest element as list[index] and store
corresponding index to index_Smallest */
smallest=list[index];
index_Smallest=index;
}
}
return index_Smallest; //return index_Smallest to main function
}
Output
Sample Screen shot of Program