URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       Essentials of Scientific Computing 2013
  HTML https://esc2013.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: Programming Questions
       *****************************************************
       #Post#: 13--------------------------------------------------
       Matrix Class
   DIR By: mathnathan
       Date: July 22, 2013, 12:36 pm
       ---------------------------------------------------------
       Here is the Matrix Class skeleton. Each team should fill in the
       part they chose in class. Currently there is segmentation fault.
       The program segfaults because in the Destructor that I wrote I
       deallocated the dynamic memory that you  (have not yet) but will
       dynamically allocate. So it segfaults because there is not
       dynamic memory to deallocate! You will find that most of you
       will need each other's functions to complete your own! So feel
       free to collaborate and share your functions with eachother when
       you finish them. Primarily, those of you working on the
       constructors will be in the most demand as everything else can
       not happen until the objects have been created using the
       constructors.
       --- Code ---
       class Matrix {
       
       public:
       Matrix( int n, int m, float data[] ); // KKK
       Matrix( int n, int m, float val ); // <Insert Name
       Here>
       float get( int i, int j ); // Monkey Mafia
       ~Matrix(); // Nathan
       float operator()( int i, int j ); // Nathan
       Matrix operator=( Matrix B ); // Nathan
       bool operator==( Matrix B ); // Nathan
       Matrix mul( const Matrix& B ); // Team 1
       Matrix mul( float c ); // Dooms Day Bunnies
       Matrix add( const Matrix& B ); // Ghost in The Shell
       Matrix row( int n ); // Fetch
       Matrix col( int m ); // Rice Nybbles
       Matrix copy(); // Vicious and Delicious
       void show(); // Shitake Mushrooms
       
       private:
       float* data; // This is a pointer to the actual data
       int n; //rows
       int m; //cols
       int num_elements; // The number of elements in the
       matrix (n*m)
       
       };
       
       // KKK: Dynamically allocate memory and initialize the data
       pointer to data
       Matrix::Matrix( int n, int m, float data&#91;] ) {
       
       }
       
       // <Insert Name Here>: Dynamically allocate memory and
       initialize to val
       Matrix::Matrix( int n, int m, float val ) {
       }
       
       // Monkey Mafia: Element access
       float Matrix::get( int i, int j ) {
       // Return the element in the ith row and jth column given
       that the data is
       // stored row after row
       }
       
       Matrix::~Matrix() {
       delete &#91;] data;
       }
       
       float Matrix::operator()( int i, int j ) {
       float val = get( i, j );
       return val;
       }
       
       Matrix Matrix::operator=( Matrix B ) {
       
       if( B == *this )
       return *this;
       n = B.n;
       m = B.m;
       num_elements = B.num_elements;
       delete &#91;] data;
       data = new float[num_elements];
       for (int k = 0; k <= num_elements; k++)
       data[k] = B.data[k];
       
       return *this;
       }
       
       bool Matrix::operator==( Matrix B ) {
       
       if( n != B.n )
       return false;
       if( m != B.m )
       return false;
       if( num_elements != B.num_elements )
       return false;
       for( int k = 0; k <= num_elements; k++)
       if( data[k] != B.data[k] )
       return false;
       return true;
       
       }
       
       // Team 1: Matrix Multiplication
       Matrix Matrix::mul( const Matrix& B ) {
       // Should be able to handle vectors as well i.e. matrices
       with 1 column
       }
       
       // Dooms Day Bunnies: Multiply matrix by scalar c
       Matrix Matrix::mul( float c ) {
       
       }
       
       // Ghost in the Shell: Add 2 matrices together
       Matrix Matrix::add( const Matrix& B ) {
       
       }
       
       // Fetch: Return the nth row from this instance
       Matrix Matrix::row( int n ) {
       
       }
       
       // Rice Nybbles: Return the mth column from this instance
       Matrix Matrix::col( int m ){
       
       }
       
       // Vicious and Delicious: Return a copy of this instance
       Matrix Matrix::copy(){
       
       }
       
       // Shitake Mushrooms: Print the matrix to the screen
       void Matrix::show() {
       
       }
       
       
       --- End Code ---
       Here is the test driver we wrote. To compile everything simply
       compile the below test matDriver.cpp file. It includes and uses
       the matrix class.
       --- Code ---
       #include <stdio.h>
       #include "matrix.h"
       
       int main() {
       
       int iters = 1000;
       
       float data[9] = {.2,.2,.15,.6,.3,.3,.2,.5,.55};
       int n = 3;
       int m = 3;
       Matrix A( n, m, data );
       Matrix C( n, m, 0.0 );
       for( int i = 0; i < iters; i++ ) {
       C = A.mul( A );
       }
       
       Matrix E = A.copy();
       
       
       return 0;
       }
       
       --- End Code ---
       *****************************************************
       Page 1 of 1