matrix

creat class have
*if two columns can be different sizes.
*the object can be resize.
*the object check if the user refers to an element that is out of range
*provide different matrix-oriented operation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
class myarray{
  private:
	 int p[3][3];
	int p2[100];
  public:
	 void diffrentsize(){
	int x;
	int y;
	int z;
	int w;
	cout<<"enter 4 size of array"<<endl;
  cin>>x;
  cin>>y;
  cin>>z;
  cin>>w;
  int *p4[4];
  p4[0]=new int[x];
  p4[1]=new int[y];
  p4[2]=new int[z];
  p4[3]=new int[w];
	  }
	  void setarray(vector<int> p[3][3],vector<int> p2[100]){
		  p[3][3]=p[3][3];
		  p2[100]=p2[100];
	  }
	  int getp(){
		  return p[3][3];
	  }
	  	  void size()
	  {
	int *arr;
	int size;
   int* resize_arr = new int[size + 1];
   for(int i = 0; i < size; i++)
        resize_arr[i] = arr[i];

   size++;
   arr = resize_arr;
   delete[] resize_arr;
	  }
         void check(vector<int> p2 ) {
          if(p2.length()>100){
          cout<<"the length of array is bigger than size of array"<<endl;
          }
		  else 
		  {cout<<"the length of array is fit the size of array"<<endl;}
		  }
  void det(vector<int> p[3][3]){
  cout<<endl<<"TO FIND DETERMINANT"<<endl;
	int n = 0,val;
	cout<<"enter no. of columns/rows of square matrix"<<endl;
	cin>>n;
	int **p3;
	p3 = new int*[n]; //make an array of ints, p points to them
	for (int i = 0; i< n; i++) 
	{
		p3[i] = new int[n]; //each pointer now points to 'n' amount of ints

	}
	int a  = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			p3[i][j] = a++; //assign some values
		}
	}
	cout<<"enter the values of matrix row-wise"<<endl;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin>>p3[i][j];
			if (n==1)
				val=p3[i][j];
		}
	}
        //using the formulae
	if (n==2)
	{val=p3[0][0]*p3[1][1]-p3[0][1]*p3[1][0];}
	if (n==3)
		{val=(p3[0][0]*((p3[1][1]*p3[2][2])-(p3[1][2]*p3[2][1])))-
		    (p3[0][1]*((p3[1][0]*p3[2][2])-(p3[1][2]*p3[2][0])))+
			(p3[0][2]*((p3[1][0]*p3[2][1])-(p3[1][1]*p3[2][0])));}
        for (int i = 0; i < 3; i++)
	{
		delete((int*)p[i]);//free up the array of pointers, must delete each one otherwise memory will leak

	}
	delete [] p;
	cout<<"determinant value is:";
	cout<<val<<endl;
	
}
void transpose(vector<int> p[3][3]){
	int n = 0,m=0;
	cout<<"TO FIND TRANSPOSE"<<endl;
	cout<<"enter no. of rows and columns of matrix"<<endl;
	cin>>m>>n;
	int **p3;
	p3 = new int*[m];
	for (int i = 0; i< m; i++)
	{
		p3[i] = new int[n];
	}
	int a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			p3[i][j] = a++;
		}
	}
	cout<<"enter the numbers of matrix row-wise"<<endl;
	for (int i = 0; i < m; i++) //input values
	{
		for (int j = 0; j < n; j++)
		{
			cin>>p3[i][j];
		}
	}
	cout<<"matrix you have entered is"<<endl;
	for (int i = 0; i < m; i++) //output matrix entered
	{
		for (int j = 0; j < n; j++)
		{
			cout << p3[i][j] << "\t";
		}
		cout << "\n";
	}
	int **q;
	q = new int*[m];
	cout<<endl;
	for (int i = 0; i< m; i++) //create the matrix(to be transpose)
	{
		q[i] = new int[n];
	}
	a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			q[i][j] = a++;
		}
	}
	for (int i = 0; i < n; i++)//assigning corresponding values to make a transpose
	{
		for (int j = 0; j < m; j++)
		{
			q[i][j] = p3[j][i];
		}
	}
	cout<<"transpose is"<<endl;
	for (int i = 0; i < n; i++) //output transpose
	{
		for (int j = 0; j < m; j++)
		{
			cout << q[i][j] << "\t";
		}
		cout << "\n";
	}
	for (int i = 0; i < m; i++)
	{
		delete((int*)q[i]);
	}
	delete [] q;
	
}
void add(vector<int> p[3][3]){
	int n = 0,m=0;
	cout<<"TO FIND THE SUM"<<endl;
	cout<<"enter no. of rows and columns of matrix"<<endl;
	cin>>m>>n;
	int **t,**q;
	t = new int*[m];
	for (int i = 0; i< m; i++)
	{
		t[i] = new int[n];
	}
	int a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			t[i][j] = 0;
		}
	}
	char chk='y'; //any number of matrices can be added
	while (chk=='y'||chk=='Y'){
		q = new int*[m];
		for (int i = 0; i< m; i++)
		{
			q[i] = new int[n];
		}
		a  = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				q[i][j] = a++;
			}
		}
		cout<<"enter the numbers of matrix to be added"<<endl;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin>>q[i][j];
			}
		}
		cout<<"matrix you have entered"<<endl;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cout << q[i][j] << "\t";
			}
			cout << "\n";
		}
		for (int i = 0; i < m; i++) //addition
		{
			for (int j = 0; j < n; j++)
			{
				t[i][j] = t[i][j]+q[i][j];
			}
		}
		for (int i = 0; i < m; i++)
		{
			delete((int*)q[i]);
		}
		delete [] q;
		cout<<"wish to add another matrix to this? y/n"<<endl;
		cin>>chk;
	}
	cout<<"sum is "<<endl;
	for (int i = 0; i < m; i++) //output sum
	{
		for (int j = 0; j < n; j++)
		{
			cout << t[i][j] << "\t";
		}
		cout << "\n";
	}
	for (int i = 0; i < m; i++)
	{
		delete((int*)t[i]);
	}
	delete [] t;
	
}
void subtract(vector <int> p[3][3]){
	cout<<"TO SUBTRACT"<<endl;
	int n = 0,m=0;
	cout<<"enter no. of rows and columns of matrix"<<endl;
	cin>>m>>n;
	int **t,**q;
	char chk;
	t = new int*[m];
	for (int i = 0; i< m; i++)
	{
		t[i] = new int[n];
	}
	int a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			t[i][j] = 0;
		}
	}
	cout<<"enter the numbers of matrix"<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin>>t[i][j];
		}
	}
	cout<<"matrix you have entered"<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << t[i][j] << "\t";
		}
		cout << "\n";
	}
	chk='y';
	while (chk=='y'||chk=='Y'){
		q = new int*[n];
		for (int i = 0; i< m; i++)
		{
			q[i] = new int[n];
		}
		a  = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				q[i][j] = a++;
			}
		}
		cout<<"enter the numbers of matrix to be subtracted"<<endl;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin>>q[i][j];
			}
		}
		cout<<"matrix you have entered"<<endl;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cout << q[i][j] << "\t";
			}
			cout << "\n";
		}
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				t[i][j] = t[i][j]-q[i][j];
			}
		}
		for (int i = 0; i < m; i++)
		{
			delete((int*)q[i]);
		}
		delete [] q;
		cout<<"wish to subtract another matrix from the result? y/n"<<endl;
		cin>>chk;
	}
	cout<<"difference is "<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << t[i][j] << "\t";
		}
		cout << "\n";
	}
	for (int i = 0; i < n; i++)
	{
		delete((int*)t[i]);
	}
	delete [] t;
	
}
void multiply(vector<int> p[3][3]){
	int n = 0,m=0,l=0;
	cout<<"TO FIND THE PRODUCT"<<endl;
	cout<<"enter no. of rows and columns of matrix"<<endl;
	cin>>m>>n;
	int **t,**q;
	t = new int*[m];
	for (int i = 0; i< m; i++)
	{
		t[i] = new int[n];
	}
	int a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			t[i][j] = 0;
		}
	}
	cout<<"enter the numbers of matrix "<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin>>t[i][j];
		}
	}
	cout<<"matrix you have entered"<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << t[i][j] << "\t";
		}
		cout << "\n";
	}
	cout<<"enter no. of columns of 2nd matrix"<<endl;//no.of rows of 2nd column=no.of columns of 1st matrix(rule)
	cin>>l;
	q = new int*[n];
	for (int i = 0; i< n; i++)
	{
		q[i] = new int[l];
	}
	a  = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < l; j++)
		{
			q[i][j] = a++;
		}
	}
	cout<<"enter the numbers of matrix to be multiplied"<<endl;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < l; j++)
		{
			cin>>q[i][j];
		}
	}
	cout<<"matrix you have entered"<<endl;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < l; j++)
		{
			cout << q[i][j] << "\t";
		}
		cout << "\n";
	}
	cout<<"product is"<<endl;
	int k=0;
	for (int i=0;i<m;i++)
	{
		for (int j=0;j<l;j++)
		{
			for (int r=1;r<=n;r++)
			{
				k=k+(t[i][r-1]*q[r-1][j]);
			}
			cout<<k<<"\t";
			k=0;
		}
		cout<<endl;
	}
	
}

