Overclock.net banner

1 - 3 of 3 Posts

·
@knightsbr1dge
Joined
·
1,568 Posts
Discussion Starter · #1 ·
Right, urgent help required. Would really appreciate help from those who know what they are doing when it comes to C++.

Situation:
I have to write a program in C++ that will read values in from a .txt file and place each value into a matrix.

- the file name is 'r.txt'
- it contains 4 rows and 28 rows of numbers
- each number must be placed into a matrix which is 4 columns by 28 rows

I have the following header file available, which I will be using later in the task to transpose and multiply this matrix with another (smaller) one;

Code:

Code:
#include <iostream>
#include <vector>
// because of the bug in Visual C++ 6.0 the usual statement "using namespace std;" cannot be used here
// instead, the "namespace" std has to be manually specified for all objects in this namespace
// specifically, we use std::cout instead of cout, std::endl instead of endl and
// std::vector instead of just vector

// a class which holds matrices of an arbitrary size
class matrix {
protected:
int sizex,sizey; // horizontal and vertical size of the matrix
std::vector< std::vector<double> > thenumbers; // vector of vectors, which stores the actual numbers
public:
matrix(); // default constructor
matrix(int y_size, int x_size); // a constructor to create a matrix of specified dimensions
matrix(const matrix& a_matrix); // copy constuctor
inline double& operator() (int i, int j) { return thenumbers[i][j]; } // indexing operator
inline const double& operator() (int i, int j) const { return thenumbers[i][j]; } // indexing operator
void print(); // this function is for printing the matrix
friend matrix transpose(const matrix& op1); // transposition
friend bool operator== (const matrix& op1, const matrix& op2); // equality
inline friend bool operator!= (const matrix& op1, const matrix& op2) { return (!(op1==op2)); } // inequality is "not an equality"
friend matrix operator+ (const matrix& op1, const matrix& op2); // addition
friend matrix operator- (const matrix& op1, const matrix& op2); // subtraction
friend matrix operator* (const matrix& op1, const matrix& op2); // multiplication of matrices
friend matrix operator* (const double op1, const matrix& op2); // multiplication by a number on the left
friend matrix operator* (const matrix& op1, const double op2); // multiplication by a number on the right
friend std::ostream& operator<< (std::ostream& stream, const matrix& op1); // printing matrices into the streams
};

// default constructor creates an empty matrix (0 by 0)
matrix::matrix() {
sizex=0; sizey=0; // NB: in this case the vector of vectors is initialised using the default constructor
}

// a constructor to create a matrix of specified dimensions 
matrix::matrix(int y_size, int x_size)
: thenumbers(y_size,std::vector<double>(x_size,0.0)) { // calling appropriate constructors for the individual vectors
sizex=x_size; sizey=y_size; // using the specified matrix dimensions
}

// copy constructor
matrix::matrix(const matrix& a_matrix) {
sizex=a_matrix.sizex; sizey=a_matrix.sizey; // these two would be correctly copied by the default copy constructor...
thenumbers=a_matrix.thenumbers; // ...but not the vector of vectors. This simple line calls the default copy
  // constructor of the vector of vectors and, thus, it ensures that the data is copied correctly
}

// a member function for printing the matrix
void matrix::print() {
for (int i=0; i<sizey; i++) { // rows
for (int j=0; j<sizex; j++) { // columns
std::cout << ' ' << thenumbers[i][j]; // space separated numbers of a single row
}
std::cout << std::endl; // endline to start printing the next row
}
}

// matrix transposition
matrix transpose(const matrix& op1) { // the argument of this friend function is a matrix (X by Y) that we want to transpose
matrix result=matrix(op1.sizex,op1.sizey); // this is new (=result) matrix (Y by X) - after creating it contains zeros
for (int i=0; i<op1.sizey; i++) { // rows
for (int j=0; j<op1.sizex; j++) { // columns
result(j,i)=op1(i,j); // the data is copied from the original matrix into the new matrix...
}
}
return result; // ...which is returned in the end
}

