1

This function should initialize arrays C[m+1][n+1] and B[m][n] and fill the first row and first column of C with zeros. Note: int*** C refers to a pointer points to the 2D integer array. Please correct the error.

void initLCSTable(int*** C, char*** B, int m, int n)
{
C[m + 1][n + 1] = {{0}}; //i don't know if this makes the 1st row & column to 0
for (int row = 0; row < m; row++)
{
    for (int col = 0; col < n; col++)
    {
        C[m][n] = 0;
    }
B[m][n];
}
void printLengthTable(int** C, int m, int n);
void printArrowTable(char** B, int m, int n);

//The following function releases the memory space table C & B 
occupied
void freeLCSTable(int** C, char** B, int m)
{
// add code here. Please assist me in this function.
}


main {
int** C;
char** B;
initLCSTable(&C, &B, m, n);

cout << "\nTable C" << endl;
printLengthTable(C, m, n);

cout << "\nTable B" << endl;
printArrowTable(B, m, n);
return 0;
}


//This function print the 2D length array C
//Note: array C has m+1 rows and n+1 column
void printLengthTable(int** C, int m, int n)
{
    for (int i = 0; i <= m; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            cout << C[i][j] << " ";
        }
        cout << endl;
    }
}

//******************************************
//This function print the 2D arrow array B
//Note: array B has m rows and n column
void printArrowTable(char** B, int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << B[i][j] << " ";
        }
        cout << endl;
    }
}

Output should look like this (ignore the non-zeros since that is a different Longest Subsequence question all together):

enter image description here

Ayaan
  • 45
  • 1
  • 5
  • I'm not comfortable answering this question because you have not made a credible attempt at solving it yourself yet, but here is a hint to get you started: [How do I declare a 2d array in C++ using new?](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – user4581301 Nov 14 '18 at 05:14
  • A side note: You don't want to do this in C++. The preferred approach would be [nesting `std::vector`s](https://stackoverflow.com/questions/9694838/how-to-implement-2d-vector-array) or a [matrix class wrapper](https://stackoverflow.com/a/2076668/4581301) around a single vector to make the vector appear to be 2 dimensional – user4581301 Nov 14 '18 at 05:18
  • @user4581301 I have now made an attempt at trying to initializing the first row and column to 0s. But I'm not sure if Im wrong since I cannot check because I don't know how to release the memory in the freeLCStable function. (I'm really sorry but I'll highly appreciate it if you can assist me) – Ayaan Nov 14 '18 at 05:48
  • @user4581301 I was suggested to use std::vector as well by other users here but my professor wants me to do this way specifically.. (sorry again) – Ayaan Nov 14 '18 at 05:50
  • `C[m + 1][n + 1] = {{0}}; //i don't know if this makes the 1st row & column to 0` doesn't allocate anything. Read the link about how to declare a 2D array. You can't do jack until you allocate storage. – user4581301 Nov 14 '18 at 06:31

1 Answers1

0

As long as you initialize part of the array, the rest will be default-initialized.

C[m + 1][n + 1] = {{0}}; // Zero-initialized
C[m + 1][n + 1] = {0};   // Same ^
C[m + 1][n + 1] = {{1}}; // Zero-intialized except for [0][0] = 1
C[m + 1][n + 1] = {1};   // Same ^
C[m + 1][n + 1];         // Uninitialized!

There are some contexts where arrays are zero-initialized anyway, but it doesn't hurt to do so explicitly.

Note that if you want to initialize with values other than zero, you'd be (mostly) out of luck. I've done work recently with template metaprogramming that would help...

// You can subclass this type, add it as a member, etc.
template<class T, T... I> struct Seq {
    typedef T value_type [sizeof...(I)];
    // If you want to use it directly from here:
    static constexpr value_type value = {I...};
    // Don't bother trying with any variation of...
    // static constexpr value_type to_array(void) { return {I...}; }
};
template<class T, T... I0, T... I1>
auto operator+(Seq<T, I0...> const&, Seq<T, I1...> const&)
    -> Seq<T, I0+I1...> { return {}; }
template<class T, T... I0, T... I1>
auto operator<<(Seq<T, I0...> const&, Seq<T, I1...> const&)
    -> Seq<T, I0..., I1...> { return {}; }
// ... more Seq manipulation ...

I can use this to define ranges of numbers, binomial coefficients, etc. and as long as I still have some form of access to the template arguments, I can make an array of it. (That includes moving around a Seq<...> as an abstract T until the function where it's used.) I'd love to know what more can be done with this. I tried returning an initializer_list and defining an array out of that, but no luck (array initializers apparently only look like initializer_lists.)

John P
  • 1,463
  • 3
  • 19
  • 39
  • okay but how do I complete the void freeLCSTable() function above? – Ayaan Nov 14 '18 at 04:51
  • Sorry, I'm not sure, and I'm not sure I answered the question looking at your code again. I work almost exclusively on the stack. It looks like you're assigning an array to a pointer that was already allocated, which would mean that the original is never freed and freeLCSTable would try to free the array that's on the stack. Hopefully someone with more dynamic memory experience could chime in. – John P Nov 14 '18 at 05:03