Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,911 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,189 people online right now. Registration is fast and FREE... Join Now!




cout problem

 
Reply to this topicStart new topic

cout problem, can't see what is out

koprivica
6 Jan, 2008 - 09:55 AM
Post #1

New D.I.C Head
*

Joined: 13 Dec, 2007
Posts: 8


My Contributions
CODE



CODE
#include <stdlib.h>
#include <iostream.h>

#include "matrix.h"

// allocate
void Matrix::allocate (){
    mat=new Type*[rows];
    for (int i=0;i<=rows;i++) {
        mat[i]=new Type[cols];
        for (int j=0;j<=cols;j++) mat[i][j]=Type(0);    
    }
}


// release
void Matrix::release (){
    for (int i=0;i<=rows;i++) delete [] mat[i];
    delete [] mat;
}


// copy matrix
void Matrix::copy (const Matrix& right){
    for (int i=0;i<=rows;i++)
        for (int j=0;j<=cols;j++) this->mat[i][j]=right.mat[i][j];
}



// constructor
Matrix::Matrix (int m, int n): rows(m), cols(n){
    allocate();
}


// copy constructor
Matrix::Matrix (const Matrix& right): rows(right.rows),cols(right.cols){
    allocate();
    copy(right);
}


// destructor
Matrix::~Matrix (){
    release ();
}


Matrix& Matrix:: operator = (const Matrix& right){
    if (&right==this) return *this;
    if (right.rows!=rows || right.cols!=cols) return *this;
    release();
    allocate();
    copy(right);
    return *this;
}


Type& Matrix::operator () (int i, int j) {
    static Type error(0);
    if (i<0 || i>rows || j<0 || j>cols) return error;
    return mat[i-1][j-1];
}


int operator == (const Matrix& left, const Matrix& right) {
    if (right.rows!=left.rows || right.cols!=left.cols) return 0;
    for (int i=0;i<=left.rows;i++)
        for (int j=0;j<=left.cols;j++)
            if (left.mat[i][j]!=right.mat[i][j]) return 0;
    return 1;
}


int operator != (const Matrix& left, const Matrix& right) {
    return !(left==right);
}


Matrix operator + (const Matrix& m) {return m;}


Matrix operator - (const Matrix& m) {return -Type(1)*m;}


Matrix operator + (const Matrix& left, const Matrix& right) {
    Matrix m=left;
    return m+=right;
}


Matrix operator - (const Matrix& left, const Matrix& right) {
    Matrix m=left;
    return m-=right;
}


Matrix& Matrix :: operator+= (const Matrix& right) {
    if (rows!=right.rows || cols!=right.cols) return *this;
    for (int i=0;i<rows;i++)
        for (int j=0;j<cols;j++)
            mat[i][j]+=right.mat[i][j];
    return *this;

}


Matrix& Matrix :: operator-= (const Matrix& right) {
    if (rows!=right.rows || cols!=right.cols) return *this;
    for (int i=0;i<rows;i++)
        for (int j=0;j<cols;j++)
            mat[i][j]-=right.mat[i][j];
    return *this;

}

    
Matrix operator * (const Matrix& left, const Matrix& right) {
    Matrix m (left.rows, right.cols);
    if (left.cols!=right.rows) return m;
    for (int i=0;i<left.rows;i++)
        for (int j=0;j<right.cols;j++)
            for (int k=0;k<left.cols;k++)
                m.mat[i][j]+=left.mat[i][k]*right.mat[k][j];
    return m;

}


Matrix& Matrix :: operator*= (const Type& t) {
    for (int i=0;i<rows;i++)
        for (int j=0;j<cols;j++)
            mat[i][j]*=t;
    return *this;
}

        

Matrix operator * (const Matrix& left, const Type& t){
    Matrix m=left;
    return m*=t;
}


Matrix operator * (const Type& t, const Matrix& right){
    return right*t;
}


Matrix& Matrix::operator *= (const Matrix& right) {
    return *this=*this*right;
}


istream& operator >> (istream& is, Matrix& m) {
    for (int i=0;i<m.rows;i++)
        for (int j=0;j<m.cols;j++)
            is>>m.mat[i][j];
    return is;
}


ostream& operator << (ostream& os, const Matrix& m) {
    os<<"{\n";
    for (int i=0;i<m.rows;i++) {
        os <<"{";
        for (int j=0;j<m.cols;j++)
            os << m.mat[i][j] << ((j<m.cols-1)?",":"}");
        os << ((i<m.rows-1)?",\n":"\n}\n");
    }
    return os;
}


// transpose matrix
Matrix trans (const Matrix& m) {
    Matrix temp(m.cols, m.rows);
    for (int i=0;i<m.rows;i++)
        for (int j=0;j<m.cols;j++)
            temp.mat[j][i]=m.mat[i][j];
    return temp;
}


