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

Experiment with the program to see how it works. #include <cstdlib> #include <io

ID: 3787444 • Letter: E

Question

Experiment with the program to see how it works.

#include <cstdlib>

#include <iostream>

/*

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?

The program is hard coded to only work with arrays of 10 numbers. Add a constant called size before main that is assigned the number of elements to be sorted. Use this size constant in the rest of the program so that the sort will work for any number of values. Run your program with size equal to 12 (add 2 more numbers to the array in its declaration list).

A simple change to the program will sort the numbers from largest to smallest. Change your program so that it does this. Do not print the sorted values in reverse order! Actually change the sort.

34

17

26

44

12

81

72

20

62

44

Explanation / Answer

Please find the required solutions:


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]

Solution: find_small_index will return 4
Explanation:
initial values small_index=0;
iteration-1 : index = 1
numbers[1]<numbers[0]
17<34(true)
small_index=1
iteration-2 : index = 2
numbers[2]<numbers[1]
26<17(false)
small_index=1
iteration-3 : index = 3
numbers[3]<numbers[1]
44<17(false)
small_index=1
iteration-4 : index = 4
numbers[4]<numbers[1]
12<17(true)
small_index=4

iteration-5 : index = 5
numbers[5]<numbers[4]
81<12(false)
small_index=4

iteration-6 : index = 6
numbers[6]<numbers[4]
72<12(false)
small_index=4

iteration-7 : index = 7
numbers[7]<numbers[4]
20<12(false)
small_index=4

iteration-8 : index = 8
numbers[8]<numbers[4]
62<12(false)
small_index=4

iteration-9 : index = 9
numbers[9]<numbers[4]
44<12(false)
small_index=4


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.

Solution: false
Explanation:
initial values small_index=0;
iteration-1 : index = 1
numbers[1]<numbers[0]
17<34(true)
small_index=1
iteration-2 : index = 2
numbers[2]<numbers[1]
26<17(false)
small_index=1
iteration-3 : index = 3
numbers[3]<numbers[1]
44<17(false)
small_index=1



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?

Solution:Initialize assuming first number is small. the following statement is used




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?

Solution: Array values are sorted.


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).

Solution: Because array is sorted in the previous iteration itself



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

Solution: Because array numbers (parameter of swapper is int)



The program is hard coded to only work with arrays of 10 numbers. Add a constant called size before main that is assigned the number of elements to be sorted. Use this size constant in the rest of the program so that the sort will work for any number of values. Run your program with size equal to 12 (add 2 more numbers to the array in its declaration list).

#include <cstdlib>
#include <iostream>
/*
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 size =12;
int main(int argc, char *argv[])
{
// array of numbers
int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41,89,99};
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 < size-1)
{
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 < size; 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;
}


A simple change to the program will sort the numbers from largest to smallest. Change your program so that it does this. Do not print the sorted values in reverse order! Actually change the sort.

In find_small_index index comparision is made using <
If we make comparision using > array will be sorted in descending order (largest to smallest).

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