Re: Could use some help...:)

From: Silver (argytzak_at_med.auth.gr)
Date: 12/27/03


Date: Sat, 27 Dec 2003 02:37:47 +0200


>
> Just to be clear, no offense is meant by my criticisms. I believe you
> will benefit greatly from making sure, as you write each line of code,
> that you're writing it for a reason. If you're not sure that a given
> line or comment makes your program better, omit it.
>

That 's the reason I posted my message from the first place, to be given
some advice, of any kind ;)

Here's my code with run-time error. It will look pretty clumsy, I'm still a
beginner after all :o
(Indentation will be bad, due to copy-paste)
Plus, I didn't grasp that thing about:
"Implement operator *= as a member of A, and let a global (namespace)
operator call the *= operator of a local object."

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

class A
{
 protected:
  int n;
  int **p;

 public:
  A() { }
  A(int x);
  ~A();

  int** get_d() const { return p; }
  int operator [](unsigned row); // Overloaded 'array-access
operator'
};

// Constructor definition
A::A(int x)
{
 n = x;

 // dynamic 2D array allocation
 p = new int *[n];
 if (p == 0)
 {
  cout << endl
       << "Memory allocation failed.\n";
        exit(1); // Terminate program
 }
 for (int i1 = 0; i1 < n; i1++)
 {
  p[i1] = new int[n];
  if (p[i1] == 0)
  {
   cout << endl
     << "Memory allocation failed.\n";
   exit(1); // Terminate program
  }
 }

    cout << endl;
 for (int i2 = 0; i2 < n; i2++)
 {
  for (int i3 = 0; i3 < n; i3++)
  {
   cout << "p[" << i2 << "][" << i3 << "] = ";
   cin >> p[i2][i3];
  }
 }
}

A::~A()
{
    for (int i = 0; i < n; ++i)
        delete[] p[i];
 delete[] p;
}

class B: public A
{
 private:
  int k;
  int **d;

 public:
   B(int y);
   ~B();
   int operator [](unsigned row); // Overloaded 'array-access
operator'
   int operator ()(unsigned row, unsigned column); // Overloaded '()'
operator

 friend void setD(A a, B b);
 friend int** operator*(const A &alpha, const B &beta);
};

B::B(int y)
{
 k = y;

 // dynamic 2D array allocation
 d = new int *[k];
 if (d == 0)
 {
  cout << endl
       << "Memory allocation failed.\n";
  exit(1); // Terminate program
 }
 for (int i4 = 0; i4 < k; i4++)
 {
  d[i4] = new int[k];
  if (d[i4] == 0)
  {
   cout << endl
     << "Memory allocation failed.\n";
   exit(1);
  }
 }

    cout << endl;
 for (int i5 = 0; i5 < k; i5++)
 {
  for (int i6 = 0; i6 < k; i6++)
  {
   cout << "d[" << i5 << "][" << i6 << "] = ";
   cin >> d[i5][i6];
  }
 }
}

B::~B()
{
    for (int i = 0; i < n; ++i)
        delete[] d[i];
 delete[] d;
}

int B::operator [](unsigned row)
{
 int row_sum = 0;
 for (int j = 0; j < n; j++)
  row_sum += row_sum + d[row][j];
 return row_sum;
}

int B::operator ()(unsigned row, unsigned column)
{
 int dot_product = 0;
 for (int i = 0; i < n; i++)
  dot_product += dot_product + ( p[row][i] * d[i][column]);
 return dot_product;
}

int A::operator [](unsigned row)
{
 int row_sum = 0;
 for (int j = 0; j < n; j++)
  row_sum += row_sum + p[row][j];
 return row_sum;
}

int** operator*(const A &alpha, const B &beta)
{
 /*// --- Memory allocation for p_array (nxn) ---
 int **p_array = new int *[n];
 if (p_array == 0)
 {
  cout << endl
       << "Memory allocation failed.\n";
  exit(1); // Terminate program
 }
 for (int i = 0; i < n; i++)
 {
  p_array[i] = new int[n];
  if (p_array[i] == 0)
  {
   cout << endl
     << "Memory allocation failed.\n";
   exit(1);
  }
 }
 // --- End of memory allocation ---

 /* Problem encountered:
 ** int (*p_array )[n] = new const int[n][n];
 ** Error message. ???
 */

 // Matrix multiplication
 int **temp = alpha.get_d();

 cout << endl;
 for(int j1 = 0; j1 < beta.n; j1++)
 {
  for (int j2 = 0 ; j2 < beta.n; j2++)
  {
   beta.d[j1][j2] = 0;
   for(int j3 = 0; j3 < beta.k; j3++)
   {
    beta.d[j1][j2] += temp[j1][j3] * beta.p[j3][j2];
   }
   cout << setw(4) << beta.d[j1][j2];
  }
  cout << endl;
 }
 return beta.d;
}

//void setD(A a, B b)
//{
// b.d = (a.p)*(b.p);
//}

int main()
{
 int size;

 cout << "\nDwse diastash n gia ton pinaka nxn? --> ";
 cin >> size;

 A a(size);
 B b(size);
 //b.pa*b;

 cout << endl;
 return 0;
}



Relevant Pages