Question

In: Computer Science

Please provide your answer in the following format Write a C++ function, smallestIndex, that takes as...

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

    …

    …

}

Solutions

Expert Solution

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


Related Solutions

in c++, please provide only a function. the function should be named "tally." it takes no...
in c++, please provide only a function. the function should be named "tally." it takes no parameters but returns an int. the first int should be 0, next being 1, then 2, etc.. will rate if correct. again should only be 1 function.
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
Write a C function to implement operation of a stack using the following format: /** *...
Write a C function to implement operation of a stack using the following format: /** * function: *       push * * expects: *       pointer to the stack *       pointer to the size *       the value to push * * returns: *     true when value has been pushed *       false otherwise * * The push function push a value to the passed in stack */ bool push(int *stack, int *size, int max_size, int to_push) {...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
use linux or c program. please provide the answer in details. Write a program that will...
use linux or c program. please provide the answer in details. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT