Reputation: 29
I have a homework assignment where I need to modify an older assignment to use templates.
The original assignment called for random sorting of a dynamically allocated array of integers and I need to create a template that extends this to strings and doubles. I'm using a while loop and a switch-case to present a menu to the user, where they can decide which type they're using and to fill in the array.
I've tried to cut this down as much as possible, to make debugging easier. The integer implementation gets no compiler errors, but the double and string variations do.
int* pPresortedInts = NULL; //pointer to array of ints
std::cout << std::endl << "How many integers did you want to sort? ";
std::cin >> sizeOfArray;
pPresortedInts = new int[sizeOfArray]; //dynamically allocate array
//prompt user to fill array
std::cout << std::endl << "Great! Please enter " << sizeOfArray << " numbers: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
std::cin >> pPresortedInts[counter];
}
//output filled array
std::cout << std::endl << std::endl << "Here are the integers in their original order: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
std::cout << std::endl << pPresortedInts[counter];
}
std::cout << std::endl << std::endl;
//shuffle elements and print the result
shuffleSort(pPresortedInts, sizeOfArray);
//Remember to delete dynamically allocated arrays
delete [] pPresortedInts;
pPresortedInts = NULL;
And the double implementation:
double* pPresortedDoubles = NULL; //pointer to array of doubles
std::cout << std::endl << "How many doubles did you want to sort? ";
std::cin >> sizeOfArray;
pPresortedDoubles = new double[sizeOfArray]; //dynamically allocate array
//prompt user to fill array
std::cout << std::endl << "Great! Please enter " << sizeOfArray << " numbers: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
std::cin >> pPresortedDoubles[counter];
}
//output filled array
std::cout << std::endl << std::endl << "Here are the doubles in their original order: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
std::cout << std::endl << pPresortedDoubles[counter];
}
std::cout << std::endl << std::endl;
//shuffle elements and print the result
shuffleSort(pPresortedDoubles, sizeOfArray);
//Remember to delete dynamically allocated arrays
delete [] pPresortedDoubles;
pPresortedDoubles = NULL;
Finally, here's the template itself:
template <class T>
void shuffleSort(T pPresortedArray[], T size) {
T* pShuffledArray = new T[size]; // pointer to dynamically allocated array of a generic type
T randomElement;
T temp;
//fill ShuffledArray with PresortedArray
for (int counter = 0; counter < size; counter++) {
pShuffledArray[counter] = pPresortedArray[counter];
}
for (int counter = 0; counter < size; counter++) {
randomElement = rand()%size; // choose a random element from the ShuffledArray array
//swap that element with the currently selected counter
temp = pShuffledArray[randomElement];
pShuffledArray[randomElement] = pShuffledArray[counter];
pShuffledArray[counter] = temp;
}
std::cout << "Shuffled array: ";
//print the (hopefully) shuffled array
for (int counter = 0; counter < size; counter++) {
std::cout << std::endl << pShuffledArray[counter];
}
std::cout << std::endl << std::endl;
//Delete dynamically allocated array within scope
delete [] pShuffledArray;
pShuffledArray = NULL;
}
I've tried specifying the type at time of the function call, but that didn't fix the error. I don't believe that my issue has anything to do with taking arguments by reference, either.
I tend to prefer Gist for reading code, so I've uploaded the entire program there, along with the assignment description (in case that's helpful somehow).
Thanks for any help!
Upvotes: 0
Views: 267
Reputation: 1632
At this line:
void shuffleSort(T pPresortedArray[], T size)
Do you really want that argument size's type is T.
And you can use std::random_shuffle random_shuffle API and a new API
Upvotes: 0
Reputation: 8671
The size of your array should be a size_t
or an int
, but not a T
at any rate :)
Except for that, it's a pretty good piece of code.
You could use std::swap
to simplify it a bit.
Also, your input code could easily be made into a template too, to avoid heavy duplication.
Upvotes: 1
Reputation: 50210
well this seems wrong
void shuffleSort(T pPresortedArray[], T size) {
WHy would you template the size of the array, its always going to be size_t
void shuffleSort(T pPresortedArray[], size_t size) {
I suspect you did a global replace of 'int' by 'T' :-)
Upvotes: 2