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

Update the code from the questions if necessary. #include #include /* Program so

ID: 3787950 • Letter: U

Question

Update the code from the questions if necessary.

#include

#include

/*

Program sorts an array of integers using a selection sort.

The general algorithm repeatedly finds the smallest number

in the array and places it at the front of the list.

*/

using namespace std;

int find_small_index (int start_index, int numbers []);

void swap_values (int index1, int index2, int numbers []);

int main(int argc, char *argv[])

{

    // array of numbers

    int numbers [10] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41};

    int start_index; // current starting spot for search

    int small_index; // index of the smallest number in the array

    int index;        // index used for print the array values

   

    start_index = 0;

    // continue finding the smallest value and placing it

    // at the front of the list

    while (start_index < 9)

    {

          small_index = find_small_index (start_index, numbers);

          swap_values (small_index, start_index, numbers);

          start_index++;

    }

   

    cout << " The sorted array is: ";

    for (index = 0; index < 10; index++)

        cout << numbers [index] << " ";

    cout << " ";

         

    return 0;

}

int find_small_index (int start_index, int numbers [])

{

    int small_index, // smallest index to be returned

        index;       // current index being viewed

   

    small_index = start_index;

    for (index = start_index + 1; index < 10; index++)

        if (numbers [index] < numbers [small_index])

           small_index = index;

    return small_index;

}

   

void swap_values (int index1, int index2, int numbers [])

{

     int swapper;

    

     swapper = numbers [index1];

     numbers [index1] = numbers [index2];

     numbers [index2] = swapper;

}

1. What value would find_small_index return for the following array?

34

17

26

44

12

81

72

20

62

44

[0]    [1] [2]    [3]   [4]   [5]    [6]   [7]   [8] [9]

2. Assume that the array in question 1 is being used, will the value of the Boolean expression in the if statement in find_small_index be true or false when index is equal to 3 in the for loop? Explain your answer.

3.What is the point of the assignment small_index = start_index; at the beginning of find_small_index? How does this help the function to accomplish its goal?

4. start_index is increased by 1 each time through the loop in main. When find_small_index is called with start_index equal to 5, what must be true about the array values in indexes 0 through 4?

5. In the while loop in main, start_index only goes up to 8 (start_index < 9). Explain why the loop does not need to run when start_index equals 9 (the last index in the array).

6. In swap_values, swapper is declared as an int type. Why?

34

17

26

44

12

81

72

20

62

44

Explanation / Answer

1). What value would find_small_index return for the following array?

34

17

26

44

12

81

72

20

62

44

[0]    [1] [2]    [3]   [4]   [5]    [6]   [7]   [8] [9]

Answer : find_small_index would return 4 for the first time this function is called

3.)What is the point of the assignment small_index = start_index; at the beginning offind_small_index? How does this help the function to accomplish its goal?

Answer : This function finds the index of the array which contains the smallest value within the specific range of array. It helps the goal in acheiving sorted array. As, this function finds the index of the smallest value. We will swap the value to the required position.

4.) start_index is increased by 1 each time through the loop in main. Whenfind_small_index is called with start_index equal to 5, what must be true about the array values in indexes 0 through 4?

Answer : Because after every time find_small_index is called , we get the smallest value. So,if we don't increase the start_index ; we will only get the same index i.e smallest value but never the second smallest value .So, we will be unable to sort the array. Due to which start_index is increased so that now the find_small_index will find the smallest value excluding the previously selected indexes.

Whenfind_small_index is called with start_index equal to 5, what must be true about the array values in indexes 0 through 4 t? Answer : array values in indexes 0 through 4 are all sorted in ascending order

5)In the while loop in main, start_index only goes up to 8 (start_index < 9). Explain why the loop does not need to run when start_index equals 9 (the last index in the array).

Answer : Because when the start index will come to 9.Then, that would be the last value in the array which will automatically be the largest number as the previous all elements are sorted.So, we need not run the loop for that element

6)In swap_values, swapper is declared as an int type. Why?

Answer : Because in swap_values, we swap the values of the array elements.And , as the array is declared as int and we are swapping the values of that array only which contains integer values. Therefore,the swapper is also declared as int type

34

17

26

44

12

81

72

20

62

44

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