Desperate help with my code

I'm having trouble with my code, how would I change the sort to largest to smallest?

Also, I was told that I have to change const int size = 12 and replace all reference to size, but I have no idea how to do that, nor where to even start.

If anyone can help, would be great.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  #include <cstdlib>
#include <iostream>
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;
}
1)
You are using a selection sort: for each element of the array, find the smallest element in the remainder of the array (inclusive) and swap it with the current element.

To sort in the other direction, find the largest element in the remainder of the array (inclusive).

So, ideally, you should have a function called find_large_index().

2)
You are using a fixed-size array (ten elements), and everything in your code only works with that specific-size of array (ten elements).

You can fix this by changing your array definition thus:

11
12
  constexpr size = 10;
  int numbers [size] = {7, 9, ...};

Your functions also need another argument:

34
35
int find_small_index (int start_index, int numbers [], int size)
{

Everywhere you have a hard-coded number ("10" or "9") you need to replace it with an equivalent "size" expression. For example, 10 should be written as size.

And, finally, when you call your function, make sure to indicate the array's size:
21
22
         small_index = find_small_index (start_index, numbers, size);
         ...

Hope this helps.
Last edited on
Topic archived. No new replies allowed.