-1

I was trying to make a function for matrix multiplication in C++, but the general function which I was trying to make in order to generate each element of the resultant matrix, I required to pass both the n-dimensional errors, where I required the row of the first array and column of the second row, I could easily provide the row address using indexing (array_1[x]) and then running it in a loop by increment the index but how can I do the same in another case, I want to use the column of an array, but on increment, it increases in terms of the same row, but I want to move down a column. Example: On passing an array, array_1[x], in int *y and operating y[k],k++, I am moving left to right along the row, but I want to move down the column.

Instead, I had to declare both arrays as public and perform the operation on these arrays in function. Can you suggest a way to increment the passed array down the column rather than along the row, or can you suggest a way to solve the problem without actually declaring the arrays globally?

Here is my current code:

  `
   #include<iostream>
    using namespace std;
     int i,j,m,n;
      int array_1[200][200];
     int array_2[200][200];
      int array_sum(int row,int column);
       int main()
  {
    cout<<"Enter the dimensions of the first array:";
    cin>>i>>j;
   cout<<"Enter the dimensions of the second array:";
  cin>>m>>n;
  if(j==m)
  {

    cout<<"Enter the inputs for the first array:";
    int row_1=0,column_1=0;
    for(;row_1<i;row_1++)
    {
        column_1=0;
        for(;column_1<j;column_1++)
        {
            cin>>array_1[row_1][column_1];
        }
    }
    array_1[row_1][column_1]='\0';
     cout<<endl<<"Enter the inputs for the second array:";
     int row_2=0;
     int column_2=0;
    for(;row_2<m;row_2++)
    {
        column_2=0;
        for(;column_2<n;column_2++)
        {
            cin>>array_2[row_2][column_2];
        }
    }
    array_2[row_2][column_2]='\0';
    int result_array[i][n];
    for(int row_3=0;row_3<i;row_3++)
    {
        for(int column_3=0;column_3<n;column_3++)
        {
            result_array[row_3][column_3]=array_sum(row_3,column_3);
        }
    }
    for(int row_3=0;row_3<i;row_3++)
    {
        for(int column_3=0;column_3<n;column_3++)
        {
            cout<<result_array[row_3][column_3]<<" ";
        }
        cout<<endl;
    }

    }
   else
   {
    cout<<"Invalid dimensions."<<endl<<"Multiplication not possible"<<endl;
   }
     }
       int array_sum(int row, int column)
   {
      int sum=0;
   int k=0;
  while(k<j)
  {
    sum=sum+(array_1[row][k]*array_2[k][column]);
    k++;
   }
           return sum;
      }`
  • 2
    _`int result_array[i][n];`_ isn't valid c++ code. If you want to work with fixed sized arrays, use `std::array`, if those have a variable size, use `std::vector`. – πάντα ῥεῖ Aug 05 '23 at 08:11
  • 1
    And in general you would hide the datastructure (std::array) inside a matrix class, because semantically an 2d arrays is NOT the same as a matrix. Side note : stop using `using namespace std;` and learn to type fully qualified names like `std::array` – Pepijn Kramer Aug 05 '23 at 08:18

0 Answers0