0

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}};
B[m][n];
}
void printLengthTable(int** C, int m, int n);
void printArrowTable(char** B, int m, int n);

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;
}

Output should look like this (ignore the non-zeros since that is a different Longest Subsequence question all together): https://i.stack.imgur.com/ElWMY.png

Ayaan
  • 45
  • 1
  • 5
  • `std::vector> C(m+1, std::vector(n+1));` -- `std::vector> B(m, std::vector(n));` Any reason why you're not simply using `std::vector`? – PaulMcKenzie Nov 14 '18 at 02:46
  • @PaulMcKenzie I'm simply not familiar with it and I am required to do it this way. Could you assist me with the code that I wrote instead of vector? – Ayaan Nov 14 '18 at 02:54
  • You are accessing uninitialized pointers. Unless your teacher doesn't know what they're doing, you must have been taught "dynamic memory allocation" using `new[]`. – PaulMcKenzie Nov 14 '18 at 02:55
  • Ya fair. Could you assist me with vectors method then? Your piece of code doesn't fill the first row and column of array C into 0s. – Ayaan Nov 14 '18 at 02:58
  • Wrong. My method fills *all* the entries of `C` with 0. Why do you care what the other entries are on initialization, as long as the first row and first column are 0? Also, you can access elements of vectors by simply using `[]` just like regular arrays. – PaulMcKenzie Nov 14 '18 at 02:59
  • Ah yeah that's a good point but would I be able to fill C table with non zeros on the second row and second column and onwards? if so, then i tried running your code and it gives me an exception error in the main in these lines. How do i modify them?: int** C; char** B; initLCSTable(&C, &B, m, n); – Ayaan Nov 14 '18 at 03:04
  • Are you using pointers, or are you using `vector`? Your original code could never work correctly, again, due to accessing pointers that are not initialized. Either get rid of the pointers and use `vector`, or learn how to correctly handle dynamically allocated memory. – PaulMcKenzie Nov 14 '18 at 03:08
  • I'll use vectors from now since you think that will be right. – Ayaan Nov 14 '18 at 03:09
  • If I will use pointers then I need assist doing the following function regarding releasing the memory space table C & B occupied void freeLCSTable(int** C, char** B, int m) { //Add your own code here //---- //---- } – Ayaan Nov 14 '18 at 03:11
  • A `vector` handles all of that for you automatically. There is no need for such a `free` function when using vector. – PaulMcKenzie Nov 14 '18 at 03:16
  • I just checked that my professor expects me to do it with free function. Could you help me write the code for the free function and initLCSTable above, please? it keeps on giving me errors – Ayaan Nov 14 '18 at 03:20
  • @PaulMcKenzie, that could be misleading... if you have a std::vector we should iterate over the vector and delete all memory that was allocated, then can call std::vector::clear() if needed. – Omid CompSCI Nov 14 '18 at 03:22
  • @OmidCompSCI could you help me write it without vectors (use pointers) because I keep on getting exceptions and I am not very confident with dynamic allocation memory – Ayaan Nov 14 '18 at 03:25
  • @Ayaan, can't you do something like: int[m][n] some_array; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { some_array[m][n] = 0; } } c = &some_array; b = &some_array. This doesn't allocate C or B, but points to an allocated 2D array. – Omid CompSCI Nov 14 '18 at 03:33
  • is this for initLCSTable function? If it gives me an error on int[m][n] some_array; – Ayaan Nov 14 '18 at 03:38
  • @Ayaan, yes for initLCSTable, and it should be int some_array[m][n].... you sure you understand c++? You shouldn't be on pointers if you can't figure out syntax issues? – Omid CompSCI Nov 14 '18 at 03:44
  • @OmidCompSCI I'm a beginner still learning. On the line of some_array[m][n]; it gives me an error on m and n saying that expression must have a constant value and that the value of parameter "m" and "n" cannot be used as a constant – Ayaan Nov 14 '18 at 03:51
  • @Ayaan, https://stackoverflow.com/questions/31027229/initialize-array-with-a-non-const-function-argument. Forgot compile time you don't know what m and n are. must be some constant value. IF you know what M & N are then just hard code it for now. so like int some_array[5][10], and code everything else for now, and come back to that issue later. – Omid CompSCI Nov 14 '18 at 03:55
  • Alright got it, and could you assist me with the freeLCSTable function pls – Ayaan Nov 14 '18 at 04:05
  • @OmidCompSCI Alright got it, and could you assist me with the freeLCSTable function pls – Ayaan Nov 14 '18 at 04:11
  • @OmidCompSCI What is misleading? There is no need for pointers at all in this program. A `vector>` and `vector>` both do not need manual cleanup. That is the point I'm making. There is no need for a "free" function. – PaulMcKenzie Nov 14 '18 at 04:38
  • [Example](http://coliru.stacked-crooked.com/a/8bbde27635d9118d). There are no memory leaks in that program. – PaulMcKenzie Nov 14 '18 at 04:45
  • @PaulMcKenzie, yea but your statement "There is no need for such a free function when using vector." might mislead some people. But yes you are correct. In terms of this program you don't need to. – Omid CompSCI Nov 14 '18 at 05:00
  • @Ayaan, the way I told you does not require a freeLCSTable() function, because you didn't allocate any memory to C or B, they are just pointing to a place, they didn't create their own memory to delete. You will have to look into doing something like C = new int[5][20]. My syntax might be off, but you can google/search on how to dynamically allocate array in c++ – Omid CompSCI Nov 14 '18 at 05:03

0 Answers0