void post(vector <int>p[3][3]){
	int po[3][3];
	int x=1;
	for (int i=0;i<3;i++){
	for(int j=0;j<3;j++){
		cin>>po[i][j];
		if(po[i][j]*-1==-po[i][j]||po[i][j]*-1==po[i][j]){
			x*=po[i][j];}
	}
	}
	cout<<"the check if matrix is positive-definite "<<endl;
	if (x=='-'){
		cout<<"negitive"<<endl;}
	else {cout<<"positive"<<endl;}
}
};
Last edited on
O man! who has taught you C++, who has taught you to code in that way ? It is terrible, and awful.
Last edited on
Ok, therockon7throw was right. That is ugly. That looks like FORTRAN. Word to the wise, you won't get many responses with code looking like that. Explain what you are trying to do better than just giving a bunch of code with minimal comments.

Listen -- AVOID hardcoded numbers! This is a computer program not pencil and paper. Especially since you want to resize things. And learn what a vector is. It is a dynamically allocating array. You don't want to have a function parameter that takes a vector argument like std::vector<int> p[3][3]. Hell, I am not even sure if that is valid bc I cant begin to understand what you are trying to accomplish there.

So for what you first wrote:
creat class have
*if two columns can be different sizes.

*the object can be resize.

poor english but I am guessing this means you want a vector. Use the push_back() function to add on to the end, or clear() and resize() if you want to start from scratch.


