I am in a class working on templates. I am trying to create a template that will make an array for any datatype. I have got the template working I think but when I am outputting my first array for some reason it looks to have doubled my array and I have an array that is created with my random numbers correctly but then an empty array added on. I haven't used a debugger because I would have to cycle through the first 500 elements to get to where I can see what it does. I thought somebody might be able to tell me why the display function is outputting the correct array plus what looks to be an empty array behind it. Here is the code:
#include <iostream>
#include <cstdlib>
#include <array>
#include <iomanip>
using namespace std;
template< typename T > class myArray {
public:
T *myarray;
myArray(int size)
{
myarray = new T [size];
}
void bubbleSort (int size)
{
for (int i = size; i >= 0; i--)
for (int j = size; j > size - i; j--)
if (myarray[j] > myarray[j - 1])
swap(myarray[j], myarray[j-1]);
}
void display(int size)
{
cout << setprecision(2) << fixed << endl;
int j = 0;
for (int i = 0; i < size; i++)
{
cout << setw(4) << myarray[j] << setw(9) << myarray[j+1] << setw(9) << myarray[j+2] << setw(9) <<
myarray[j+3] << setw(9) << myarray[j+4] << setw(9) << myarray[j+5] << setw(9) <<
myarray[j+6] << setw(9) << myarray[j+7] << setw(9) << myarray[j+8] << setw(9) <<
myarray[j+9] << setw(9) << myarray[j+10] << setw(9) << myarray[j+11] << endl;
j = j + 12;
}
}
};
int main(int argc, char** argv) {
int size = 500;
myArray <int> t1(size);
for (int i = 0; i < size; i++)
{
int n = rand()% 5000 + 1;
t1.myarray[i] = n;
}
t1.display(size);
return 0;
}