I'm writing a program for my C++ class that takes the user input for the size of a int and char array, fills the arrays with random values (numbers 0-100, letters A-Z) then sorts, reverses, and displays both arrays.
For the most part the program works and I understand the logic I used here, but...
After running and debugging the code multiple times. I noticed that when the arrays were being filled with values, the first element, even though it was actually being given a value, it would not print the assigned value it was given in ascending order, but would in a descending order? I don't understand this at all.
NOTE: I have to use template functions for the sorting, reversing and display of the arrays.
template <class T> 
void sort(T *arr, int a) {
    T temp;
    for (int i = 0; i < a; i++) {
        for (int j = a; j > 0; j--) {
            if (arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
 }
template <class T>
void reverse(T *arr, int a) {
    T temp;
    for (int i = 0; i < a / 2; i++) {
        temp = arr[i];
        arr[i] = arr[a - i];
        arr[a - i] = temp;
    }
}
template <class T>
void display(T *arr, int a) {
    for (int i = 0; i < a; i++) {
        cout << arr[i] << ", ";
    }
    cout << endl;
}
template<class T>
void save(T *arr, int a) {
    sort(arr, a);
    display(arr, a);
    reverse(arr, a);
    display(arr, a);
}
int main() {
    int x, y;
    cout << "Please enter a number for an array of data type \"int\"" << endl;
    cin >> x;
    cout << "Please enter a number for an array of data type \"char\"" << endl;
    cin >> y;
    int *arr1 = new int[x];
    char *arr2 = new char[y];
    for (int i = 0; i < x; i++) 
        cout << (arr1[i] = rand() % 100 + 1);
    srand(time(nullptr));
    for (int i = 0; i < y; i++)
        cout << (arr2[i] = rand() % 26 + 65);
    system("cls");
    save(arr1, x);
    save(arr2, y);
    delete[]arr1;
    delete[]arr2;
    system("pause");
    return 0;
}
 
     
     
    