Sorting array using selection method

Hi everyone! So as the title says, I was instructed to write a program that takes an array from the user and sorts the numbers from least to greatest using the selection sort method.

The selection method basically goes through an array comparing indexes with the previous one and checks to see if the value in that index is smaller. If it is smaller, then that value is saved in the current index.

I wanted to keep my explanation short, so if it didn't make sense, it is explained in more detail here: http://cforbeginners.com/ssort.html

This is what I have so far:


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
59
60
61
  /*Write a program that sorts n integers, from smallest to largest, with what’s called the selection sort
method. The input and output should be exactly:

How many numbers do you have?
[USER ENTERS A POSITIVE INTEGER]
Input number (count 1):
[USER ENTERS A INTEGER]
Input number (count 2):
[USER ENTERS A INTEGER]
...
Input number (count X):
[USER ENTERS A INTEGER]
The sorted input is:
X (count 1)
X (count 2)
...
X (count X)*/

#include <iostream>
using namespace std;

int main() {
  int n;


  cout << "How many numbers do you have?" << endl; // get number n from user
  cin >> n;

  int* array = new int[n]; //create dynamic array of size n

  for (count; count <= n; count++)
  {
    for (int i=0; i < count; i++)
    {
      cout << "Input number (count " << count << "):" << endl;
      cin >> array[i]; // store numbers in array
    }
  }

  int min_index=0;

  for (i; i < n - 1; i++)// index of smallest int
  {
    for (int j = i + 1; j < n - 1; j++) //moves to next entry in array while number is not last number
    {
      if (array[i] > array[min_index]) //Find the smallest entry among entries 0 to n - 1
      {
        min_index = i;
        array[i] = array[j];//swap smallest entry with entry 0
        array[j] = min_index; //Output the array
      }

    }
  }

  cout << "The sorted input is: " << endl;
  for (int count=1;count<n;count++)
    cout << array[i] << "(count " << count << ")" << endl; //output array

  delete[] array; //Frees memory that array allocates
}


When I run my program, I just get:

How many numbers do you have?
4 //I input 4
Input number (count 1):
1
Input number (count 2):
4
Input number (count 3):
7
Input number (count 4):
9
The sorted input is: //blank, program ends

First of all, I'm not sure if I'm storing the numbers provided by the user in the array correctly, and second, I'm not sure how to go about printing the arr[i] in the last for loop since it's out of scope.

I just learned about arrays a couple days ago so I'm still struggling a bit, so any help/hints would be greatly appreciated. Thank you!
Last edited on
Your code does not compile. How can you run it?
1
2
3
4
 In function 'int main()':
31:8: error: 'count' was not declared in this scope
42:8: error: 'i' was not declared in this scope
58:19: error: 'i' was not declared in this scope 
Is this assignment due for you tomorrow as well? lol

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
#include <iostream>
using namespace std;

int main() {
  int n;


  cout << "How many numbers do you have?" << endl; // get number n from user
  cin >> n;

  int* array = new int[n]; //create dynamic array of size n

  int count = 1;
  for (int i = 0; i < n; i++)
  { 
    cout << "Input number (count " << count << "):" << endl;
    cin >> array[i];
    count = count + 1;
    /*for (int i=0; i < count; i++)
    {
      //cout << "Input number (count " << count << "):" << endl;
      cin >> array[i]; // store numbers in array
    }*/
  }

  int min_index=0;

  for (int i = 0; i < n - 1; i++)// index of smallest int
  {
    for (int j = i + 1; j < n - 1; j++) //moves to next entry in array while number is not last number
    {
      if (array[i] > array[min_index]) //Find the smallest entry among entries 0 to n - 1
      {
        min_index = i;
        array[i] = array[j];//swap smallest entry with entry 0
        array[j] = min_index; //Output the array
      }

    }
  }
  cout << "The sorted input is: " << endl;
  int count2 = 1;
  for (int i=0;i<n;i++) {
  cout << array[i] << "(count " << count2 << ")" << endl; //output array
  count2 = count2 + 1;
  }

  delete[] array; //Frees memory that array allocates
}


The output:
How many numbers do you have?
3
Input number (count 1):
3
Input number (count 2):
2
Input number (count 3):
1
The sorted input is:
3 (count 1)
2 (count 2)
1 (count 3)


It doesn't look sorted! :(
Topic archived. No new replies allowed.