Matrix method help please
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Matrix method help please

  1. #1
    Join Date
    Nov 2009
    Posts
    19

    Matrix method help please

    I have written some simple methods for a matrix class to do two dimensional problem solving but I'm not sure how to do a replace one matrix's values with another.
    Could anybody help please?
    Code:
    #include "Matrix.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    //constructor
    Matrix::Matrix(int N) {
    	nelts = N*N; 
    	data.resize(nelts);
    }
    
    //destructor
    Matrix::~Matrix(){}
    
    
    
    //METHOD Set value of element i, j
    void Matrix::setValue(int i, int j, int N, double x) {
    	if (i<0 || i >= nelts){
    	  cerr << "Out of bounds.\n";	//error check
    	  abort();
    	  }
    	else
    	  data[j*N+i] = x;
    }
    
    //METHOD clear the Matrix (set to size zero)
    void Matrix::clearMat(){
    	data.clear();
    }
    
    //METHOD Get value of element i, j
    double Matrix::getValue(int i, int j, int N) {
    	
      return data[i + j*N];
    }
    
    //METHOD print Matrix, takes starting point as arg
    
    void Matrix::print(int N){	
      for (int i=0; i < N; i++) {
    	for (int j=0; j < N; j++) {
    	cout << data[j*N+i] << ",";
    	}
      cout << endl;
      }
    }
    
    void Matrix::output(int N){	
      ofstream stuff;
      stuff.open ("stuff.csv", ios::app);
      for (int i=0; i < N; i++) {
    	for (int j=0; j < N; j++) {
    	stuff << data[j*N+i] << ",";
    	}
      stuff << endl;
      }
      stuff.close();
    }
    
    //METHOD returns size of Matrix
    int Matrix::getsize(){
    	int size=data.size();
    	return size;
    }
    
    void Matrix::viewall(){
      for (int i = 0; i <= nelts-1; i++){
    	cout << data[i] << endl;
      }
    }
    I've been trying something like

    Code:
    //METHOD returns size of Matrix
    void Matrix::replace(Matrix &v){
    	data=v.data;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Matrix method help please

    Please post Matrix.h. No one knows what variable type "data" is.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2009
    Posts
    19

    Re: Matrix method help please

    Sorry

    Code:
    #ifndef MATRIX_H		//preprocessor instructions
    #define MATRIX_H
    
    #include <iostream>		//preprocessor instructions
    #include <vector>
    #include <math.h>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    class Matrix{
    
    public:				//accessible anywhere in the program
    	Matrix(int N);
    	~Matrix();
    
    	void swapper(Matrix &v);
    	void viewall();
    	void setValue(int i, int j, int N, double x);
    	void clearMat();
    	double getValue(int i, int j, int N);
    	void print(int N);
    	void output(int N);
    	int getsize();
    
    private:		//only accessible insde the scope of the class
    	int nelts;
    	int N;
    	vector <double> data;
    	
    };
    #endif

  4. #4
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Matrix method help please

    OK.

    The line you say shouldn't work does work. It copies the contents of the passed in vector to the one in the object. So I see no problem -- it works exactly as you've coded it to work.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2009
    Posts
    19

    Re: Matrix method help please

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center