Diagnolizing matrix

Hi

Im trying to write a member function to diagnolize dynamically allocated matrices (arrays).

i cant think of a way i would do this.

i have the set so that i can multiply, scalar multiply, add and subtract matrices, so i have all the needed operators overloaded and working correctly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "Matrix.h"
#include <iostream>
Matrix::Matrix()
{
	row=0;
	col=0;
	address=NULL;
}

Matrix::diag(Matrix Mat1)
{



Matrix::Matrix(int a, int b)
{
	if(a<1||b<1)
	{
		cout << " Wrong num" << endl;
	}
	row = a ;
	col = b;

	address=new double*[row];
	for(int i=0;i<row;i++)
		address[i]=new double[col];

	for(int i=0;i<row;i++)
		for(int j=0;j<col;j++)
			address[i][j]=0;



}


Matrix::Matrix(int a, int b,int c)
{
        // I added this block to create memory
	if(a<1||b<1)
	{
		cout << " Wrong num" << endl;
	}
	row = a ;
	col = b ;

	address=new double*[row];
	for(int i=0;i<row;i++)
		address[i]=new double[col];
	//Done editing


	for(int i=0;i<row;i++)
		for(int j=0;j<col;j++)
			address[i][j]=c;
}


Matrix::~Matrix()
{
	for(int i=0;i<row;i++)
		delete [] address[i];
	
	delete [] address;

}

Matrix::Matrix(const Matrix &copy)
{
	col=copy.col;
	row=copy.row;

	address= new double*[row];
	for(int i=0;i<row;i++)
		address[i]=new double[col];

	for(int i=0;i<row;i++)
		for(int j=0;j<col;j++)
			address[i][j]=copy.address[i][j];

}

//operator = does not return anything.  It just copying everything over.
void Matrix::operator = (Matrix copy)
{
	// need to clear out junk before put anything in
	for(int i=0;i<row;i++)
		delete [] address[i];
	
	delete [] address;

	col=copy.col;
	row=copy.row;

	address= new double*[row];
	for(int i=0;i<row;i++)
	address[i]=new double[col];
	

	for(int i=0;i<row;i++)
		for(int j=0;j<col;j++)
			address[i][j]=copy.address[i][j];
	//return copy;

}

istream& operator >> (istream &in, Matrix &Mat1)
{

	// need to clear out junk before put anything in
	for(int i=0;i<Mat1.row;i++)
		delete [] Mat1.address[i];
	
	delete [] Mat1.address;

	//make a little nicer
	int r , c;
	in >> r >> c;
	Mat1.row = r;
	Mat1.col = c;

	if(Mat1.address==NULL)
	{
		Mat1.address=new double*[r];
		for(int i=0;i<Mat1.row;i++)
		Mat1.address[i]=new double[c];
	}

	for(int i=0;i<r;i++)
	{
		for(int j=0;j<c;j++)
			in >> Mat1.address[i][j];
	}

	return in;
}

ostream& operator << (ostream& out, Matrix Mat1)
{

	for(int i=0;i<Mat1.row;i++)	
	{
		for(int j=0;j<Mat1.col;j++)
			out << "\t" << Mat1.address[i][j];
		out << "\n";
	}
	out << endl;

return out;
}


//This operator need to pass in two arguments.
Matrix operator * (Matrix Mat1, Matrix Mat2)
{
	if (Mat1.row != Mat2.row || Mat1.col != Mat2.col)
	cout << "Make sure the dimensions of the matrices are the same"<<endl;
	exit(1);
        int sum;

	Matrix temp( Mat1.row, Mat2.col);
	

	for (int i=0; i<temp.row; i++)
		for (int j=0; j<temp.col; j++)
                 { sum = 0;
		    for (int k=0; k<temp.col; k++){
			temp.address[i][j]+= Mat1.address[i][k] * Mat2.address[k][j];
			 
			}
                 }





    return temp;
}
   Matrix operator * (Matrix Mat1, double scalar) 
{
Matrix temp1(Mat1.row, Mat1.col);

	for (int i=0; i<temp1.row; i++)
		for (int j=0; j<temp1.col;j++)
			temp1.address[i][j] = scalar * Mat1.address[i][j];
return temp1;


}

  Matrix operator - (Matrix Mat1, Matrix Mat2) 
{
if (Mat1.row != Mat2.row || Mat1.col != Mat2.col)
	cout << "Make sure the dimensions of the matrices are the same"<<endl;
exit(1);
Matrix temp2(Mat1.row, Mat2.col);

	for (int i=0; i<temp2.row; i++)
		for (int j=0; j<temp2.col;j++)
			temp2.address[i][j] = Mat1.address[i][j] - Mat2.address[i][j];
return temp2;
}



  Matrix operator + (Matrix Mat1, Matrix Mat2) 
{
if (Mat1.row != Mat2.row || Mat1.col != Mat2.col)
	cout << "Make sure the dimensions of the matrices are the same"<<endl;
exit(1);
Matrix temp3(Mat1.row, Mat2.col);

	for (int i=0; i<temp3.row; i++)
		for (int j=0; j<temp3.col;j++)
			temp3.address[i][j] = Mat1.address[i][j] + Mat2.address[i][j];
return temp3;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Matrix.h"
#include <iostream>
using namespace std;


int main()
{
	Matrix M1;
	Matrix M2;
	Matrix temp;
	double scalar;
	char input;

	cout << "what would you like to do (multipy=m, matmultiply=M, subtract=s, add=a)?"<<endl;
	scanf("%c", &input);
		if (input == 'm'){
			cout << "enter the dimensons followed by the matrix"<< endl;
				cin >> M1;
			cout << "enter the scalar" <<endl;
				cin >> scalar;
				temp = M1*scalar;
				cout << temp;

				}
		else if (input == 'M'){
			cout << "enter the dimensons of the two, followed by the two matrices (must be same size)"<< endl;
				cin >> M1;
				cin >> M2;
				temp = M1*M2;
				cout << temp;
				}
		
		else if (input == 's'){	
			cout << "enter the dimensons of the two, followed by the two matrices (must be same size)"<< endl;
			cin >> M1;
			cin >> M2;
			temp = M1-M2;
			cout << temp;
					}
			
		else if (input == 'a'){	
			cout << "enter the dimensons of the two, followed by the two matrices (must be same size)"<< endl;
			cin >> M1;
			cin >> M2;
			temp = M1+M2;
			cout << temp;
					}

	







return 0;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

class Matrix
{
private:
	int row;
	int col;
	double** address;
	
public:
	
	Matrix();
	Matrix(int a,int b);
	Matrix(int a, int b,int c);
	~Matrix();
	Matrix(const Matrix &);
	Matrix diag(Matrix Mat1);
	
	//Matrix operator =(Matrix copy);
	void operator=(Matrix copy); //assignment does not return anything
	friend ostream& operator << (ostream&, Matrix);
	friend istream& operator >> (istream&, Matrix&);
	friend Matrix operator*(Matrix Mat1, Matrix Mat2);// need to pass in as two argument
	friend Matrix operator*(Matrix Mat1, double scalar);// need to pass in as two argument
	friend Matrix operator-(Matrix Mat1, Matrix Mat2);// need to pass in as two argument
	friend Matrix operator+(Matrix Mat1, Matrix Mat2);// need to pass in as two argument
};




any help is appreciated.
bump.
Topic archived. No new replies allowed.