jlb,
What are you trying to pass to this function? An array of double? A pointer to an array of double? |
I was indeed trying to pass an array to the function and not a pointer to the array.
Next why are you attempting to use pointer notation instead of the "normal" and clearer array notation: |
I am using the pointer notation as much as possible in order to practice them, as suggested
by the instructions for the program: "Use pointer notation rather than array
notation whenever possible." This program has no purpose for me other than getting familiar
with pointers and the way one uses them.
Where are you allocating memory for that pointer (minValue)? Wouldn't a normal double be adequate? |
You're right a double makes more sense for
minValue
since I don't have to do
anything with its address.
Why the subtraction in the comparison part of your for() loop in this function? |
The fonction selectionSort() sorts the different doubles contained in arrPtr[] in ascending order.
It goes through arrPtr[0] through arrPtr[ num-1 ] -with the for() loops- and assigns the first smallest
double to arrPtr[0], the second smallest double to arrPtr[1] etc.. I decided to run the loop only until
the second to last element of the array, since if all elements but one have been sorted in ascending
order, the last one will inevitably be the highest double in this case.
Lastly you should avoid mixing C-stdio functions with C++ streams. In a C++ program prefer C++ streams since they are less error prone. |
Thank you for the advice about C-stdio functions, what else do you recommend using? (I only use them for my void pause() function in every program so far).
I managed to fix my code by the way, thanks to your remarks, here is the updated version:
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
|
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
void pause();
void selectionSort( double [], int);
//int average();
int main()
{
double *arrayPtr;
double average;
int testNum;
cout << fixed << showpoint << setprecision(1);
cout << "\nEnter the number of tests: ";
cin >> testNum;
cout << "\n\n";
arrayPtr = new double[testNum];
for (int count = 0; count < testNum; count ++)
{
cout << "\tResult for test " << count + 1 << ": ";
cin >> *(arrayPtr + count);
}
selectionSort(arrayPtr, testNum);
cout << "\n\nResults in ascending order: ";
for (int count = 0; count < testNum; count ++)
{
cout << *arrayPtr << " ";
arrayPtr++;
}
//average(arrayPtr, testNum);
cout << "\n\nAverage for results: "
<< average;
pause();
}
void selectionSort(double arr[], int num)
{
int startScan,
minIndex;
double minValue;
for (startScan = 0; startScan < (num - 1); startScan ++)
{
minIndex = startScan;
minValue = arr[startScan];
for (int index = (startScan + 1); index < num; index ++)
{
if (arr[index] < minValue)
{
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}
//void average() {}
void pause()
{
printf("\n%s","Press any key to continue");
_getch();
printf("\n\n");
return;
}
|
Thank you again!