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.
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.
(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...
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array using a template. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an integer n and returns 1 if n is a perfect number, otherwise it will return 0. If the sum of a number’s proper divisors are equal to the number, than the number is called a perfect number. For example, 6 is a perfect number: 6=1+2+3.
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT