when i compile i get an error message saying the following
"51: error: assignment of read-only location"
line 51 reads: c[j] = a[j] + b[j];
any help to get rid of this problem???

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

#define N 3
void addMatrix(const double a[][N],
               const double b[][N], const double c[][N]);
int main()
{
  double a[N][N];
  double b[N][N];
  double c[N][N];

  // Enter nine digits for matrix 1                                                                              
  cout << "Enter matrix1: ";

  for (int i = 0; i < N; i++)
    {
      for (int j = 0; j < N; j++)
      cin >> a[i][j];
    }
  // Enter nine digits for matrix 2                                                                              
  cout << "Enter matrix2: ";

  for (int i = 0; i < N; i++)
    {
      for (int j = 0; j < N; j++)
      cin >> b[i][j];
    }

  for(int i = 0; i < N; i++)
    {
      for (int j = 0; j < N; j++)
      c[i][j] = 0;
    }
  addMatrix(a,b,c);

  return 0;
}

void addMatrix(const double a[][N],
               const double b[][N], const double c[][N])
{
  for (int i = 0; i < N; i++)
    {
     for (int j = 0; j < N; j++)
        c[i][j] = a[i][j] + b[i][j];
    }
 cout << " The multiplication of the matrices is " << endl;

 for (int i = 0; i < N; i++)
   {

 for (int j = 0; j < N; j++)
   cout << a[i][j];

 if(i == 0)
   cout << setw(3);
   else if(i == 1)
     cout << setw(3) <<"+" << setw(3);
   else if(i == 2)
     cout <<setw(3);


 for (int j = 0; j < N; j++)
   cout << b[i][j];

 if(i == 0)
   cout << setw(3);
   else if(i == 1)
     cout << setw(3) <<"+" << setw(3);
   else if(i == 2)
     cout <<setw(3);

 for (int j = 0; j < N; j++)
   cout <<c[i][j];

   cout << endl;
   }

}

its actually line 47 here, not line 51

It's because you have 'double c[][N]' declared as a constant. It's giving you an error because it is not going to be constant.

Just change it from 'constant double c[][N]' to 'double c[][N]'

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.