#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.
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.