// one row and col is out
Matrix compl (const Matrix& m, int r, int c) {
    Matrix temp(m.rows-1, m.cols-1);
    if (m.cols<1 || m.rows<1) return temp;
    for (int i=0,x=0;i<m.rows;i++) {
        if (i==r-1) continue;
        for (int j=0,y=0;j<m.cols;j++) {
            if (j==c-1) continue;
            temp.mat[x][y]=m.mat[i][j];
            y++;
        }
        x++;
    }
    return temp;
}


// det matrix
Type det (const Matrix& m) {
    if (m.rows!=m.cols) return 0;
    if (m.rows==1) return m.mat[0][0];
    Type temp=Type(0);
    for (int j=0,c=1;j<m.cols;j++,c*=-1)
        temp+=(TypeŠ*m.mat[0][j]*det(compl(m,1,j+1)));
    return temp;
}


// cofact
Type cofact (const Matrix& m, int r, int c) {
    if (m.rows!=m.cols) return 0;
    if ((r+c)%2!=0)
        return (-det (compl(m,r,c)));
    else
        return (det (compl(m,r,c)));
}


// adj matrix
Matrix adj (const Matrix& m) {
    Matrix temp(m.rows, m.cols);
    if (m.rows!=m.cols) return temp;
    for (int i=0;i<m.rows;i++)
        for (int j=0;j<m.cols;j++)
            temp.mat[i][j]=cofact(m,i+1,j+1);
    return trans(temp);
}


// invers matrix
Matrix inv (const Matrix& m) {
    Type temp=det(m);
    if (temp!=0)
        return (1/temp)*adj(m);
    else
        return Matrix(0,0);
}

void main () {

#define NMAX 4

    Matrix m (NMAX,NMAX);

        cin>>m;


    
    const Matrix& m1=m;
    const Matrix m2=inv (m1);


cout<<"m="<<m2<<'\n';
}



Where:

CODE
CODE

// Modul matrix.h

#ifndef _Matrix
#define _Matrix

class istream;
class ostream;

typedef int Type;

class Matrix{
public:
    Matrix (int m, int n);
    Matrix (const Matrix& mat);
    ~Matrix ();

    Matrix& operator = (const Matrix&);

    friend int operator == (const Matrix&, const Matrix&);
    friend int operator != (const Matrix&, const Matrix&);
    Type& operator () (int i, int j);

    friend Matrix operator + (const Matrix&);
    friend Matrix operator - (const Matrix&);
    friend Matrix operator + (const Matrix&, const Matrix&);
    friend Matrix operator - (const Matrix&, const Matrix&);
    friend Matrix operator * (const Matrix&, const Matrix&);
    friend Matrix operator * (const Matrix&, const Type&);
    friend Matrix operator * (const Type&, const Matrix&);

    friend Matrix trans (const Matrix&);
    friend Matrix compl (const Matrix&, int i, int j);
    friend Type det (const Matrix&);
    friend Type cofact (const Matrix&, int i, int j);
    friend Matrix adj (const Matrix&);
    friend Matrix inv (const Matrix&);

    Matrix& operator += (const Matrix&);
    Matrix& operator -= (const Matrix&);
    Matrix& operator *= (const Matrix&);
    Matrix& operator *= (const Type&);


    friend istream& operator >> (istream&, Matrix&);
    friend ostream& operator << (ostream&, const Matrix&);

protected:
    void allocate ();
    void release ();
    void copy (const Matrix&);

private:
    int cols, rows;
    Type **mat;

};


#endif



You can also see doc file.
I can figure out what is problem with main function. It allow me to write elements of matrix but not to see what is hepenning in output. Can somebody help me with this?


*mod edit - added code tags

This post has been edited by jjhaag: 6 Jan, 2008 - 10:44 AM


Attached File(s)
Attached File  Matrix.doc ( 45k ) Number of downloads: 27
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Cout Problem
6 Jan, 2008 - 10:19 AM
Post #2

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,588



Thanked: 12 times
Dream Kudos: 325
My Contributions
Please post your code inside code tags. Like this code.gif
User is online!Profile CardPM
+Quote Post

PennyBoki
RE: Cout Problem
6 Jan, 2008 - 01:54 PM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi koprivica I believe you have trrouble with holding the execution window open? If yes then please check this thread:

http://www.dreamincode.net/forums/showtopic30581.htm

If that is not the problem then please say so. Hope this helps.
User is offlineProfile CardPM
+Quote Post

koprivica
RE: Cout Problem
8 Jan, 2008 - 02:03 PM
Post #4

New D.I.C Head
*

Joined: 13 Dec, 2007
Posts: 8


My Contributions
you are so close to figure out my problem. please, look attached doc file to see the message that I get when execute code.
other thinks that you indicate me whit your link are not working.

Thanks for helping me

P.S.

PennyBoki is interesting name. I already saw it on the net.


Attached File(s)
Attached File  Doc1.doc ( 91.5k ) Number of downloads: 15
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 01:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month