// matrix comparison
bool operator== (const matrix& op1, const matrix& op2) { // two arguments are the two matrices that we are comparing
if ((op1.sizex!=op2.sizex)||(op1.sizey!=op2.sizey)) { // if the matrices of different dimensions, they cannot be equal
return false;
} else { // however, if the matrix dimensions are the same, the matrices should be compared
for (int i=0; i<op1.sizey; i++) { // rows
for (int j=0; j<op1.sizex; j++) { // columns
if (op1(i,j)!=op2(i,j)) return false; // if we have two unequal elements, the matrices are not equal
}
}
return true; // only if all of the matrix elements are the same, we can say that they the matrices are equal
}
}

// the operator to add two matrices
matrix operator+ (const matrix& op1, const matrix& op2) {
if ((op1.sizex!=op2.sizex)||(op1.sizey!=op2.sizey)) { // if the matrix dimensions do not match...
throw 100; // ...throw an exception with a code 100
} else {
matrix result=matrix(op1.sizey,op1.sizex); // otherwise, create the resulting matrix
for (int i=0; i<op1.sizey; i++) { // rows
for (int j=0; j<op1.sizex; j++) { // columns
result(i,j)=op1(i,j)+op2(i,j); // elements of the resulting matrix are the sums of appropriate elements of the argument matrices
}
}
return result; // the sum is returned
}
}

// the operator to subtract two matrices (for the detailed comments see the addition operator)
matrix operator- (const matrix& op1, const matrix& op2) {
if ((op1.sizex!=op2.sizex)||(op1.sizey!=op2.sizey)) { // if the matrix dimensions do not match...
throw 100; // ...throw an exception with a code 100
} else {
matrix result=matrix(op1.sizey,op1.sizex);
for (int i=0; i<op1.sizey; i++) {
for (int j=0; j<op1.sizex; j++) {
result(i,j)=op1(i,j)-op2(i,j);
}
}
return result;
}
}

// operator that returns a product of two matrices
matrix operator* (const matrix& op1, const matrix& op2) {
if (op1.sizex!=op2.sizey) { // if number of columns of the first matrix is not equal to number of rows of the second...
throw 100; // ...then throw an exception
} else {
matrix result=matrix(op1.sizey,op2.sizex); // pay close attention to the dimensions of the result!
for (int i=0; i<op1.sizey; i++) { // rows of the result
for (int j=0; j<op2.sizex; j++) { // columns of the result
result(i,j)=0.0;
for (int k=0; k<op1.sizex; k++) // sum of products (i'th row elements times j'th column elements)
result(i,j)+=op1(i,k)*op2(k,j);
}
}
return result;
}
}

// the following two operators implement multiplication of numbers and matrices
matrix operator* (const double op1, const matrix& op2) {
matrix result=matrix(op2.sizey,op2.sizex); // it can be done for matrices with arbitrary dimensions
for (int i=0; i<op2.sizey; i++) { // rows
for (int j=0; j<op2.sizex; j++) { // columns
result(i,j)=op1*op2(i,j); // the result is the element of the original matrix times the number
}
}
return result;
}

// the same as above, but for matrix multiplied by a number
matrix operator* (const matrix& op1, const double op2) {
matrix result=matrix(op1.sizey,op1.sizex);
for (int i=0; i<op1.sizey; i++) {
for (int j=0; j<op1.sizex; j++) {
result(i,j)=op1(i,j)*op2;
}
}
return result;
}

// this operator implements printing the matrix into the stream (for details see Lecture 12)
std::ostream& operator<<(std::ostream& stream, const matrix& mA) {
for (int i=0; i<mA.sizey; i++) {
for (int j=0; j<mA.sizex; j++) {
stream << mA(i,j) << ' ';
}
stream << std::endl;
}
return stream;
}
I would really appreciate any help you could provide me with. This is due in tomorrow (Friday) evening.
 

·
@knightsbr1dge
Joined
·
1,568 Posts
Discussion Starter · #3 ·
will post what i have done in about an hour, just on my way home.
 
1 - 3 of 3 Posts
Top