www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/01/09:12:05

From: jakarppi AT tuomi DOT oulu DOT fi (Jari Karppinen)
Newsgroups: comp.os.msdos.djgpp
Subject: a trouble with c++ code
Date: 1 Feb 1999 13:40:25 GMT
Organization: University of Oulu, Mathematics Department
Lines: 169
Message-ID: <794as9$cpo$1@ousrvr3.oulu.fi>
NNTP-Posting-Host: tuomi.oulu.fi
X-Trace: ousrvr3.oulu.fi 917876425 13112 130.231.241.29 (1 Feb 1999 13:40:25 GMT)
X-Complaints-To: news AT news DOT oulu DOT fi
NNTP-Posting-Date: 1 Feb 1999 13:40:25 GMT
X-Newsreader: TIN [UNIX 1.3 unoff BETA release 960807]
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I wrote a simple program which implements classes for matrices and
vectors. When I compile the program under Unix, it runs without trouble.
However, a version compiled with djgpp doesn't work right.

The program is supposed to read data for a matrix and a vector and
then print their product (that is, b=m*a, where m is the matrix and a,b
are vectors). If I feed it for example m=1 0 0 1 (2x2 identity matrix) 
and a=10 10 it prints 10 10 under Unix... but a djgpp compiled exe prints
0 1.4013e-44.

Any ideas why it doesn't work? The source is below:
--
#include <iostream.h>

class vector;

class matrix {
  int rows,cols;
  float **elems;
public:
  matrix();
  matrix(int,int);
  ~matrix();

  matrix operator=(const matrix&);
  friend vector operator*(const matrix&,const vector&);

  void set(int,int,float);

  matrix& input();
  void print();
};

matrix::matrix() {
  rows=1; cols=1;
  elems=new float*[rows];
  elems[0]=new float[1];
  elems[0][0]=0;
}

matrix::matrix(int m,int n) {
  rows=m; cols=n;
  elems=new float*[rows];
  for (int i=0;i<rows;i++) elems[i]=new float[cols];
}

matrix::~matrix() {
  for (int i=0;i<rows;i++) delete(elems[i]);
  delete(elems);
}

matrix matrix::operator=(const matrix &m) {
  int i,j;
  for (i=0;i<rows;i++) delete(elems[i]);
  delete(elems);
  rows=m.rows; cols=m.cols;
  elems=new float*[rows];
  for (i=0;i<rows;i++)
    elems[i]=new float[cols];
  for (i=0;i<rows;i++)
  for (j=0;j<rows;j++)
  elems[i][j]=m.elems[i][j];
  return *this;
}

matrix& matrix::input() {
  int i,j;
  for (i=0;i<rows;i++) delete(elems[i]);
  delete(elems);
  cout << "\nEnter number of rows and columns for a matrix.\n";
  cin >> rows >> cols;
  elems=new float*[rows];
  for (i=0;i<rows;i++) elems[i]=new float[cols];
  cout << "\nFeed me Seymour... I need data for a " << rows << "x" << cols << " matrix.\n";
  for (i=0;i<rows;i++)
  for (j=0;j<cols;j++)
  cin >> elems[i][j];
  return *this;
}

void matrix::print() {
  cout << "\n";
  for (int i=0;i<rows;i++) {
    for (int j=0;j<cols;j++) cout << elems[i][j] << " ";
      cout << "\n";
  }
}

class vector {
  int cols;
  float *elems;
public:
  vector();
  vector(int);
  ~vector();

  vector& operator=(const vector&);
  friend vector operator*(const matrix&,const vector&);

  void set(int,float);

  vector& input();
  void print();
};

vector::vector() {
  cols=1;
  elems=new float[cols];
  elems[0]=0;
}

vector::vector(int n) {
  cols=n;
  elems=new float[cols];
}

vector::~vector() {
  delete(elems);
}

vector& vector::operator=(const vector& v) {
  delete(elems);
  cols=v.cols;
  elems=new float[cols];
  for (int i=0;i<v.cols;i++) elems[i]=v.elems[i];
  return *this;
}

vector operator*(const matrix& m,const vector& v) {
  vector r(m.rows);
  for (int i=0;i<m.rows;i++) {
    r.elems[i]=0;
    for (int j=0;j<m.cols;j++) r.elems[i]+=m.elems[i][j]*v.elems[j];
  }
  return r;
}

vector& vector::input() {
  delete(elems);
  cout << "\nEnter number of elements for a vector.\n";
  cin >> cols;
  elems=new float[cols];
  cout << "\nFeed me Seymour... I need data for a " << cols << "-vector.\n";
  for (int j=0;j<cols;j++)
  cin >> elems[j];
  return *this;
}

void vector::print() {
  cout << "\n";
  for (int i=0;i<cols;i++) cout << elems[i] << " ";
  cout << "\n";
}

int main() {
  matrix m;
  vector a;

  m.input();
  a.input();

  vector b=m*a;
  b.print();

  return 0;
}



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019