What will the value return?

If this program was replaced with the following array: [34, 17, 26, 44, 12, 81, 72, 20, 62, 44] what value will find_small_index return?

#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 << "\n\nThe sorted array is:\n";
for (index = 0; index < 10; index++)
cout << numbers [index] << " ";
cout << "\n\n";

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;
}
It looks to me like find_small_index() returns the index of the smallest value in the numbers array between numbers[start_index] and numbers[9].
closed account (48T7M4Gy)
9
Have you considered looking at existing sort algorithms ?
Topic archived. No new replies allowed.