Unknown error with my code

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
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;


void sort(int unsortedArray[], int arraySize, int arraySizeIf, int intSize)
{
    int sortedArray[arraySize];
    int arrayLowestNumber = intSize;
    int arrayLowestNumberPosition;
    int arrayReplacementNumber = intSize + 10;
    for (int i = 0; i < arraySize; i++)
    {
        for(int j = 0; j < arraySize; j++)
        {
            if (unsortedArray[j] < arrayLowestNumber)
            {
                arrayLowestNumberPosition = j;
                arrayLowestNumber = unsortedArray[j];
            }
        }
        sortedArray[i] = unsortedArray[arrayLowestNumberPosition];
        unsortedArray[arrayLowestNumberPosition] = arrayReplacementNumber;
    }
    cout << endl << "SORTED {";
    for (int count = 0; count < arraySize; count++)
    {
        if (!(count == arraySizeIf))
        {
            cout << sortedArray[count];
            cout << ", ";
        }
        else{
            cout << sortedArray[count];
            cout << "}";
        }
    }
}


int main()
{
    srand(time(NULL));
    int arraySize;
    int intSize;
    cout << "Please enter the amount of numbers to be sorted: ";
    cin >> arraySize;
    int arraySizeIf = arraySize - 1;
    cout << "Please enter the maximum size that the numbers can be: ";
    cin >> intSize;
    cout << "UNSORTED: {";
    int unsortedArray[arraySize];
    for (int i = 0; i < arraySize; i++)
    {
        if (!(i == arraySizeIf))
        {
            unsortedArray[i] = rand() % intSize;
            cout << unsortedArray[i];
            cout << ", ";
        }
        else{
            unsortedArray[i] = rand() % intSize;
            cout << unsortedArray[i];
            cout << "}";
        }
    }

    sort(unsortedArray, arraySize, arraySizeIf, intSize);

}


output:
1
2
3
4
Please enter the amount of numbers to be sorted: 5
Please enter the maximum size that the numbers can be: 100
UNSORTED: {43, 50, 79, 43, 89}
SORTED {43, 110, 110, 110, 110}


I know this is probably something very silly, but i've looked over my code multiple times but I just can't seem to find the error.

if someone could point it out of "hint" towards it, I would very much appreciate it.
What is the value of arrayReplacementNumber? Line 14.
It is specified by a user input on line 50 and goes through a function call on line 71
Yes, but do you ever change its value beyond line 14? You set it to intSize+10, then use it in line 26.
Last edited on
Your loop on lines 17-24 checks whether the unsortedArray contains an element smaller than arrayLowestNumber and updates the arrayLowestNumberPosition accordingly. The problem is that after the first completion of that loop the arrayLowestNumber already has the smallest value of the original array.

Move line 12 to between lines 16 and 17.


PS. think about this:
1
2
3
4
5
6
7
8
9
10
11
12
void foo( const int arr[], int size ) {
  cout << '{';
  for (int count = 0; count < size; ++count )
    {
      if ( count )
        {
            cout << ", ";
        }
      cout << array[count];
    }
  cout << '}';
}

What might it do and how?
Topic archived. No new replies allowed.