Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ function, smallestIndex, that takes as parameters an int array and i

ID: 3769911 • 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. Also, write a program to test your function. Your task is to finish out the abstraction process below.

#include using namespace std;

//define all global variables here

const int ARRAY_SIZE = 15;

//all function prototype here int main()

{ //Your task is to finish out the abstraction process.

//1. create an array call "list" with integer type and array size const, initialize 15 int numbers

//2. display/print the content of the array cout << "List elements: ";

//3. call your printArray function with parameter here cout << endl;

//4. Display the position of the first occurence of the smallest element in the list

cout << "The position of the first occurence of the smallest element in the list is: ";

//call the smallestIndex function with paramenter

//5. Display "the smallest element in list is: "

cout << "The smallest element in list is : ";

return 0; }

//1. function to print the array

void printArray(int list[], int listSize)

{

for (int index = 0; index < listSize; index++)

cout << list[index] << " "; }

//2. function to find and return the index of smallest element of an array

int smallestIndex(int list[], int listSize)

{

int minIndex = 0;

//assume first element is the smallest

for (int counter = 1; counter < listSize; counter++)

if (list[minIndex] > list[counter]) minIndex = counter;

return minIndex;

}

Explanation / Answer

#include using namespace std; const int ARRAY_SIZE = 15; //all function prototype here void printArray(int *, int ); int smallestIndex(int *, int ); int main() { int list[ ARRAY_SIZE]; cout << "List elements: "; printArray(list, ARRAY_SIZE); cout << endl; cout << "The position of the first occurence of the smallest element in the list is: "<< smallestIndex( list, ARRAY_SIZE); cout<<"the smallest element in list is: "<< list[smallestIndex( list, ARRAY_SIZE)]; return 0; } //1. function to print the array void printArray(int list[], int listSize) { for (int index = 0; index < listSize; index++) cout << list[index] << " "; } //2. function to find and return the index of smallest element of an array int smallestIndex(int list[], int listSize) { int minIndex = 0; //assume first element is the smallest for (int counter = 1; counter < listSize; counter++) if (list[minIndex] > list[counter]) minIndex = counter; return minIndex; }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote