Why won't my selection sort, sort the code but displays 200 zeros instead

Hello all, can someone explain why my program displays the numbers in an ordely fashion when i run it and type in the file, but the following line says: (at bottom of post) it fits on the screen fine but the website writes it weird for some reason)


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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  #include <iostream>
#include <fstream>

using namespace std;

// function prototypes
int binarySearch(const int [], int, int);
void selectionSort(int[], int);
void swap(int &, int &);

const int SIZE = 200;


int main()
{
    const int ARRAY_SIZE = 200; // Array size
    int numbers[ARRAY_SIZE];    // array with 200 elements
    int count = 0;              // Loop counter variable
    ifstream inputFile;         // Input file
    string name, filename;
    int number;
    int values[ARRAY_SIZE] = {};


    std::string fileNames[]
    {
        "numbers.txt",
        "File2.txt",
        "File3.txt"
    };

    std::cout << "\nFile names available:\n";

    for (int idx = 0; idx < sizeof(fileNames)/sizeof(fileNames[0]); idx++)
    {
        std::cout << ' ' << fileNames[idx] << '\n';
    }

    // prompt user to enter file name. also display if it exists or not
    // Get file name from the user
    cout << "\nEnter the file name: ";
    cin >> filename;

    // open the file
    inputFile.open(filename);

    if (inputFile)
    {
        // Read the numbers from the file and display them.
        while (count < ARRAY_SIZE && inputFile >> numbers[count])
        count ++;
        {
            cout << number << endl;
        }

        // close the file.
        inputFile.close();
    }
    else
    {
        // Display an error message.
        cout << "Error opening the file.\n";
    }

    // display the numbers read:
    cout << "The numbers are: ";
    for (count = 0; count < ARRAY_SIZE; count++)
    cout << numbers[count] << " ";
    cout << endl;

    // sort the array
    selectionSort(values, ARRAY_SIZE);

    // Display the sorted array.
   cout << "The sorted values:\n";
   for (auto element : values)
      cout << element << " ";
   cout << endl;



    return 0;
}



void selectionSort(int array[], int size)
{
   int minIndex, minValue;

   for (int start = 0; start < (size - 1); start++)
   {
      minIndex = start;
      minValue = array[start];
      for (int index = start + 1; index < size; index++)
      {
         if (array[index] < minValue)
         {
            minValue = array[index];
            minIndex = index;
         }
      }
      swap(array[minIndex], array[start]);
   }
}

//***************************************************
// The swap function swaps a and b in memory.       *
//***************************************************
void swap(int &a, int &b)
{
   int temp = a;
   a = b;
   b = temp;
}
Last edited on

File names available:
 numbers.txt
 File2.txt
 File3.txt

Enter the file name: numbers.txt
0
The numbers are: 156 106 109 3 124 12 52 33 27 66 164 79 200 60 85 82 35 44 180 133 97 117 152 174 1 69 24 118 112 127 129 65 126 104 175 197 63 120 36 151 172 19 113 47 38 132 39 74 56 185 137 67 123 168 21 95 182 2 116 155 107 165 68 7 30 192 139 18 26 16 199 170 110 6 138 121 64 51 163 57 23 80 84 4 13 54 122 20 119 194 53 176 187 40 25 76 58 130 167 115 183 111 193 190 140 22 134 15 166 198 131 29 61 114 90 77 96 73 32 45 94 147 42 71 78 8 135 195 159 46 161 14 41 142 153 178 105 136 11 43 62 93 181 9 75 37 49 146 55 87 143 92 89 86 189 5 141 186 144 169 81 158 150 148 83 17 179 28 103 48 177 10 72 128 125 108 171 173 34 31 154 149 145 91 101 100 188 184 98 157 191 102 88 196 162 70 50 59 160 99
The sorted values:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Process returned 0 (0x0)   execution time : 4.395 s
Press any key to continue.

line 72: You pass your (empty) array 'values' into your sorting function, instead of array 'numbers'

:)
line 72: You pass your (empty) array 'values' into your sorting function, instead of array 'numbers'

:)

I just tried that and got the same exact result
You need to change your code at line 76 too.
Topic archived. No new replies allowed.