Alright, so I am trying to create a program that will take a [m][n] Matrix and multiply it by a [n][p] Matrix, filling each matrix with increasing values.

I am having a problem with my function, prototype, and call. I keep getting the "...not declared in this scope" error and know it has to do with the syntax I used for my function.

Below is my code. Can anyone please help me?

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//int multiply(int[][acol],int[][bcol]);
// void menu();

int main ()
{
  int row(0), column(0);
  int arow(0),acol(0), brow(0),bcol(0);
  int matrix_a[arow][acol];
  int matrix_b[brow][bcol];
  int i(0), j(0);
  int temp(0),value(0);


  cout << "Please Enter the row dimension for Matrix A" << endl;
  cin >> arow;

  cout << "Please enter the column dimension for Matrix A" << endl;
  cin >> acol;

  cout << "Your matrix is: " << arow << " by " << acol << endl;

  for(i = 0; i < arow; i++)
    for(j = 0; j < acol; j++)
      {
        ++value;

        matrix_a[i][j] = value;

        cout << "The values for Matrix A are: " << matrix_a[i][j] << endl;
      }

  cout << "Please enter the row dimension for Matrix B.\n" << "Note: it must equal the column dimension of A" << endl;
  cin >> brow;

  if ( brow != acol)
    {
      cout << "Matrix multiplication requires inner dimensions of the matrices to be equal" << endl;
      cout << "Program exiting" << endl;
      return (0);
    }

  cout << "Please enter the column dimension for Matrix B" << endl;

  cin >> bcol;

  cout << "Your matrix is: " << brow << " by " << bcol << endl;

  for(i = 0; i < brow; i++)
    for(j = 0; j < bcol; j++)
      {
        ++temp;

        matrix_b[i][j] = temp;

        cout << "The values for Matrix B are: " << matrix_b[i][j] << endl;
      }

  // multiply(matrix_a,matrix_b);


  return 0;

}

/*
int multiply(int a[][], int b[][])
{
  int i, j, k, result = 0;
  double sum, total = 0.0;

  for (i = 0; i < arow; i++)
    for (j = 0; j < bcol; j++)
      {
        sum = 0;
        for (k = 0; k < brow; k++)
          total = 0;
        total = a[i][k]*b[k][j];
        sum = sum + total;
      }
  result[i][j] = sum;

return result;
}
*/

Thank you for your time!

Recommended Answers

All 5 Replies

int row(0), column(0);
int arow(0),acol(0), brow(0),bcol(0);
int matrix_a[arow][acol];
int matrix_b[brow][bcol];
int i(0), j(0);
int temp(0),value(0);

What are you trying to do here?

Initializing variables I planned on using.

The 'row' and 'column' can be thrown out.

Then I was working with Matrix A as a 2D array comprised of a row and a column value in which the column of A (acol) must equal the row of B (brow).

Then I was using value to fill the matrices with a random value. It just increases since the values in the matrices do not matter to me because I plan on using this program to run it and profile it for later optimization.

You can't initialize variables like that. You have to do int row = 0; int column = 0;, etc.

After hard coding in the values for the arrays (which I didn't want to do, but have to for the moment) I am having trouble getting my function call to return a value.

I have my function set to return the 2D array called "result" and I want to pass the reference or value into an array in main.

Can someone help me do that?

You are setting your matrix to be 0 rows by 0 columns in this declaration:

int arow(0),acol(0), brow(0),bcol(0);
int matrix_a[arow][acol];
int matrix_b[brow][bcol];

You are then asking for the number of rows and columns in the matrix and assigning values to that matrix based on the number of rows and columns:

cout << "Please Enter the row dimension for Matrix A" << endl;
cin >> arow;

cout << "Please enter the column dimension for Matrix A" << endl;
cin >> acol;

cout << "Your matrix is: " << arow << " by " << acol << endl;

for(i = 0; i < arow; i++)
for(j = 0; j < acol; j++)
{
++value;

matrix_a[i][j] = value;

cout << "The values for Matrix A are: " << matrix_a[i][j] << endl;
}

So you are putting values into a matrix that has no room to hold those values since you made a declaration of:

int matrix_a[arow][acol];

where arow and acol are both 0. It actually ran for me, but you are creating the risk that you will overwrite values because you do not set aside enough memory. I think you need to declare your arrays dynamically with the "new" command since you do not know the number of rows and columns ahead of time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.