*the object check if the user refers to an element that is out of range

You want a try catch exception for this.
in other words:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdexcept>
#include <iostream>
#include <exception>
#include <vector>

int main(int argc, char* argv[]) 
{
   std::vector myVector;
   myVector.resize(5,0); // initialize vector to ndim = 5, value = 0
   myVector.push_back(1); // resize the vector to ndim = 6 with myVector[5] = 1

   int someNumber = 8;
   try {
      myVector.at(someNumber);
   } catch (std::out_of_range const &ex) {
      std::cout << "Exception: " << ex.what() << " -- "<< someNumber 
      << " is out of the allowed range" << std::endl;
   }

}


*provide different matrix-oriented operation

with vectors you can access this sort of information with size()
in other words:
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

unsigned int ndim = 10; // 10x10 square matrix

// notice no hard coded numbers except for ndim which wouldn't
// really exist because you should be passing "second" to the function 
// and returning transpose

std::vector<double> first;
std::vector<std::vector<double> > second;
first.resize(ndim,0);   // initialize all ten rows to zero
second.resize(ndim,first); // initialize ten columns of zero from first

int num = 1;
// second is 10x10 square matrix, transpose will also be 10x10
// try using this function, add in code to print out second and transpose
std::vector<std::vector<double> > transpose = second;
// transpose the matrix, notice no h
for(unsigned int i = 0; i < second.size(); ++i) {
   for(unsigned int j = 0; j < second[i].size(); ++j) {
      second[i][j] = num;
      transpose[j][i] = second[i][j];
      ++num;
   }
}
Topic archived. No new replies allowed.