Write a C++ function, smallestIndex, that takes as parameters an int array and i
ID: 3759862 • Letter: W
Question
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. Write another C++ function,lastLargestIndex, that takes as parameters an
int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write a driver to test your functions, compile it and test the program. Turn in your analysis and design for lastLargestIndex and your source code.
Explanation / Answer
#include <iostream>
using namespace std;
int smallestIndex(int arrayS[], int arraySizeS);
int lastLargestIndex(int arrayL[], int arraySizeL);
int main()
{
const int MAX_ARRAY_SIZE = 15;
int test[MAX_ARRAY_SIZE] {16, 22, 4, 17, 21, 84, 43, 62, 53, 24, 35, 13,
29, 16, 29};
int smallestPosition = smallestIndex(test, MAX_ARRAY_SIZE);
cout << "The smallest index position is: " << smallestPosition << ".";
cout << "The value of the smallest index is: " << test[smallestPosition] << ".";
int largestPosition = lastLargestIndex(test, MAX_ARRAY_SIZE);
cout << "The largest index position is: " << largestPosition << ".";
cout << "The value of the largest index is: " << test[largestPosition];
system("pause");
return 0;
}
int smallestIndex(int arrayS[], int arraySizeS)
{
int smallest = 0;
for (int i = 0; i < arraySizeS - 1; i++)
{
if (arrayS[i] < arrayS[smallest]) smallest = i;
}
return smallest;
}
int lastLargestIndex(int arrayL[], int arraySizeL)
{
int largest = 0;
for (int ii = 0; ii < arraySizeL; ii++)
{
if (arrayL[ii] >= arrayL[largest]) largest = ii;
}
return largest;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.