1

How do I get the second array to be properly looped through? It is a simple matrix multiplication operation (using Visual Studio 2019) and arr is being looped through, but arr2 isn't being looped through completely. Only the first 2 elements are being multiplied by all elements of arr.

#include <iostream>

using namespace std;

int main() {
    int r1 = 2, c1 = 3, r2 = 3, c2 = 3;

    int** arr = new int* [r1];
    for (int i = 0; i < c1; i++) {
        arr[i] = new int[c1];
    }
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c1; j++) {
            arr[i][j] = j;
        }
    }

    int** arr2 = new int*[r2];
    for (int i = 0; i < c2; i++) {
        arr2[i] = new int[c2];
    }
    for (int i = 0; i < c2; i++) {
        for (int j = 0; j < r2; j++) {
            arr2[i][j] = j;
        }
    }

    int** arr3 = new int*[r1];
    for (int i = 0; i < c2; i++) {
        arr3[i] = new int[c2];
    }
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            arr3[i][j] = 0;
        }
    }


    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            for (int g = 0; g < c1; g++) {
                arr3[i][j] += arr[i][g] * arr2[g][j];

                cout << "[" << i << "][" << g << "] " << arr[i][g] << "\tX\t" << "[" << g << "][" << j << "] " << arr2[g][j] << " => " << arr3[i][j] << endl;
            }
        }
    }

    cout << "\n" << endl;

    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            cout << "New Array Element: [" << i << "][" << j << "] => " << arr3[i][j] << endl;
        }
    }

    return 0;
}

  • 1
    `int** arr2 = new int*[r2]; for (int i = 0; i < c2; i++) {` -- See anything wrong with that line? What is `c2` doing there? You make the same mistake in all the other loops. This is also a good time to learn about writing functions, as it is easy to make a mistake with all of these one and two-letter variable names being used in several places. – PaulMcKenzie Jul 03 '20 at 00:50
  • IMHO, you have better success allocating a single area of memory and treating as a two dimensional array. BTW, this is how it is usually represented by the processor (since all of memory is linear, single dimension). – Thomas Matthews Jul 03 '20 at 01:01

1 Answers1

3

You make the same basic mistake in all of your loops -- that mistake being that you are not iterating over the rows correctly.

You are using the column count when you should be using the row count:

 int** arr = new int* [r1];
    for (int i = 0; i < c1; i++) {  // <-- This should be i < r1, not i < c1;
        arr[i] = new int[c1];
    }

Second, you could have written a function to lessen the chance of error, and also have a better chance of finding bugs like this:

 int **createArray(int rows, int cols)
 { 
    int** arr = new int* [rows];
      for (int i = 0; i < rows; i++) { 
        arr[i] = new int[cols];
    }
    
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            arr[i][j] = j;
        }
    }
    return arr;
}

int main()
{
   int r1 = 2, c1 = 3, r2 = 3, c2 = 3;

   int** arr = createArray(r1, c1);
   int** arr2 = createArray(r2,c2);
   int** arr3 = createArray(r1,c2);
   //...
}

Now the creation code is not repeated over and over again, plus the issue of using the wrong variables becomes minimized, if not eliminated.


Having said this, this way of creating 2D arrays is one of the worst ways of doing this. Right now, your code has memory leaks (it still is considered a leak, even though the program will terminate -- memory tools such as valgrind and others would indicate the errors as soon as your program exits).

Ideally, you would use a container class such as std::vector<int> to handle the dynamic memory management automatically.

But even if this is an exercise where you must use int**, it is still a very bad way of creating two-dimensional arrays under those constraints. Here is an answer that goes in detail about this issue, why it's bad, and how it can be alleviated.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45