1. Sorting function not working

Why is my sorting function not working?

Code:
```void sort(int array[], int numts)
{
int lowIndex, lowest;

for (int count = 0; count < (numts-1); count++)
{
lowIndex = count;
lowest = array[count];
for (int index = count + 1; index < numts; index++)
{
if (array[index] < lowest)
{
lowest = array[index];
lowIndex = index;
}
}
array[lowIndex] = array[count];
array[count] = lowest;

lowest = array[count];
array[count] = array[lowIndex];
array[lowIndex] = lowest;
}
return;
}```
I am trying everything!

2. Originally Posted by musique

Code:
```		array[count] = lowest;

lowest = array[count];```
I doubt that does what you wanted it to do

3. Originally Posted by musique
Why is my sorting function not working?

Code:
```void sort(int array[], int numts)
{
int lowIndex, lowest;

for (int count = 0; count < (numts-1); count++)
{
lowIndex = count;
lowest = array[count];
for (int index = count + 1; index < numts; index++)
{
if (array[index] < lowest)
{
lowest = array[index];
lowIndex = index;
}		}
array[lowIndex] = array[count];
array[count] = lowest;

lowest = array[count];
array[count] = array[lowIndex];
array[lowIndex] = lowest;
}
return;
}```
I am trying everything!
I think you need to swap the elements here using a third variable.

4. Code:
```		lowest = array[count];
array[count] = array[lowIndex];
array[lowIndex] = lowest;```
It could be that this part is superfluous.

This should be called Selection Sort. At each iteration you find the lowest value in the remaining range and swap that with the first item in the range.

It seems to me that this already does the last part:
Code:
```		array[lowIndex] = array[count];
array[count] = lowest;```

5. Thanks for the reply ben10 but what do you mean? As in array [count]?

6. Thank you for the reply anon. I tried it but it didn't work. Should incorporate pointers?

7. Originally Posted by musique
Thanks for the reply ben10 but what do you mean? As in array [count]?
If you find a number to be greater than the previous one you don't do anything but if the reverse happens i.e if you find a number which is smaller than the previous number, you have to swap them. And swapping is generally done using a third variable.

8. Originally Posted by BEN10
If you find a number to be greater than the previous one you don't do anything but if the reverse happens i.e if you find a number which is smaller than the previous number, you have to swap them. And swapping is generally done using a third variable.
That's because he's implementing selection sort, not bubble sort.

9. And swapping is generally done using a third variable.
But you see, there is a third variable there, and it is called lowest. (Both you and the OP may have a problem getting "attached" to certain patterns in the code and not recognizing their variations )

Thank you for the reply anon. I tried it but it didn't work. Should incorporate pointers?
I don't understand. I just utilized the delete key and it seems to be working to me.

Code:
```void sort(int array[], int numts)
{
int lowIndex, lowest;

for (int count = 0; count < (numts-1); count++)
{
lowIndex = count;
lowest = array[count];
for (int index = count + 1; index < numts; index++)
{
if (array[index] < lowest)
{
lowest = array[index];
lowIndex = index;
}
}
array[lowIndex] = array[count];
array[count] = lowest;
}
return;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

int main()
{
std::vector<int> vec;
std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(vec));
sort(&vec[0], vec.size());
std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
}```

10. Yup, I just tried moving the show array function to see if it just was not displaying and it was just that, it is working just was not being displayed, thanks. I just need to add pointers to it now, but when I do that all hell breaks lose and I get this error (error C2664: 'showArray' : cannot convert parameter 1 from 'int *' to 'int *[]')...example...

Code:
```/***This program dynamically allocates an array large enough to
hold a user-defined number of test scores***/
#include <iostream>
#include <iomanip>
using namespace std;

//Function prototypes
int getnumtestscores();
void gettestscores(int[], int);
void showArray(int *[], int);
void sort(int [], int);
double average(int[], int);

int main()
{
int numts = getnumtestscores();

int *testscores = new int[numts];		//To dynamically allocate an array

gettestscores(testscores, numts);

cout << "The testscores are: \n"; showArray (testscores, numts);

cout << "The testscores sorted are: \n"; sort (testscores, numts);

showArray (testscores, numts);

cout << "The average of all " << numts << " test scores is: "
<< fixed << showpoint << setprecision(2)
<< average(testscores, numts);
cout << endl;

// Free dynamically allocated array in memory
delete [] testscores;
// Make scores point to null
testscores = 0;

return 0;
}

int getnumtestscores()
{
int num;

cout << "\n How many test scores do you want to enter : ";
for(;;) // loop forever  ... until return
{
cin >> num;

return num;
}
}

void gettestscores(int testscores[], int numts)
{
cout << "What are the testscores for the students?\n";

for (int count = 0; count < numts; count++)
{
cout << "Testscore " << setw(2) << (count+1) << ":";

if (testscores < 0)		//Input validation
{
cout << "An invalid score was entered, please enter a score more than 0\n";
}

cin >> testscores[count];
}
}

void sort(int *array[], int numts)
{
int lowIndex, *lowest;

for (int count = 0; count < (numts-1); count++)
{
lowIndex = count;
lowest = array[count];

for (int index = count + 1; index < numts; index++)
{
if (*(array[index]) < *lowest)
{
lowest = array[index];
lowIndex = index;
}
}
array[lowIndex] = array[count];
array[count] = lowest;

}

return;
}

void showArray(int array[], int numts)
{
for (int count = 0; count < numts; count++)
cout << array[count] <<" \n";
cout << endl;
}

double average(int array[], int numts)
{
int total = 0;

for (int count = 0; count < numts; count++)

total +=array[count];

}```

11. Are you wanting to sort an array of pointers to things, or are you wanting to use a pointer to an array? (The later being totally unnecessary since the array is a pointer when passed to sort).

12. I was told in order to understand pointers I should try to use it instead of arrays. I don't understand pointers so in order to see how they function I was trying to alter my currently working program with pointers. That is what I want to see...how the pointers are exactly the same as the array.

13. Code:
```void showArray(int array[], int numts)
void showArray(int* array, int numts)```
These two are the same. When you pass an array to a function, it becomes a pointer to the first element.

Code:
```cout << array[count] <<" \n";
cout << *(array + count) << '\n';```
These two are also the same. The [] operator is just short-hand for pointer arithmetic. Although in a more pointer-oriented approach you'd keep incrementing it, instead of adding some offset to each access:

Code:
```void showArray(int* array, int numts)
{
int* end = array + numts;
for (; array != end; ++array)
cout << *array <<" \n";
cout << endl;
}```

14. Hmmm, let me try it and see, thanks for your help anon.