twoDarraysChar.cpp
//
// Created by caleb on 3/20/24.
//

#include <iostream>

char** create2DarrayChar(int rows, int columns) {
    char **pp = new char* [rows];
    for (int i = 0; i < rows; ++i) {
        pp[i] = new char[columns + 1];
    }
    return pp;
}


void populateChar(char** pp, int rows, int columns) {
    int ascii = 'A';
    for (int i = 0; i < rows; ++i) {
        int j = 0;
        for (j = 0; j < columns -1; ++j) {
            pp[i][j] = ascii + i + j;
        }
        pp[i][j] = '\0';
        ascii += 10;
        if (ascii > 100) {
            ascii = 65;
        }
    }
}

void populateCharReverse(char** pp, int rows, int columns) {
    int ascii = 'A';
    for (int i = 0; i < rows; ++i) {
    //for (int i = rows - 1; i >= 0; --i) {
        int j = columns;
        for (int j = 0; j < columns; ++j) {
        //for (j = columns - 1; j >= 0; --j) {
            pp[i][j] = ascii - i - j;
        }
        pp[i][j] = '\0';
        ascii += 10;
        if (ascii > 100) {
            ascii = 65;
        }
    }
}

///////////////////////////////////////////////////////////////////////////
/* traverses */
void traverseChar(char** pp, int rows, int columns) {
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < columns; ++j) {
            std::cout << pp[i][j] << " ";
        }
    }
    std::cout << std::endl;
}

void traverseCharSingleLoop (char** pp, int rows) {
    for (int i = 0; i < rows; ++i) {
        std::cout << pp[i] << std::endl;
    }
}
/////////////////////////////////////////////////////////////////////////////
/* frees */
void freeMemoryChar(char **pp, int rows) {
    for (int i = 0; i < rows; ++i) {
        delete[] pp[i];
    }
}


/////////////////////////////////////////////////////////////////////////////
/* Dereference versions */

char** create2DarrayCharDereference(int rows, int columns) {
    char **pp = new char* [rows];
    for (int i = 0; i < rows; ++i) {
        *(pp + i) = new char[columns + 1];
    }
    return pp;
}

void populateCharDereference(char** pp, int rows, int columns) {
    int ascii = 'A';
    for (int i = 0; i < rows; ++i) {
        int j = 0;
        for (j = 0; j < columns -1; ++j) {
            *(*(pp + i) + j) = ascii + i + j;
        }
        pp[i][j] = '\0';
        ascii += 10;
        if (ascii > 100) {
            ascii = 65;
        }
    }
}
void traverseCharDereference(char** pp, int rows, int columns) {
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < columns; ++j) {
            std::cout << *(*(pp + i) + j) << " ";
        }
    }
    std::cout << std::endl;
}
void traverseCharSingleLoopDereference(char** pp, int rows)  {
    for (int i = 0; i < rows; ++i) {
        std::cout << *(pp + i) << std::endl;
    }
}
void freeMemoryCharDereference(char **pp, int rows) {
    for (int i = 0; i < rows; ++i) {
        delete[] *(pp + i);
    }
}

void sort(char **pp, int columns, int rows) {
    char temp;
    for (int i = 0; i < columns; ++i) {
        for (int j = 0; j < (rows - 1); ++j) {
            if (pp[i][j] > pp[i][j + 1]) {
                temp = pp[i][j];
                pp[i][j] = pp[i][j + 1];
                pp[i][j + 1] = temp;
            }
        }
    }
}