Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include / * Program sorts an array of integers using a selection sort. The general algorithm repeatedly finds the smallest number in the array

#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()
{
// 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?
34172644128172206244
[0][1][2][3][4][5][6][7][8][9]
2. What is the point of the assignment small_index = start_index; at the beginning of find_small_index?
3. In the control for the for loop in find_small_index, index does not start at zero as it does in many of the loops that we have written. It starts at different places, different times that it is called. Why is this?
4. 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).
5. 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.
6. Write the change that you would make to the code here.
Submit :
1. The answers to the above questions
2. cpp version of the code.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Design a health and safety policy.

Answered: 1 week ago