0

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!

Community
  • 1
  • 1
Lithedreamer
  • 29
  • 1
  • 5

3 Answers3

2

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' :-)

pm100
  • 48,078
  • 23
  • 82
  • 145
  • You got me. :) What's the difference between size_t and just declaring it an int? – Lithedreamer Feb 26 '15 at 01:02
  • @Lithedreamer size_t is the accepted way of saying this represents a count / size of things (for example sizeof returns a size_t). It is typically unsigned int – pm100 Feb 26 '15 at 16:34
  • Thanks! Is that appropriate for looping through arrays, or is it particularly suited for functions? – Lithedreamer Feb 26 '15 at 20:44
1

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.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
  • Thanks! It seems obvious now that the size should be an integer. I also needed to declare randomElement as an integer. I made a note that the input code could be turned into a template earlier, but I wanted to get it working first. I'm going to go work on that now, actually; it sounds like fun. My instructor tends to frown on anything easy, so I'm probably going to skip std::swap though. – Lithedreamer Feb 26 '15 at 01:00
  • Quick follow-up, is there an easy way to pass a variable type as an argument? Like: shufflePrompt(int, "integerString"); – Lithedreamer Feb 26 '15 at 01:13
  • Solved: shufflePrompt("Integers"); Templates are awesome! – Lithedreamer Feb 26 '15 at 01:35
  • Mmm... I hope in time you'll come to see how dangerous they can be too :). – kuroi neko Feb 26 '15 at 08:13
  • That's such an omnious smile. I guess I'll find out...sometime. – Lithedreamer Feb 26 '15 at 20:45
0

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

std::shuffle API

Ron Tang
  • 1,532
  • 12
  • 20