Question

In: Computer Science

Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

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 write our commands in the middle:

Write a C++ Program:

#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

    …

    …

}

Solutions

Expert Solution

I have completed the code in C++

#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};                                  
    
    printArray(list, ARRAY_SIZE);
     
    // printing the index of smallestElement 
    cout<<"\nThe index of the first occurence of smallest element is: "<<smallestIndex(list, ARRAY_SIZE);
    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){
    int smallestElement=1000000;
    
    // finding the smallestElement
    for(int i = 0; i<ARRAY_SIZE; i++){
        if(list[i]<smallestElement)
            smallestElement=list[i];
    }
    
    // finding the first occurence of smallestElement
    for(int i = 0; i<ARRAY_SIZE; i++){
        if(list[i]==smallestElement)
            return i;
    }
}

Here the smallest element is 15 and its index is 8 so the function is working perfectly fine.

i have added comments so that you can understand better.

If you like the answer then please upvote.


Related Solutions

Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
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...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a function called printChList that takes as its parameters a character array, its size, and...
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
C Write a function that takes as input an array of int pointers, multiplies the ints...
C Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If...
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT