Sort Array Function

So arrays are not my strong point and once again I call upon this forum.
The output of the code should look something like this:
6 10 20 5 30 15 25
The original numbers are:
[0] 10
[1] 20
[2] 5
[3] 30
[4] 15
[5] 25
The sorted numbers are:
[0] 5
[1] 10
[2] 15
[3] 20
[4] 25
[5] 30
//
using the sort function we take an array and output the original numbers then output the numbers sorted. So far all we've managed to do is get wild answers or none at all.

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
54
55
56
57
58
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;


void sort(int numbers[], int count)
{
    for(int i = 0; i < count - 1; i++)
    {
        int currentMin = numbers[i];
        int currentMinIndex = i;

        for(int j = i + 1; j < count; j++)
        {
            if(currentMin > numbers[j])
            {
                currentMin = numbers[j];
                currentMinIndex = j;
            }
        }
           
            if(currentMinIndex != i){
                numbers[currentMinIndex] = numbers[i];
                numbers[i] = currentMin;

            }
        }
}

int main()
{
    int num = 100;
    int numbers[int num];
    int count;
    cin >> count;

    cout << "The original numbers are: " << endl;
    for(int i = 0; i < count; i++)
    {
      cout << "[" << i << "]" << ' ';
      cin >> numbers[i];
      cout << endl;
    }
    cout << endl;

    cout << "The sorted numbers are: " << endl;

    for(int i = 1; i < count; i++){
        if(numbers[i] != numbers[i - 1]){
            cout << numbers[i] << " ";

            }
        }


}


Tips, tricks, codes, and hints all helpful!
Well since you're using namespace std and including <algorithm> you need to name your "sort" function something else. There is already a function named sort in the standard namespace and prototyped in <algorithm>.

Second your "swap" looks incorrect. You may want to review your documentation for your sort. What type of sort are you trying to accomplish?

Sort Array Function

Write a program that will read in a set of numbers, print them out as is, and then print them back out in sorted assending order.

You should create a function called: void sort(int numbers[], int count)

This function should sort the given int number array into assending order. Use the selection sort algorithm from the book.

The input consists of a count number, followed by that many data numbers.

Sample run:

6 10 20 5 30 15 25
The original numbers are:
[0] 10
[1] 20
[2] 5
[3] 30
[4] 15
[5] 25
The sorted numbers are:
[0] 5
[1] 10
[2] 15
[3] 20
[4] 25
[5] 30


This is literally all he gave us to work with. And I'm getting most of the function code from a classmate who's taken this class before. We're struggling.
This is literally all he gave us to work with.

Seems adequate to me. Have you read the book as suggested?
Use the selection sort algorithm from the book.


There is also a lot of information on the net about sorting, have you done any searches for "selection sort"?

The original numbers are:

The instructions say to read in the numbers. To me this suggests possibly reading the values from a file, not necessarily getting them from the user.

Also you need to review your book on how to declare arrays, the following is incorrect.
1
2
    int num = 100;
    int numbers[int num];

First in C++ arrays must be compile time constants, second you have two variables in the braces, only one is required.

Topic archived. No new replies allowed.